-
-
Notifications
You must be signed in to change notification settings - Fork 586
/
index.js
executable file
·77 lines (64 loc) · 1.96 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* eslint-disable no-param-reassign */
import virtual from '@rollup/plugin-virtual';
import { promise as matched } from 'matched';
const DEFAULT_OUTPUT = 'multi-entry.js';
const AS_IMPORT = 'import';
const AS_EXPORT = 'export * from';
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)}`;
const configure = (input) => {
if (typeof input === 'string') {
config.include = [input];
} else if (Array.isArray(input)) {
config.include = input;
} else {
const { include = [], exclude = [], entryFileName = DEFAULT_OUTPUT, exports } = input;
config.include = include;
config.exclude = exclude;
config.entryFileName = entryFileName;
if (exports === false) {
prefix = AS_IMPORT;
}
}
};
let virtualisedEntry;
return {
name: 'multi-entry',
options(options) {
if (options.input !== config.entryFileName) {
configure(options.input);
}
return {
...options,
input: config.entryFileName
};
},
outputOptions(options) {
return {
...options,
entryFileNames: config.preserveModules ? 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) {
return virtualisedEntry && virtualisedEntry.load(id);
}
};
}