Skip to content

Commit

Permalink
fix(bundling): support exported array of options for rollup (#22703)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo committed Apr 5, 2024
1 parent b9ecf1a commit 430aecc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
26 changes: 26 additions & 0 deletions e2e/rollup/src/rollup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
});
});
20 changes: 12 additions & 8 deletions packages/rollup/src/executors/rollup/rollup.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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';
Expand Down Expand Up @@ -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') {
Expand Down

0 comments on commit 430aecc

Please sign in to comment.