diff --git a/packages/multi-entry/README.md b/packages/multi-entry/README.md index 5d531d69d..c6bb745b9 100755 --- a/packages/multi-entry/README.md +++ b/packages/multi-entry/README.md @@ -80,6 +80,13 @@ Default: `'multi-entry.js'` `entryFileName` changes the name of the generated entry file. By default, it will override `outputOptions.entryFileNames` to be `'multi-entry.js'`. +### `preserveModules` + +Type: `Boolean`
+Default: `false` + +`preserveModules` is to be used in conjunction with [`output.preserveModules`](https://rollupjs.org/guide/en/#outputpreservemodules). If `true`, overrides the `entryFileName` option to be output.entryFileNames. If `false`, the plugin will respect the `entryFileName` option. + ## Supported Input Types This plugin extends Rollup's `input` option to support multiple new value types, in addition to a `String` specifying a path to a file. diff --git a/packages/multi-entry/src/index.js b/packages/multi-entry/src/index.js index ea01af793..dec066ac4 100755 --- a/packages/multi-entry/src/index.js +++ b/packages/multi-entry/src/index.js @@ -53,7 +53,7 @@ export default function multiEntry(conf = {}) { outputOptions(options) { return { ...options, - entryFileNames: config.entryFileName + entryFileNames: config.preserveModules ? options.entryFileNames : config.entryFileName }; }, diff --git a/packages/multi-entry/test/test.mjs b/packages/multi-entry/test/test.mjs index 5799db92f..9addd6dad 100755 --- a/packages/multi-entry/test/test.mjs +++ b/packages/multi-entry/test/test.mjs @@ -91,3 +91,43 @@ test('works as CJS plugin', async (t) => { const code = await getCode(bundle); t.truthy(code.includes('exports.zero = zero;')); }); + +test('maintains filename when preserveModules = true', async (t) => { + const bundle = await rollup({ + input: 'test/fixtures/{0,1}.js', + plugins: [multiEntry({ preserveModules: true, entryFileName: 'testing.js' })] + }); + + const files = await getCode(bundle, { format: 'cjs', preserveModules: true }, true); + + const nonVirtualFiles = files.filter(({ fileName }) => !fileName.includes('_virtual/')); + + t.is(nonVirtualFiles.length, 2); + + t.truthy(nonVirtualFiles.find(({ fileName }) => fileName === '0.js')); + t.truthy(nonVirtualFiles.find(({ fileName }) => fileName === '1.js')); +}); + +test('makes a bundle with entryFileName as the output.entryFileName when preserveModules = true and entryName is not set', async (t) => { + const bundle = await rollup({ + input: 'test/fixtures/{0,1}.js', + plugins: [multiEntry({ preserveModules: true })] + }); + + const files = await getCode( + bundle, + { + format: 'cjs', + preserveModules: true, + entryFileNames: (c) => `entry-${c.name}.js` + }, + true + ); + + const nonVirtualFiles = files.filter(({ fileName }) => !fileName.includes('_virtual/')); + + t.is(nonVirtualFiles.length, 2); + + t.truthy(nonVirtualFiles.find(({ fileName }) => fileName === 'entry-0.js')); + t.truthy(nonVirtualFiles.find(({ fileName }) => fileName === 'entry-1.js')); +}); diff --git a/packages/multi-entry/types/index.d.ts b/packages/multi-entry/types/index.d.ts index 3331b2e87..0d6c085c3 100644 --- a/packages/multi-entry/types/index.d.ts +++ b/packages/multi-entry/types/index.d.ts @@ -26,6 +26,13 @@ interface RollupMultiEntryOptions { * @default 'multi-entry.js' */ entryFileName?: string; + /** + * The preserveModules option is to be used in conjunction with output.preserveModules ({@link https://rollupjs.org/guide/en/#outputpreservemodules}). + * If `true`, overrides the `entryFileName` option to be output.entryFileNames. + * If `false`, the plugin will respect the `entryFileName` option. + * @default false + */ + preserveModules?: boolean; } /**