Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ESM export of hydrate app. #5814

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,18 @@ export const generateHydrateApp = async (
};

const rollupAppBuild = await rollup(rollupOptions);
const rollupOutput = await rollupAppBuild.generate({
const rollupCjsOutput = await rollupAppBuild.generate({
banner: generatePreamble(config),
format: 'cjs',
file: 'index.js',
});
const rollupEsmOutput = await rollupAppBuild.generate({
banner: generatePreamble(config),
format: 'esm',
file: 'index.mjs',
});

await writeHydrateOutputs(config, compilerCtx, buildCtx, outputTargets, rollupOutput);
await writeHydrateOutputs(config, compilerCtx, buildCtx, outputTargets, rollupCjsOutput, rollupEsmOutput);
} catch (e: any) {
if (!buildCtx.hasError) {
// TODO(STENCIL-353): Implement a type guard that balances using our own copy of Rollup types (which are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ export const writeHydrateOutputs = (
compilerCtx: d.CompilerCtx,
buildCtx: d.BuildCtx,
outputTargets: d.OutputTargetHydrate[],
rollupOutput: RollupOutput,
rollupCjsOutput: RollupOutput,
rollupEsmOutput: RollupOutput,
) => {
return Promise.all(
outputTargets.map((outputTarget) => {
return writeHydrateOutput(config, compilerCtx, buildCtx, outputTarget, rollupOutput);
return writeHydrateOutput(config, compilerCtx, buildCtx, outputTarget, rollupCjsOutput, rollupEsmOutput);
}),
);
};
Expand All @@ -24,19 +25,22 @@ const writeHydrateOutput = async (
compilerCtx: d.CompilerCtx,
buildCtx: d.BuildCtx,
outputTarget: d.OutputTargetHydrate,
rollupOutput: RollupOutput,
rollupCjsOutput: RollupOutput,
rollupEsmOutput: RollupOutput,
) => {
const hydratePackageName = await getHydratePackageName(config, compilerCtx);

const hydrateAppDirPath = outputTarget.dir;

const hydrateCoreIndexPath = join(hydrateAppDirPath, 'index.js');
const hydrateCoreIndexCjsPath = join(hydrateAppDirPath, 'index.js');
const hydrateCoreIndexEsmPath = join(hydrateAppDirPath, 'index.mjs');
const hydrateCoreIndexDtsFilePath = join(hydrateAppDirPath, 'index.d.ts');

const pkgJsonPath = join(hydrateAppDirPath, 'package.json');
const pkgJsonCode = getHydratePackageJson(
config,
hydrateCoreIndexPath,
hydrateCoreIndexCjsPath,
hydrateCoreIndexEsmPath,
hydrateCoreIndexDtsFilePath,
hydratePackageName,
);
Expand All @@ -47,10 +51,10 @@ const writeHydrateOutput = async (
]);

// always remember a path to the hydrate app that the prerendering may need later on
buildCtx.hydrateAppFilePath = hydrateCoreIndexPath;
buildCtx.hydrateAppFilePath = hydrateCoreIndexCjsPath;

await Promise.all(
rollupOutput.output.map(async (output) => {
[...rollupCjsOutput.output, ...rollupEsmOutput.output].map(async (output) => {
if (output.type === 'chunk') {
output.code = relocateHydrateContextConst(config, compilerCtx, output.code);
const filePath = join(hydrateAppDirPath, output.fileName);
Expand All @@ -62,14 +66,16 @@ const writeHydrateOutput = async (

const getHydratePackageJson = (
config: d.ValidatedConfig,
hydrateAppFilePath: string,
hydrateAppCjsFilePath: string,
hydrateAppEsmFilePath: string,
hydrateDtsFilePath: string,
hydratePackageName: string,
) => {
const pkg: d.PackageJsonData = {
name: hydratePackageName,
description: `${config.namespace} component hydration app.`,
main: basename(hydrateAppFilePath),
main: basename(hydrateAppCjsFilePath),
module: basename(hydrateAppEsmFilePath),
types: basename(hydrateDtsFilePath),
};
return JSON.stringify(pkg, null, 2);
Expand Down