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