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