UNPKG

12.6 kBJavaScriptView Raw
1/*
2 @license
3 Rollup.js v2.29.0
4 Thu, 08 Oct 2020 04:24:04 GMT - commit 0b02e52bc7816c473784794670a2c3047ac62a07
5
6
7 https://github.com/rollup/rollup
8
9 Released under the MIT License.
10*/
11'use strict';
12
13var cli = require('../bin/rollup');
14var rollup = require('./rollup.js');
15require('util');
16var fs = require('fs');
17require('path');
18require('./mergeOptions.js');
19var loadConfigFile_js = require('./loadConfigFile.js');
20require('crypto');
21var require$$0 = require('events');
22require('module');
23require('url');
24var index = require('./index.js');
25require('stream');
26require('os');
27var assert = require('assert');
28
29var 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
39const 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 // Offset the date so it will return the correct value when getting the ISO string
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
70var dateTime_1 = dateTime;
71// TODO: Remove this for the next major release
72var _default = dateTime;
73dateTime_1.default = _default;
74
75var signals = rollup.createCommonjsModule(function (module) {
76// This is not the set of all possible signals.
77//
78// It IS, however, the set of all signals that trigger
79// an exit on either Linux or BSD systems. Linux is a
80// superset of the signal names supported on BSD, and
81// the unknown signals just fail to register, so we can
82// catch that easily enough.
83//
84// Don't bother with SIGKILL. It's uncatchable, which
85// means that we can't fire any callbacks anyway.
86//
87// If a user does happen to register a handler on a non-
88// fatal signal like SIGWINCH or something, and then
89// exit, it'll end up firing `process.emit('exit')`, so
90// the handler will be fired anyway.
91//
92// SIGBUS, SIGFPE, SIGSEGV and SIGILL, when not raised
93// artificially, inherently leave the process in a
94// state from which it is not safe to try and enter JS
95// listeners.
96module.exports = [
97 'SIGABRT',
98 'SIGALRM',
99 'SIGHUP',
100 'SIGINT',
101 'SIGTERM'
102];
103
104if (process.platform !== 'win32') {
105 module.exports.push(
106 'SIGVTALRM',
107 'SIGXCPU',
108 'SIGXFSZ',
109 'SIGUSR2',
110 'SIGTRAP',
111 'SIGSYS',
112 'SIGQUIT',
113 'SIGIOT'
114 // should detect profiler and enable/disable accordingly.
115 // see #21
116 // 'SIGPROF'
117 );
118}
119
120if (process.platform === 'linux') {
121 module.exports.push(
122 'SIGIO',
123 'SIGPOLL',
124 'SIGPWR',
125 'SIGSTKFLT',
126 'SIGUNUSED'
127 );
128}
129});
130
131// Note: since nyc uses this module to output coverage, any lines
132// that are in the direct sync flow of nyc's outputCoverage are
133// ignored, since we can never get coverage for them.
134
135var signals$1 = signals;
136var isWin = /^win/i.test(process.platform);
137
138var EE = require$$0;
139/* istanbul ignore if */
140if (typeof EE !== 'function') {
141 EE = EE.EventEmitter;
142}
143
144var emitter;
145if (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// Because this emitter is a global, we have to check to see if a
154// previous version of this library failed to enable infinite listeners.
155// I know what you're about to say. But literally everything about
156// signal-exit is a compromise with evil. Get used to it.
157if (!emitter.infinite) {
158 emitter.setMaxListeners(Infinity);
159 emitter.infinite = true;
160}
161
162var 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
186var unload_1 = unload;
187function 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
203function 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// { <signal>: <listener fn>, ... }
212var sigListeners = {};
213signals$1.forEach(function (sig) {
214 sigListeners[sig] = function listener () {
215 // If there are no other listeners, an exit is coming!
216 // Simplest way: remove us and then re-send the signal.
217 // We know that this will kill the process, so we can
218 // safely emit now.
219 var listeners = process.listeners(sig);
220 if (listeners.length === emitter.count) {
221 unload();
222 emit('exit', null, sig);
223 /* istanbul ignore next */
224 emit('afterexit', null, sig);
225 /* istanbul ignore next */
226 if (isWin && sig === 'SIGHUP') {
227 // "SIGHUP" throws an `ENOSYS` error on Windows,
228 // so use a supported signal instead
229 sig = 'SIGINT';
230 }
231 process.kill(process.pid, sig);
232 }
233 };
234});
235
236var signals_1 = function () {
237 return signals$1
238};
239
240var load_1 = load;
241
242var loaded = false;
243
244function load () {
245 if (loaded) {
246 return
247 }
248 loaded = true;
249
250 // This is the number of onSignalExit's that are in play.
251 // It's important so that we can count the correct number of
252 // listeners on signals, and don't wait for the other one to
253 // handle it instead of us.
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
269var originalProcessReallyExit = process.reallyExit;
270function processReallyExit (code) {
271 process.exitCode = code || 0;
272 emit('exit', process.exitCode, null);
273 /* istanbul ignore next */
274 emit('afterexit', process.exitCode, null);
275 /* istanbul ignore next */
276 originalProcessReallyExit.call(process, process.exitCode);
277}
278
279var originalProcessEmit = process.emit;
280function 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 /* istanbul ignore next */
288 emit('afterexit', process.exitCode, null);
289 return ret
290 } else {
291 return originalProcessEmit.apply(this, arguments)
292 }
293}
294signalExit.unload = unload_1;
295signalExit.signals = signals_1;
296signalExit.load = load_1;
297
298const CLEAR_SCREEN = '\u001Bc';
299function 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
318async 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 // tslint:disable-next-line:no-unnecessary-type-assertion
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 // removing a non-existent listener is a no-op
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
440exports.watch = watch;
441//# sourceMappingURL=watch-cli.js.map