Skip to content

Commit

Permalink
refactor(multi-entry)!: virtualised & entryFileName (#520)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Bluefinger committed Aug 4, 2020
1 parent ec804d3 commit b819a0f
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 36 deletions.
9 changes: 8 additions & 1 deletion packages/multi-entry/README.md
Expand Up @@ -17,7 +17,7 @@ _Note: `default` exports cannot be combined and exported by this plugin. Only na

## Requirements

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

## Install

Expand Down Expand Up @@ -73,6 +73,13 @@ Default: `true`

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.

### `entryFileName`

Type: `String`<br>
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'`.

## 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
1 change: 1 addition & 0 deletions packages/multi-entry/package.json
Expand Up @@ -48,6 +48,7 @@
"rollup": "^1.20.0 || ^2.0.0"
},
"dependencies": {
"@rollup/plugin-virtual": "^2.0.3",
"matched": "^5.0.0"
},
"devDependencies": {
Expand Down
91 changes: 56 additions & 35 deletions packages/multi-entry/src/index.js
@@ -1,56 +1,77 @@
/* eslint-disable consistent-return, no-param-reassign */
/* eslint-disable no-param-reassign */

import matched from 'matched';
import virtual from '@rollup/plugin-virtual';
import { promise as matched } from 'matched';

const entry = '\0rollup:plugin-multi-entry:entry-point';
const DEFAULT_OUTPUT = 'multi-entry.js';
const AS_IMPORT = 'import';
const AS_EXPORT = 'export * from';

export default function multiEntry(conf) {
let include = [];
let exclude = [];
let exporter = (path) => `export * from ${JSON.stringify(path)};`;
export default function multiEntry(conf = {}) {
const config = {
include: [],
exclude: [],
entryFileName: DEFAULT_OUTPUT,
exports: true,
...conf
};

let prefix = config.exports === false ? AS_IMPORT : AS_EXPORT;
const exporter = (path) => `${prefix} ${JSON.stringify(path)}`;

function configure(config) {
if (typeof config === 'string') {
include = [config];
} else if (Array.isArray(config)) {
include = config;
const configure = (input) => {
if (typeof input === 'string') {
config.include = [input];
} else if (Array.isArray(input)) {
config.include = input;
} else {
include = config.include || [];
exclude = config.exclude || [];
if (config.exports === false) {
exporter = (path) => `import ${JSON.stringify(path)};`;
const { include = [], exclude = [], entryFileName = DEFAULT_OUTPUT, exports } = input;
config.include = include;
config.exclude = exclude;
config.entryFileName = entryFileName;
if (exports === false) {
prefix = AS_IMPORT;
}
}
}
};

if (conf) {
configure(conf);
}
let virtualisedEntry;

return {
name: 'multi-entry',

options(options) {
if (options.input && options.input !== entry) {
if (options.input !== config.entryFileName) {
configure(options.input);
}
options.input = entry;
return {
...options,
input: config.entryFileName
};
},

resolveId(id) {
if (id === entry) {
return entry;
}
outputOptions(options) {
return {
...options,
entryFileNames: config.entryFileName
};
},

buildStart(options) {
const patterns = config.include.concat(config.exclude.map((pattern) => `!${pattern}`));
const entries = patterns.length
? matched(patterns, { realpath: true }).then((paths) => paths.map(exporter).join('\n'))
: Promise.resolve('');

virtualisedEntry = virtual({ [options.input]: entries });
},

resolveId(id, importer) {
return virtualisedEntry && virtualisedEntry.resolveId(id, importer);
},

load(id) {
if (id === entry) {
if (!include.length) {
return Promise.resolve('');
}
const patterns = include.concat(exclude.map((pattern) => `!${pattern}`));
return matched(patterns, { realpath: true }).then((paths) =>
paths.map(exporter).join('\n')
);
}
return virtualisedEntry && virtualisedEntry.load(id);
}
};
}
9 changes: 9 additions & 0 deletions packages/multi-entry/test/test.js
Expand Up @@ -72,3 +72,12 @@ test('allows to prevent exporting', async (t) => {
t.falsy(code.includes('zero'));
t.falsy(code.includes('one'));
});

test('makes a bundle with entryFileName as the filename', async (t) => {
const bundle = await rollup({
input: 'test/fixtures/{0,1}.js',
plugins: [multiEntry({ entryFileName: 'testing.js' })]
});
const [result] = await getCode(bundle, { format: 'cjs' }, true);
t.is(result.fileName, 'testing.js');
});
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b819a0f

Please sign in to comment.