1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | 'use strict';
|
12 |
|
13 | const fs = require('fs');
|
14 | const index = require('./index.js');
|
15 | const cli = require('../bin/rollup');
|
16 | const rollup = require('./rollup.js');
|
17 | const require$$0 = require('assert');
|
18 | const require$$0$1 = require('events');
|
19 | const loadConfigFile_js = require('./loadConfigFile.js');
|
20 | require('path');
|
21 | require('util');
|
22 | require('stream');
|
23 | require('os');
|
24 | require('./mergeOptions.js');
|
25 | require('module');
|
26 | require('crypto');
|
27 | require('url');
|
28 | require('process');
|
29 | require('tty');
|
30 |
|
31 | function timeZone(date = new Date()) {
|
32 | const offset = date.getTimezoneOffset();
|
33 | const absOffset = Math.abs(offset);
|
34 | const hours = Math.floor(absOffset / 60);
|
35 | const minutes = absOffset % 60;
|
36 | const minutesOut = minutes > 0 ? ':' + ('0' + minutes).slice(-2) : '';
|
37 | return (offset < 0 ? '+' : '-') + hours + minutesOut;
|
38 | }
|
39 |
|
40 | function dateTime(options = {}) {
|
41 | let {
|
42 | date = new Date(),
|
43 | local = true,
|
44 | showTimeZone = false,
|
45 | showMilliseconds = false
|
46 | } = options;
|
47 |
|
48 | if (local) {
|
49 |
|
50 | date = new Date(date.getTime() - (date.getTimezoneOffset() * 60000));
|
51 | }
|
52 |
|
53 | let end = '';
|
54 |
|
55 | if (showTimeZone) {
|
56 | end = ' UTC' + (local ? timeZone(date) : '');
|
57 | }
|
58 |
|
59 | if (showMilliseconds && date.getUTCMilliseconds() > 0) {
|
60 | end = ` ${date.getUTCMilliseconds()}ms${end}`;
|
61 | }
|
62 |
|
63 | return date
|
64 | .toISOString()
|
65 | .replace(/T/, ' ')
|
66 | .replace(/\..+/, end);
|
67 | }
|
68 |
|
69 | var signalExit = {exports: {}};
|
70 |
|
71 | var signals$1 = {exports: {}};
|
72 |
|
73 | (function (module) {
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | module.exports = [
|
95 | 'SIGABRT',
|
96 | 'SIGALRM',
|
97 | 'SIGHUP',
|
98 | 'SIGINT',
|
99 | 'SIGTERM'
|
100 | ];
|
101 |
|
102 | if (process.platform !== 'win32') {
|
103 | module.exports.push(
|
104 | 'SIGVTALRM',
|
105 | 'SIGXCPU',
|
106 | 'SIGXFSZ',
|
107 | 'SIGUSR2',
|
108 | 'SIGTRAP',
|
109 | 'SIGSYS',
|
110 | 'SIGQUIT',
|
111 | 'SIGIOT'
|
112 |
|
113 |
|
114 |
|
115 | );
|
116 | }
|
117 |
|
118 | if (process.platform === 'linux') {
|
119 | module.exports.push(
|
120 | 'SIGIO',
|
121 | 'SIGPOLL',
|
122 | 'SIGPWR',
|
123 | 'SIGSTKFLT',
|
124 | 'SIGUNUSED'
|
125 | );
|
126 | }
|
127 | }(signals$1));
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | var process$1 = rollup.commonjsGlobal.process;
|
134 |
|
135 | const processOk = function (process) {
|
136 | return process &&
|
137 | typeof process === 'object' &&
|
138 | typeof process.removeListener === 'function' &&
|
139 | typeof process.emit === 'function' &&
|
140 | typeof process.reallyExit === 'function' &&
|
141 | typeof process.listeners === 'function' &&
|
142 | typeof process.kill === 'function' &&
|
143 | typeof process.pid === 'number' &&
|
144 | typeof process.on === 'function'
|
145 | };
|
146 |
|
147 |
|
148 |
|
149 | if (!processOk(process$1)) {
|
150 | signalExit.exports = function () {};
|
151 | } else {
|
152 | var assert = require$$0;
|
153 | var signals = signals$1.exports;
|
154 | var isWin = /^win/i.test(process$1.platform);
|
155 |
|
156 | var EE = require$$0$1;
|
157 |
|
158 | if (typeof EE !== 'function') {
|
159 | EE = EE.EventEmitter;
|
160 | }
|
161 |
|
162 | var emitter;
|
163 | if (process$1.__signal_exit_emitter__) {
|
164 | emitter = process$1.__signal_exit_emitter__;
|
165 | } else {
|
166 | emitter = process$1.__signal_exit_emitter__ = new EE();
|
167 | emitter.count = 0;
|
168 | emitter.emitted = {};
|
169 | }
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | if (!emitter.infinite) {
|
176 | emitter.setMaxListeners(Infinity);
|
177 | emitter.infinite = true;
|
178 | }
|
179 |
|
180 | signalExit.exports = function (cb, opts) {
|
181 |
|
182 | if (!processOk(rollup.commonjsGlobal.process)) {
|
183 | return
|
184 | }
|
185 | assert.equal(typeof cb, 'function', 'a callback must be provided for exit handler');
|
186 |
|
187 | if (loaded === false) {
|
188 | load();
|
189 | }
|
190 |
|
191 | var ev = 'exit';
|
192 | if (opts && opts.alwaysLast) {
|
193 | ev = 'afterexit';
|
194 | }
|
195 |
|
196 | var remove = function () {
|
197 | emitter.removeListener(ev, cb);
|
198 | if (emitter.listeners('exit').length === 0 &&
|
199 | emitter.listeners('afterexit').length === 0) {
|
200 | unload();
|
201 | }
|
202 | };
|
203 | emitter.on(ev, cb);
|
204 |
|
205 | return remove
|
206 | };
|
207 |
|
208 | var unload = function unload () {
|
209 | if (!loaded || !processOk(rollup.commonjsGlobal.process)) {
|
210 | return
|
211 | }
|
212 | loaded = false;
|
213 |
|
214 | signals.forEach(function (sig) {
|
215 | try {
|
216 | process$1.removeListener(sig, sigListeners[sig]);
|
217 | } catch (er) {}
|
218 | });
|
219 | process$1.emit = originalProcessEmit;
|
220 | process$1.reallyExit = originalProcessReallyExit;
|
221 | emitter.count -= 1;
|
222 | };
|
223 | signalExit.exports.unload = unload;
|
224 |
|
225 | var emit = function emit (event, code, signal) {
|
226 |
|
227 | if (emitter.emitted[event]) {
|
228 | return
|
229 | }
|
230 | emitter.emitted[event] = true;
|
231 | emitter.emit(event, code, signal);
|
232 | };
|
233 |
|
234 |
|
235 | var sigListeners = {};
|
236 | signals.forEach(function (sig) {
|
237 | sigListeners[sig] = function listener () {
|
238 |
|
239 | if (!processOk(rollup.commonjsGlobal.process)) {
|
240 | return
|
241 | }
|
242 |
|
243 |
|
244 |
|
245 |
|
246 | var listeners = process$1.listeners(sig);
|
247 | if (listeners.length === emitter.count) {
|
248 | unload();
|
249 | emit('exit', null, sig);
|
250 |
|
251 | emit('afterexit', null, sig);
|
252 |
|
253 | if (isWin && sig === 'SIGHUP') {
|
254 |
|
255 |
|
256 | sig = 'SIGINT';
|
257 | }
|
258 |
|
259 | process$1.kill(process$1.pid, sig);
|
260 | }
|
261 | };
|
262 | });
|
263 |
|
264 | signalExit.exports.signals = function () {
|
265 | return signals
|
266 | };
|
267 |
|
268 | var loaded = false;
|
269 |
|
270 | var load = function load () {
|
271 | if (loaded || !processOk(rollup.commonjsGlobal.process)) {
|
272 | return
|
273 | }
|
274 | loaded = true;
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 | emitter.count += 1;
|
281 |
|
282 | signals = signals.filter(function (sig) {
|
283 | try {
|
284 | process$1.on(sig, sigListeners[sig]);
|
285 | return true
|
286 | } catch (er) {
|
287 | return false
|
288 | }
|
289 | });
|
290 |
|
291 | process$1.emit = processEmit;
|
292 | process$1.reallyExit = processReallyExit;
|
293 | };
|
294 | signalExit.exports.load = load;
|
295 |
|
296 | var originalProcessReallyExit = process$1.reallyExit;
|
297 | var processReallyExit = function processReallyExit (code) {
|
298 |
|
299 | if (!processOk(rollup.commonjsGlobal.process)) {
|
300 | return
|
301 | }
|
302 | process$1.exitCode = code || 0;
|
303 | emit('exit', process$1.exitCode, null);
|
304 |
|
305 | emit('afterexit', process$1.exitCode, null);
|
306 |
|
307 | originalProcessReallyExit.call(process$1, process$1.exitCode);
|
308 | };
|
309 |
|
310 | var originalProcessEmit = process$1.emit;
|
311 | var processEmit = function processEmit (ev, arg) {
|
312 | if (ev === 'exit' && processOk(rollup.commonjsGlobal.process)) {
|
313 |
|
314 | if (arg !== undefined) {
|
315 | process$1.exitCode = arg;
|
316 | }
|
317 | var ret = originalProcessEmit.apply(this, arguments);
|
318 |
|
319 | emit('exit', process$1.exitCode, null);
|
320 |
|
321 | emit('afterexit', process$1.exitCode, null);
|
322 |
|
323 | return ret
|
324 | } else {
|
325 | return originalProcessEmit.apply(this, arguments)
|
326 | }
|
327 | };
|
328 | }
|
329 |
|
330 | const onExit = signalExit.exports;
|
331 |
|
332 | const CLEAR_SCREEN = '\u001Bc';
|
333 | function getResetScreen(configs, allowClearScreen) {
|
334 | let clearScreen = allowClearScreen;
|
335 | for (const config of configs) {
|
336 | if (config.watch && config.watch.clearScreen === false) {
|
337 | clearScreen = false;
|
338 | }
|
339 | }
|
340 | if (clearScreen) {
|
341 | return (heading) => loadConfigFile_js.stderr(CLEAR_SCREEN + heading);
|
342 | }
|
343 | let firstRun = true;
|
344 | return (heading) => {
|
345 | if (firstRun) {
|
346 | loadConfigFile_js.stderr(heading);
|
347 | firstRun = false;
|
348 | }
|
349 | };
|
350 | }
|
351 |
|
352 | async function watch(command) {
|
353 | process.env.ROLLUP_WATCH = 'true';
|
354 | const isTTY = process.stderr.isTTY;
|
355 | const silent = command.silent;
|
356 | let configs;
|
357 | let warnings;
|
358 | let watcher;
|
359 | let configWatcher;
|
360 | const configFile = command.config ? cli.getConfigPath(command.config) : null;
|
361 | onExit(close);
|
362 | process.on('uncaughtException', close);
|
363 | if (!process.stdin.isTTY) {
|
364 | process.stdin.on('end', close);
|
365 | process.stdin.resume();
|
366 | }
|
367 | async function loadConfigFromFileAndTrack(configFile) {
|
368 | let reloadingConfig = false;
|
369 | let aborted = false;
|
370 | let configFileData = null;
|
371 | configWatcher = index.chokidar.watch(configFile).on('change', () => reloadConfigFile());
|
372 | await reloadConfigFile();
|
373 | async function reloadConfigFile() {
|
374 | try {
|
375 | const newConfigFileData = fs.readFileSync(configFile, 'utf-8');
|
376 | if (newConfigFileData === configFileData) {
|
377 | return;
|
378 | }
|
379 | if (reloadingConfig) {
|
380 | aborted = true;
|
381 | return;
|
382 | }
|
383 | if (configFileData) {
|
384 | loadConfigFile_js.stderr(`\nReloading updated config...`);
|
385 | }
|
386 | configFileData = newConfigFileData;
|
387 | reloadingConfig = true;
|
388 | ({ options: configs, warnings } = await loadConfigFile_js.loadAndParseConfigFile(configFile, command));
|
389 | reloadingConfig = false;
|
390 | if (aborted) {
|
391 | aborted = false;
|
392 | reloadConfigFile();
|
393 | }
|
394 | else {
|
395 | if (watcher) {
|
396 | watcher.close();
|
397 | }
|
398 | start(configs);
|
399 | }
|
400 | }
|
401 | catch (err) {
|
402 | configs = [];
|
403 | reloadingConfig = false;
|
404 | loadConfigFile_js.handleError(err, true);
|
405 | }
|
406 | }
|
407 | }
|
408 | if (configFile) {
|
409 | await loadConfigFromFileAndTrack(configFile);
|
410 | }
|
411 | else {
|
412 | ({ options: configs, warnings } = await cli.loadConfigFromCommand(command));
|
413 | start(configs);
|
414 | }
|
415 | const resetScreen = getResetScreen(configs, isTTY);
|
416 | function start(configs) {
|
417 | try {
|
418 | watcher = rollup.watch(configs);
|
419 | }
|
420 | catch (err) {
|
421 | return loadConfigFile_js.handleError(err);
|
422 | }
|
423 | watcher.on('event', event => {
|
424 | switch (event.code) {
|
425 | case 'ERROR':
|
426 | warnings.flush();
|
427 | loadConfigFile_js.handleError(event.error, true);
|
428 | break;
|
429 | case 'START':
|
430 | if (!silent) {
|
431 | resetScreen(loadConfigFile_js.underline(`rollup v${rollup.version}`));
|
432 | }
|
433 | break;
|
434 | case 'BUNDLE_START':
|
435 | if (!silent) {
|
436 | let input = event.input;
|
437 | if (typeof input !== 'string') {
|
438 | input = Array.isArray(input)
|
439 | ? input.join(', ')
|
440 | : Object.values(input).join(', ');
|
441 | }
|
442 | loadConfigFile_js.stderr(loadConfigFile_js.cyan(`bundles ${loadConfigFile_js.bold(input)} → ${loadConfigFile_js.bold(event.output.map(rollup.relativeId).join(', '))}...`));
|
443 | }
|
444 | break;
|
445 | case 'BUNDLE_END':
|
446 | warnings.flush();
|
447 | if (!silent)
|
448 | loadConfigFile_js.stderr(loadConfigFile_js.green(`created ${loadConfigFile_js.bold(event.output.map(rollup.relativeId).join(', '))} in ${loadConfigFile_js.bold(cli.ms(event.duration))}`));
|
449 | if (event.result && event.result.getTimings) {
|
450 | cli.printTimings(event.result.getTimings());
|
451 | }
|
452 | break;
|
453 | case 'END':
|
454 | if (!silent && isTTY) {
|
455 | loadConfigFile_js.stderr(`\n[${dateTime()}] waiting for changes...`);
|
456 | }
|
457 | }
|
458 | if ('result' in event && event.result) {
|
459 | event.result.close().catch(error => loadConfigFile_js.handleError(error, true));
|
460 | }
|
461 | });
|
462 | }
|
463 | function close(code) {
|
464 | process.removeListener('uncaughtException', close);
|
465 |
|
466 | process.stdin.removeListener('end', close);
|
467 | if (watcher)
|
468 | watcher.close();
|
469 | if (configWatcher)
|
470 | configWatcher.close();
|
471 | if (code) {
|
472 | process.exit(code);
|
473 | }
|
474 | }
|
475 | }
|
476 |
|
477 | exports.watch = watch;
|
478 |
|