Skip to content

Commit

Permalink
feat(multi-entry): add preserveModules option (#1237)
Browse files Browse the repository at this point in the history
* Update functionality to maintain file output names when preserveModule = true

BREAKING CHANGES: Changes the default behaviour to not alter filenames
if preserveModules=true

* Fix docstring

* Tests

* Fix type doc

* Swap to use preserveModules config option instead

* Update readme and type docstrings

* Fix tests

* Revert odd changes in diff

* Improve test

* Fix tests

Co-authored-by: Blake Lovelace <[email protected]>
  • Loading branch information
814k31 and Blake Lovelace authored Oct 24, 2022
1 parent 9b231e7 commit 805339c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/multi-entry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`<br>
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.
Expand Down
2 changes: 1 addition & 1 deletion packages/multi-entry/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default function multiEntry(conf = {}) {
outputOptions(options) {
return {
...options,
entryFileNames: config.entryFileName
entryFileNames: config.preserveModules ? options.entryFileNames : config.entryFileName
};
},

Expand Down
40 changes: 40 additions & 0 deletions packages/multi-entry/test/test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
});
7 changes: 7 additions & 0 deletions packages/multi-entry/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 805339c

Please sign in to comment.