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

ESM: Ability to generate ESM-only for select entrypoints in core #29320

Draft
wants to merge 9 commits into
base: norbert/esm-runtime
Choose a base branch
from
Draft
16 changes: 8 additions & 8 deletions code/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,16 @@
"import": "./dist/preview/globals.js",
"require": "./dist/preview/globals.cjs"
},
"./cli": {
"types": "./dist/cli/index.d.ts",
"import": "./dist/cli/index.js",
"require": "./dist/cli/index.cjs"
},
"./babel": {
"types": "./dist/babel/index.d.ts",
"import": "./dist/babel/index.js",
"require": "./dist/babel/index.cjs"
},
"./cli": {
"types": "./dist/cli/index.d.ts",
"import": "./dist/cli/index.js",
"require": "./dist/cli/index.cjs"
},
"./cli/bin": {
"types": "./dist/cli/bin/index.d.ts",
"import": "./dist/cli/bin/index.js",
Expand Down Expand Up @@ -255,12 +255,12 @@
"preview/globals": [
"./dist/preview/globals.d.ts"
],
"cli": [
"./dist/cli/index.d.ts"
],
"babel": [
"./dist/babel/index.d.ts"
],
"cli": [
"./dist/cli/index.d.ts"
],
"cli/bin": [
"./dist/cli/bin/index.d.ts"
]
Expand Down
4 changes: 2 additions & 2 deletions code/core/scripts/dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { join } from 'node:path';

import { dts, nodeInternals, process } from '../../../scripts/prepare/tools';
import pkg from '../package.json';
import { getEntries } from './entries';
import { getEntries, getModernEntries } from './entries';

async function run() {
const cwd = process.cwd();
Expand All @@ -11,7 +11,7 @@ async function run() {

const selection = flags[0] || 'all';

const entries = getEntries(cwd);
const entries = [...getEntries(cwd), ...getModernEntries(cwd)];
const external = [
...Object.keys((pkg as any).dependencies || {}),
...Object.keys((pkg as any).peerDependencies || {}),
Expand Down
10 changes: 9 additions & 1 deletion code/core/scripts/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,16 @@ export const getEntries = (cwd: string) => {
define('src/manager/globals-module-info.ts', ['node'], true),
define('src/manager/globals.ts', ['node'], true),
define('src/preview/globals.ts', ['node'], true),
define('src/cli/index.ts', ['node'], true),
define('src/babel/index.ts', ['node'], true),
];
};

export const getModernEntries = (cwd: string) => {
const define = defineEntry(cwd);

return [
//
define('src/cli/index.ts', ['node'], true),
define('src/cli/bin/index.ts', ['node'], true),
];
};
Expand Down
37 changes: 33 additions & 4 deletions code/core/scripts/prep.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable local-rules/no-uncategorized-errors */
import { existsSync, mkdirSync, watch } from 'node:fs';
import { existsSync, watch } from 'node:fs';
import { mkdir, rm, writeFile } from 'node:fs/promises';
import { dirname, join } from 'node:path';

Expand All @@ -18,7 +18,7 @@ import {
} from '../../../scripts/prepare/tools';
import pkg from '../package.json';
import { globalsModuleInfoMap } from '../src/manager/globals-module-info';
import { getBundles, getEntries, getFinals } from './entries';
import { getBundles, getEntries, getFinals, getModernEntries } from './entries';
import { generatePackageJsonFile } from './helpers/generatePackageJsonFile';
import { generateTypesFiles } from './helpers/generateTypesFiles';
import { generateTypesMapperFiles } from './helpers/generateTypesMapperFiles';
Expand Down Expand Up @@ -53,17 +53,18 @@ async function run() {
const entries = getEntries(cwd);
const bundles = getBundles(cwd);
const finals = getFinals(cwd);
const modernEntries = getModernEntries(cwd);

type EsbuildContextOptions = Parameters<(typeof esbuild)['context']>[0];

console.log(isWatch ? 'Watching...' : 'Bundling...');

const files = measure(generateSourceFiles);
const packageJson = measure(() => generatePackageJsonFile(entries));
const packageJson = measure(() => generatePackageJsonFile([...entries, ...modernEntries]));
const dist = files.then(() => measure(generateDistFiles));
const types = files.then(() =>
measure(async () => {
await generateTypesMapperFiles(entries);
await generateTypesMapperFiles([...entries, ...modernEntries]);
await modifyThemeTypes();
await generateTypesFiles(entries, isOptimized, cwd);
await modifyThemeTypes();
Expand Down Expand Up @@ -183,6 +184,33 @@ async function run() {
},
})
),
...modernEntries.map((entry) => {
return esbuild.context(
merge<EsbuildContextOptions>(nodeEsbuildOptions, {
banner: {
js: dedent`
import ESM_COMPAT_Module from "node:module";
import { fileURLToPath as ESM_COMPAT_fileURLToPath } from 'node:url';
import { dirname as ESM_COMPAT_dirname } from 'node:path';
const __filename = ESM_COMPAT_fileURLToPath(import.meta.url);
const __dirname = ESM_COMPAT_dirname(__filename);
const require = ESM_COMPAT_Module.createRequire(import.meta.url);
`,
},
entryPoints: [entry.file],
format: 'esm',
external: [
...nodeInternals,
...esbuildDefaultOptions.external,
...entry.externals,
].filter((e) => !entry.internals.includes(e)),
outdir: dirname(entry.file).replace('src', 'dist'),
outExtension: {
'.js': '.js',
},
})
);
}),
...bundles.flatMap((entry) => {
const results = [];
results.push(
Expand Down Expand Up @@ -363,6 +391,7 @@ async function run() {

await Promise.all(
Object.entries(metafileByModule).map(async ([moduleName, metafile]) => {
await mkdir(join(metafilesDir, moduleName), { recursive: true });
await writeFile(
join(metafilesDir, `${moduleName}.json`),
JSON.stringify(metafile, null, 2)
Expand Down
5 changes: 4 additions & 1 deletion code/lib/cli-storybook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@
"./src/index.ts",
"./src/bin/index.ts"
],
"platform": "node"
"platform": "node",
"formats": [
"esm"
]
},
"gitHead": "e6a7fd8a655c69780bc20b9749c2699e44beae16"
}
9 changes: 5 additions & 4 deletions code/lib/create-storybook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
"exports": {
".": {
"types": "./dist/index.d.ts",
"node": "./dist/index.cjs",
"import": "./dist/index.js",
"require": "./dist/index.cjs"
"import": "./dist/index.js"
},
"./bin/index.cjs": {
"node": "./bin/index.cjs",
Expand Down Expand Up @@ -83,7 +81,10 @@
"./src/index.ts",
"./src/bin/index.ts"
],
"platform": "node"
"platform": "node",
"formats": [
"esm"
]
},
"gitHead": "e6a7fd8a655c69780bc20b9749c2699e44beae16"
}