Skip to content

Commit b819a0f

Browse files
authored
refactor(multi-entry)!: virtualised & entryFileName (#520)
BREAKING CHANGES: Outputs a multi-entry file with different default name Refactored multi-entry to use plugin-virtual for resolving/loading the multiple entries into a single entry file. Some tidy-up and adding a new option, entryFileName, to override the default entry filename.
1 parent ec804d3 commit b819a0f

File tree

5 files changed

+86
-36
lines changed

5 files changed

+86
-36
lines changed

packages/multi-entry/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ _Note: `default` exports cannot be combined and exported by this plugin. Only na
1717

1818
## Requirements
1919

20-
This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v8.0.0+) and Rollup v1.20.0+.
20+
This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v8.3.0+) and Rollup v1.20.0+.
2121

2222
## Install
2323

@@ -73,6 +73,13 @@ Default: `true`
7373

7474
If `true`, instructs the plugin to export named exports to the bundle from all entries. If `false`, the plugin will not export any entry exports to the bundle. This can be useful when wanting to combine code from multiple entry files, but not necessarily to export each entry file's exports.
7575

76+
### `entryFileName`
77+
78+
Type: `String`<br>
79+
Default: `'multi-entry.js'`
80+
81+
`entryFileName` changes the name of the generated entry file. By default, it will override `outputOptions.entryFileNames` to be `'multi-entry.js'`.
82+
7683
## Supported Input Types
7784

7885
This plugin extends Rollup's `input` option to support multiple new value types, in addition to a `String` specifying a path to a file.

packages/multi-entry/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"rollup": "^1.20.0 || ^2.0.0"
4949
},
5050
"dependencies": {
51+
"@rollup/plugin-virtual": "^2.0.3",
5152
"matched": "^5.0.0"
5253
},
5354
"devDependencies": {

packages/multi-entry/src/index.js

Lines changed: 56 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,77 @@
1-
/* eslint-disable consistent-return, no-param-reassign */
1+
/* eslint-disable no-param-reassign */
22

3-
import matched from 'matched';
3+
import virtual from '@rollup/plugin-virtual';
4+
import { promise as matched } from 'matched';
45

5-
const entry = '\0rollup:plugin-multi-entry:entry-point';
6+
const DEFAULT_OUTPUT = 'multi-entry.js';
7+
const AS_IMPORT = 'import';
8+
const AS_EXPORT = 'export * from';
69

7-
export default function multiEntry(conf) {
8-
let include = [];
9-
let exclude = [];
10-
let exporter = (path) => `export * from ${JSON.stringify(path)};`;
10+
export default function multiEntry(conf = {}) {
11+
const config = {
12+
include: [],
13+
exclude: [],
14+
entryFileName: DEFAULT_OUTPUT,
15+
exports: true,
16+
...conf
17+
};
18+
19+
let prefix = config.exports === false ? AS_IMPORT : AS_EXPORT;
20+
const exporter = (path) => `${prefix} ${JSON.stringify(path)}`;
1121

12-
function configure(config) {
13-
if (typeof config === 'string') {
14-
include = [config];
15-
} else if (Array.isArray(config)) {
16-
include = config;
22+
const configure = (input) => {
23+
if (typeof input === 'string') {
24+
config.include = [input];
25+
} else if (Array.isArray(input)) {
26+
config.include = input;
1727
} else {
18-
include = config.include || [];
19-
exclude = config.exclude || [];
20-
if (config.exports === false) {
21-
exporter = (path) => `import ${JSON.stringify(path)};`;
28+
const { include = [], exclude = [], entryFileName = DEFAULT_OUTPUT, exports } = input;
29+
config.include = include;
30+
config.exclude = exclude;
31+
config.entryFileName = entryFileName;
32+
if (exports === false) {
33+
prefix = AS_IMPORT;
2234
}
2335
}
24-
}
36+
};
2537

26-
if (conf) {
27-
configure(conf);
28-
}
38+
let virtualisedEntry;
2939

3040
return {
41+
name: 'multi-entry',
42+
3143
options(options) {
32-
if (options.input && options.input !== entry) {
44+
if (options.input !== config.entryFileName) {
3345
configure(options.input);
3446
}
35-
options.input = entry;
47+
return {
48+
...options,
49+
input: config.entryFileName
50+
};
3651
},
3752

38-
resolveId(id) {
39-
if (id === entry) {
40-
return entry;
41-
}
53+
outputOptions(options) {
54+
return {
55+
...options,
56+
entryFileNames: config.entryFileName
57+
};
58+
},
59+
60+
buildStart(options) {
61+
const patterns = config.include.concat(config.exclude.map((pattern) => `!${pattern}`));
62+
const entries = patterns.length
63+
? matched(patterns, { realpath: true }).then((paths) => paths.map(exporter).join('\n'))
64+
: Promise.resolve('');
65+
66+
virtualisedEntry = virtual({ [options.input]: entries });
67+
},
68+
69+
resolveId(id, importer) {
70+
return virtualisedEntry && virtualisedEntry.resolveId(id, importer);
4271
},
4372

4473
load(id) {
45-
if (id === entry) {
46-
if (!include.length) {
47-
return Promise.resolve('');
48-
}
49-
const patterns = include.concat(exclude.map((pattern) => `!${pattern}`));
50-
return matched(patterns, { realpath: true }).then((paths) =>
51-
paths.map(exporter).join('\n')
52-
);
53-
}
74+
return virtualisedEntry && virtualisedEntry.load(id);
5475
}
5576
};
5677
}

packages/multi-entry/test/test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,12 @@ test('allows to prevent exporting', async (t) => {
7272
t.falsy(code.includes('zero'));
7373
t.falsy(code.includes('one'));
7474
});
75+
76+
test('makes a bundle with entryFileName as the filename', async (t) => {
77+
const bundle = await rollup({
78+
input: 'test/fixtures/{0,1}.js',
79+
plugins: [multiEntry({ entryFileName: 'testing.js' })]
80+
});
81+
const [result] = await getCode(bundle, { format: 'cjs' }, true);
82+
t.is(result.fileName, 'testing.js');
83+
});

pnpm-lock.yaml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)