This repository has been archived by the owner on Jul 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollupjs.ts
98 lines (85 loc) · 2.87 KB
/
rollupjs.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
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
import * as rollup from 'rollup';
import * as babel from 'rollup-plugin-babel';
import * as resolve from 'rollup-plugin-node-resolve';
import * as builtins from 'rollup-plugin-node-builtins';
import * as globals from 'rollup-plugin-node-globals';
import * as commonjs from 'rollup-plugin-commonjs';
import * as json from 'rollup-plugin-json';
import * as replace from 'rollup-plugin-replace';
import { Utils } from '~/utils';
import { Processor } from '~/processors/processor';
import { ProcessResult } from '~/models/processor-models';
const DEFAULT_OPTS = {
// Turn off plugins
plugins: {
babel: true,
nodeResolve: true,
nodeBuiltins: true,
nodeGlobals: true,
commonJS: true,
json: true,
replace: true,
},
// rollup.js
rollupJS: {
output: {
format: 'iife',
name: 'LastStepDummyName'
}
},
babel: {
babelrc: false,
presets: [['env', { modules: false }]],
plugins: ['external-helpers']
},
// rollup-plugin-node-resolve
nodeResolve: {
browser: true
},
// rollup-plugin-commonjs
commonJS: {
},
// rollup-plugin-json
json: {
},
// rollup-plugin-replace
replace: {
'process.env.NODE_ENV': JSON.stringify('production')
},
};
export class RollupJSProcessor extends Processor {
private options: any;
constructor(options: any = {}) {
super();
options = options || {};
let pluginToggles = Utils.shallowMerge(DEFAULT_OPTS.plugins || options.plugins);
let plugins = [];
if (pluginToggles.replace)
plugins = plugins.concat(replace(Utils.shallowMerge(DEFAULT_OPTS.replace, options.replace)));
if (pluginToggles.nodeGlobals)
plugins = plugins.concat(globals());
if (pluginToggles.nodeBuiltins)
plugins = plugins.concat(builtins());
if (pluginToggles.json)
plugins = plugins.concat(json(Utils.shallowMerge(DEFAULT_OPTS.json, options.json)));
if (pluginToggles.nodeResolve)
plugins = plugins.concat(resolve(Utils.shallowMerge(DEFAULT_OPTS.nodeResolve, options.nodeResolve)));
if (pluginToggles.babel)
plugins = plugins.concat(babel(Utils.shallowMerge(DEFAULT_OPTS.babel, options.babel)));
if (pluginToggles.commonJS)
plugins = plugins.concat(commonjs(Utils.shallowMerge(DEFAULT_OPTS.commonJS, options.commonJS)));
this.options = Utils.shallowMerge(DEFAULT_OPTS.rollupJS, options.rollupJS);
this.options.plugins = plugins.concat(this.options.plugins || []);
Utils.dbg() && Utils.debug('Options:', this.options);
}
async processFile(source: string, target: string): Promise<ProcessResult> {
let options = Utils.shallowMerge(this.options, {
input: source,
file: target
});
let bundle = await rollup.rollup(options);
// Local imports by this file
let deps = bundle.modules.reduce((i, m) => i.concat(m.dependencies), []);
return bundle.write(options).then(() => new ProcessResult(deps));
}
}