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