Skip to content

Commit

Permalink
Scripts: Only output esm for nodeEntries on request
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinpalkovic committed Jan 8, 2025
1 parent 125bdd3 commit 50587ef
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 46 deletions.
18 changes: 15 additions & 3 deletions code/addons/test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,23 @@
],
"nodeEntries": [
"./src/preset.ts",
"./src/vitest-plugin/index.ts",
"./src/vitest-plugin/global-setup.ts",
"./src/postinstall.ts",
"./src/node/vitest.ts",
"./src/node/coverage-reporter.ts"
"./src/node/coverage-reporter.ts",
{
"file": "./src/vitest-plugin/index.ts",
"format": [
"cjs",
"esm"
]
},
{
"file": "./src/vitest-plugin/global-setup.ts",
"format": [
"cjs",
"esm"
]
}
]
},
"storybook": {
Expand Down
124 changes: 81 additions & 43 deletions scripts/prepare/addon-bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,19 @@ import { esbuild } from './tools';

/* TYPES */

/**
* A NodeEntry can be a string or an object with a file and format property. If it's a string, it's
* the path to the entry file and will be built for CJS. If it's an object, 'file' is the path to
* the entry file and the 'formats' to build it in. The formats property can be 'esm', 'cjs', or
* both.
*/
type NodeEntry = string | { file: string; formats: Formats[] };

type Formats = 'esm' | 'cjs';
type BundlerConfig = {
previewEntries: string[];
managerEntries: string[];
nodeEntries: string[];
nodeEntries: NodeEntry[];
exportEntries: string[];
externals: string[];
pre: string;
Expand Down Expand Up @@ -211,59 +219,89 @@ const run = async ({ cwd, flags }: { cwd: string; flags: string[] }) => {
}

if (nodeEntries.length > 0) {
const { dtsConfig, tsConfigExists } = await getDTSConfigs({
formats: ['esm'],
entries: nodeEntries,
optimized,
});
tasks.push(async () => {
await Promise.all([
build({
...commonOptions,
entry: nodeEntries.map((e: string) => slash(join(cwd, e))),
format: ['cjs'],
target: 'node18',
platform: 'node',
external: commonExternals,
esbuildOptions: (c) => {
c.platform = 'node';
Object.assign(c, getESBuildOptions(optimized));
},
}),
build({
...commonOptions,
...(optimized ? dtsConfig : {}),
entry: nodeEntries.map((e: string) => slash(join(cwd, e))),
format: ['esm'],
target: 'node18',
platform: 'neutral',
banner: {
js: dedent`
const cjsEntries = nodeEntries.filter(
(n) => typeof n === 'string' || n.formats.includes('cjs')
);

const esmEntries = nodeEntries.filter(
(n) => typeof n !== 'string' && n.formats.includes('esm')
) as (NodeEntry & object)[];

const builds = [];

if (cjsEntries.length > 0) {
builds.push(() =>
build({
...commonOptions,
entry: cjsEntries.map((e) => slash(join(cwd, typeof e === 'string' ? e : e.file))),
format: ['cjs'],
target: 'node18',
platform: 'node',
external: commonExternals,
esbuildOptions: (c) => {
c.platform = 'node';
Object.assign(c, getESBuildOptions(optimized));
},
})
);
}

if (esmEntries.length > 0) {
const { dtsConfig, tsConfigExists } = await getDTSConfigs({
formats: ['esm'],
entries: esmEntries.map((e) => e.file),
optimized,
});

builds.push(() =>
build({
...commonOptions,
...(optimized ? dtsConfig : {}),
entry: esmEntries.map((e) => slash(join(cwd, e.file))),
format: ['esm'],
target: 'node18',
platform: 'neutral',
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);
`,
},
external: [...commonExternals, ...nodeInternals],
esbuildOptions: (c) => {
c.mainFields = ['main', 'module', 'node'];
c.conditions = ['node', 'module', 'import', 'require'];
c.platform = 'neutral';
Object.assign(c, getESBuildOptions(optimized));
},
}),
]);
},
external: [...commonExternals, ...nodeInternals],
esbuildOptions: (c) => {
c.mainFields = ['main', 'module', 'node'];
c.conditions = ['node', 'module', 'import', 'require'];
c.platform = 'neutral';
Object.assign(c, getESBuildOptions(optimized));
},
})
);

if (tsConfigExists && !optimized) {
tasks.push(...esmEntries.map((entry) => () => generateDTSMapperFile(entry.file)));
}
}

await Promise.all(builds.map((b) => b()));

if (!watch) {
await readMetafiles({ formats: ['esm', 'cjs'] });
const metafileFormats: Formats[] = [];

if (cjsEntries.length > 0) {
metafileFormats.push('cjs');
}

if (esmEntries.length > 0) {
metafileFormats.push('esm');
}
await readMetafiles({ formats: metafileFormats });
}
});

if (tsConfigExists && !optimized) {
tasks.push(...nodeEntries.map((entry) => () => generateDTSMapperFile(entry)));
}
}

for (const task of tasks) {
Expand Down

0 comments on commit 50587ef

Please sign in to comment.