forked from rollup/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
65 lines (51 loc) · 1.76 KB
/
index.ts
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
import { ChildProcess, fork } from 'child_process';
import * as path from 'path';
import * as fs from 'fs';
import { Plugin, RenderedChunk } from 'rollup';
import { RollupRunOptions } from '../types';
export default function run(opts: RollupRunOptions = {}): Plugin {
let input: string;
let proc: ChildProcess;
const args = opts.args || [];
const forkOptions = opts.options || opts;
delete (forkOptions as RollupRunOptions).args;
return {
name: 'run',
buildStart(options) {
let inputs = options.input!;
if (typeof inputs === 'string') {
inputs = [inputs];
}
if (typeof inputs === 'object') {
inputs = Object.values(inputs);
}
if (inputs.length > 1) {
throw new Error(`@rollup/plugin-run only works with a single entry point`);
}
// eslint-disable-next-line prefer-destructuring
input = inputs[0];
const resolvedInputPath = path.resolve(input);
if (fs.existsSync(resolvedInputPath)) {
input = resolvedInputPath;
}
},
generateBundle(_outputOptions, _bundle, isWrite) {
if (!isWrite) {
this.error(`@rollup/plugin-run currently only works with bundles that are written to disk`);
}
},
writeBundle(outputOptions, bundle) {
const dir = outputOptions.dir || path.dirname(outputOptions.file!);
const entryFileName = Object.keys(bundle).find((fileName) => {
const chunk = bundle[fileName] as RenderedChunk;
return chunk.isEntry && chunk.facadeModuleId === input;
});
if (entryFileName) {
if (proc) proc.kill();
proc = fork(path.join(dir, entryFileName), args, forkOptions);
} else {
this.error(`@rollup/plugin-run could not find output chunk`);
}
}
};
}