-
Notifications
You must be signed in to change notification settings - Fork 12
/
rollup_runtime.js
214 lines (180 loc) · 5.47 KB
/
rollup_runtime.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
const colors = require("ansi-colors");
const prettyTime = require("pretty-hrtime");
const paths = require("./utils/paths");
const absolutePath = paths.absolutePath;
const path = require("path");
const batchWarnings = require("./utils/logging");
const handleError = require("./utils/handleError");
const relativeId = require("./utils/relativeId");
const debug = require("debug")("crafty:runner-rollup");
function buildConfiguration(crafty, taskName, bundle, warnings) {
const destination = path.join(
absolutePath(crafty.config.destination_js),
bundle.directory ? bundle.directory : "",
bundle.destination
);
const terserOptions = { ...crafty.config.terser };
delete terserOptions.sourceMap;
const config = {
input: {
input: bundle.source,
plugins: {
// eslint
json: {
plugin: require("@rollup/plugin-json"),
weight: 10
},
// babel
// typescript
replace: {
plugin: require("@rollup/plugin-replace"),
weight: 30,
options: {
preventAssignment: true,
values: {
"process.env.NODE_ENV": `"${crafty.getEnvironment()}"`
}
}
},
pnpResolve: {
plugin: require("rollup-plugin-pnp-resolve"),
weight: 35
},
resolve: {
plugin: require("@rollup/plugin-node-resolve"),
weight: 40,
options: {
browser: true
}
},
commonjs: {
plugin: require("@rollup/plugin-commonjs"),
weight: 50
},
terser: {
plugin: require("rollup-plugin-terser").terser,
weight: 100,
options: terserOptions
}
},
external: bundle.externals
},
output: {
file: destination,
format: bundle.format || "es",
sourcemap: true,
sourcemapFile: `${destination}.map`
}
};
// Apply preset configuration
crafty.getImplementations("rollup").forEach(preset => {
debug(`${preset.presetName}.rollup(Crafty, bundle, rollupConfig)`);
preset.rollup(crafty, bundle, config);
debug("preset executed");
});
// Order and Initialize plugins
config.input.plugins = Object.keys(config.input.plugins)
.map(key => config.input.plugins[key])
.sort((a, b) => {
const weightA = a.weight || 0;
const weightB = b.weight || 0;
if (weightA < weightB) {
return -1;
}
if (weightA > weightB) {
return 1;
}
return 0;
})
.map(plugin => {
if (plugin.init) {
return plugin.init(plugin);
}
return plugin.plugin.default
? plugin.plugin.default(plugin.options)
: plugin.plugin(plugin.options);
});
const onwarn = config.input.onwarn;
config.input.onwarn = onwarn
? warning => onwarn(warning, warnings.add)
: warnings.add;
debug(`Final configuration for '${taskName}':`, config);
return config;
}
function msToHrtime(duration) {
const hrtime = `${duration / 1000}`.split(".").map(parseFloat);
hrtime[1] = hrtime[1] * 1000000;
return hrtime;
}
/**
* Prepare execution for both watch and single run
*
* @param {Crafty} crafty The crafty instance
* @param {object} bundle The current bundle's configuration
* @returns {function(*=)} The gulp task
*/
module.exports = function jsTaskES6(crafty, bundle) {
const taskName = bundle.taskName;
debug(`Creating tasks for ${taskName}`);
const warnings = batchWarnings(taskName);
// This is executed in watch mode only
crafty.watcher.addRaw({
start: () => {
const rollup = require("rollup");
const config = buildConfiguration(crafty, taskName, bundle, warnings.add);
crafty.log(`Start watching with webpack in '${colors.cyan(taskName)}'`);
const watchOptions = {
...config.input,
output: config.output,
watch: config.watch
};
const watcher = rollup.watch(watchOptions);
watcher.on("event", event => {
switch (event.code) {
case "FATAL":
handleError(event.error, true);
throw event.error;
case "ERROR":
warnings.flush();
handleError(event.error, true);
break;
case "START":
crafty.log(`Watch ready for '${colors.cyan(taskName)}'`);
break;
case "BUNDLE_START":
crafty.log(`Starting '${colors.cyan(taskName)}' ...`);
break;
case "BUNDLE_END": {
const time = prettyTime(msToHrtime(event.duration));
crafty.log(
`Finished '${colors.cyan(taskName)}' after ${colors.magenta(
time
)}\n Wrote ${colors.bold(
event.output.map(relativeId).join(", ")
)}\n Waiting for changes...`
);
break;
}
case "END":
///crafty.log(`'${colors.cyan(taskName)}' is Waiting for changes...`);
}
});
}
});
crafty.undertaker.task(taskName, cb => {
const rollup = require("rollup");
const config = buildConfiguration(crafty, taskName, bundle, warnings.add);
function onError(e) {
warnings.add(e);
warnings.flush();
cb(e);
}
return rollup
.rollup(config.input)
.then(builtBundle => {
debug(`Rollup finished parsing files for '${taskName}'`);
return builtBundle.write(config.output);
}, onError)
.then(warnings.flush, onError);
});
};