fix(bundling): support exported array of options for rollup (#22703)

This commit is contained in:
Jack Hsu 2024-04-05 15:45:19 -04:00 committed by GitHub
parent b9ecf1ab5a
commit 430aecc156
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 38 additions and 8 deletions

View File

@ -210,4 +210,30 @@ export default config;
checkFilesExist(`libs/test/dist/bundle.js`); checkFilesExist(`libs/test/dist/bundle.js`);
checkFilesExist(`libs/test/dist/bundle.es.js`); checkFilesExist(`libs/test/dist/bundle.es.js`);
}); });
it('should support array config from rollup.config.js', () => {
const jsLib = uniq('jslib');
runCLI(`generate @nx/js:lib ${jsLib} --bundler rollup`);
updateFile(
`libs/${jsLib}/rollup.config.js`,
`module.exports = (config) => [{
...config,
output: {
format: "esm",
dir: "dist/test",
name: "Mylib",
entryFileNames: "[name].js",
chunkFileNames: "[name].js"
}
}]`
);
updateJson(join('libs', jsLib, 'project.json'), (config) => {
config.targets.build.options.rollupConfig = `libs/${jsLib}/rollup.config.js`;
return config;
});
expect(() => runCLI(`build ${jsLib} --format=esm`)).not.toThrow();
checkFilesExist(`dist/test/index.js`);
});
}); });

View File

@ -118,14 +118,18 @@ export async function* rollupExecutor(
} }
const start = process.hrtime.bigint(); const start = process.hrtime.bigint();
const bundle = await rollup.rollup(rollupOptions); const allRollupOptions = Array.isArray(rollupOptions)
const output = Array.isArray(rollupOptions.output) ? rollupOptions
? rollupOptions.output : [rollupOptions];
: [rollupOptions.output];
for (const opts of allRollupOptions) {
const bundle = await rollup.rollup(opts);
const output = Array.isArray(opts.output) ? opts.output : [opts.output];
for (const o of output) { for (const o of output) {
await bundle.write(o); await bundle.write(o);
} }
}
const end = process.hrtime.bigint(); const end = process.hrtime.bigint();
const duration = `${(Number(end - start) / 1_000_000_000).toFixed(2)}s`; const duration = `${(Number(end - start) / 1_000_000_000).toFixed(2)}s`;
@ -150,7 +154,7 @@ export async function createRollupOptions(
packageJson: PackageJson, packageJson: PackageJson,
sourceRoot: string, sourceRoot: string,
npmDeps: string[] npmDeps: string[]
): Promise<rollup.RollupOptions> { ): Promise<rollup.RollupOptions | rollup.RollupOptions[]> {
const useBabel = options.compiler === 'babel'; const useBabel = options.compiler === 'babel';
const useTsc = options.compiler === 'tsc'; const useTsc = options.compiler === 'tsc';
const useSwc = options.compiler === 'swc'; const useSwc = options.compiler === 'swc';
@ -287,7 +291,7 @@ export async function createRollupOptions(
const userDefinedRollupConfigs = options.rollupConfig.map((plugin) => const userDefinedRollupConfigs = options.rollupConfig.map((plugin) =>
loadConfigFile(plugin) loadConfigFile(plugin)
); );
let finalConfig: rollup.InputOptions = rollupConfig; let finalConfig: rollup.RollupOptions = rollupConfig;
for (const _config of userDefinedRollupConfigs) { for (const _config of userDefinedRollupConfigs) {
const config = await _config; const config = await _config;
if (typeof config === 'function') { if (typeof config === 'function') {