-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
138 lines (108 loc) · 3.35 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import path from 'path';
import {PassThrough} from 'stream';
import {rollup} from 'rollup';
import vfs from 'vinyl-fs';
import Vinyl from 'vinyl';
import merge2 from 'merge2';
function bundleModuleNames(modules, modulePath) {
const webSet = new Set();
for (const source of modules) {
const id = path.relative(modulePath, source);
if (!(id.startsWith('.') || id.startsWith('node_modules'))) {
const webmod = id.split(path.sep, id[0] === '@' ? 2 : 1).join(path.sep);
webSet.add(webmod);
}
}
return [...webSet];
}
function processOutput(output, input) {
if (typeof output === 'undefined') {
return [{
file: input,
format: 'es'
}];
}
const outputList = new Set();
return [].concat(output).map(entry => {
entry = {
file: input,
...entry
};
if (typeof entry.file !== 'string') {
throw new TypeError('output.file must be a string.');
}
if (outputList.has(entry.file)) {
throw new TypeError(`Cannot output to the same file multiple times: ${entry.file}`);
}
outputList.add(entry.file);
return entry;
});
}
async function runBundle(bundle, entry) {
const rollupConfig = {...entry};
delete rollupConfig.vinylOptions;
const {output} = await bundle.generate(rollupConfig);
const {code, map} = output[0];
const vinylOptions = {
...(entry.vinylOptions || {}),
path: entry.file,
contents: Buffer.from(code)
};
if (map) {
vinylOptions.sourceMap = map;
}
return new Vinyl(vinylOptions);
}
async function runRollup(options, merged) {
const {input} = options.rollup;
if (typeof input !== 'string') {
throw new TypeError('options.rollup.input must be a string');
}
const output = processOutput(options.rollup.output, input);
if (!['undefined', 'string'].includes(typeof options.modulePath)) {
throw new TypeError('options.modulePath must be a string if defined');
}
if (!['undefined', 'boolean'].includes(typeof options.copyModules)) {
throw new TypeError('options.copyModules must be a boolean if defined');
}
const bundle = await rollup(options.rollup);
const results = [];
for (const entry of output) {
// eslint-disable-next-line no-await-in-loop
results.push(await runBundle(bundle, entry));
}
if (options.copyModules !== false) {
const modulePath = path.resolve(options.modulePath || 'node_modules');
const modules = bundleModuleNames(bundle.watchFiles, modulePath);
const base = '.';
if (options.copyModules === true) {
for (const module of modules) {
const globs = [
path.join(modulePath, module, '**'),
`!${path.join(modulePath, module, 'node_modules/**')}`
];
merged.add(vfs.src(globs, {base, nodir: true}));
}
} else if (modules.length > 0) {
/* Include package.json and LICENSE files by default. */
const casedGlobs = modules.map(module => path.join(modulePath, module, 'package.json'));
const uncasedGlobs = modules.map(module => path.join(modulePath, module, 'licen{s,c}e*'));
merged.add(vfs.src(casedGlobs, {base, nodir: true}));
merged.add(vfs.src(uncasedGlobs, {base, nocase: true}));
}
}
const rollupStream = new PassThrough({objectMode: true});
merged.add(rollupStream);
for (const vinyl of results) {
rollupStream.write(vinyl);
}
rollupStream.end();
}
function vinylRollup(options) {
const merged = merge2();
runRollup(options, merged).catch(error => {
merged.emit('error', error);
});
return merged;
}
export default vinylRollup;