UNPKG

14.3 kBJavaScriptView Raw
1/*
2 @license
3 Rollup.js v2.62.1
4 Sun, 13 Feb 2022 23:06:06 GMT - commit 159f1d7f9506bdd1266d9324efd48b2ec30f5f22
5
6
7 https://github.com/rollup/rollup
8
9 Released under the MIT License.
10*/
11'use strict';
12
13const fs = require('fs');
14const index = require('./index.js');
15const cli = require('../bin/rollup');
16const rollup = require('./rollup.js');
17const require$$0 = require('assert');
18const require$$0$1 = require('events');
19const loadConfigFile_js = require('./loadConfigFile.js');
20require('path');
21require('util');
22require('stream');
23require('os');
24require('./mergeOptions.js');
25require('module');
26require('crypto');
27require('url');
28require('process');
29require('tty');
30
31function 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
40function dateTime(options = {}) {
41 let {
42 date = new Date(),
43 local = true,
44 showTimeZone = false,
45 showMilliseconds = false
46 } = options;
47
48 if (local) {
49 // Offset the date so it will return the correct value when getting the ISO string.
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
69var signalExit = {exports: {}};
70
71var signals$1 = {exports: {}};
72
73(function (module) {
74// This is not the set of all possible signals.
75//
76// It IS, however, the set of all signals that trigger
77// an exit on either Linux or BSD systems. Linux is a
78// superset of the signal names supported on BSD, and
79// the unknown signals just fail to register, so we can
80// catch that easily enough.
81//
82// Don't bother with SIGKILL. It's uncatchable, which
83// means that we can't fire any callbacks anyway.
84//
85// If a user does happen to register a handler on a non-
86// fatal signal like SIGWINCH or something, and then
87// exit, it'll end up firing `process.emit('exit')`, so
88// the handler will be fired anyway.
89//
90// SIGBUS, SIGFPE, SIGSEGV and SIGILL, when not raised
91// artificially, inherently leave the process in a
92// state from which it is not safe to try and enter JS
93// listeners.
94module.exports = [
95 'SIGABRT',
96 'SIGALRM',
97 'SIGHUP',
98 'SIGINT',
99 'SIGTERM'
100];
101
102if (process.platform !== 'win32') {
103 module.exports.push(
104 'SIGVTALRM',
105 'SIGXCPU',
106 'SIGXFSZ',
107 'SIGUSR2',
108 'SIGTRAP',
109 'SIGSYS',
110 'SIGQUIT',
111 'SIGIOT'
112 // should detect profiler and enable/disable accordingly.
113 // see #21
114 // 'SIGPROF'
115 );
116}
117
118if (process.platform === 'linux') {
119 module.exports.push(
120 'SIGIO',
121 'SIGPOLL',
122 'SIGPWR',
123 'SIGSTKFLT',
124 'SIGUNUSED'
125 );
126}
127}(signals$1));
128
129// Note: since nyc uses this module to output coverage, any lines
130// that are in the direct sync flow of nyc's outputCoverage are
131// ignored, since we can never get coverage for them.
132// grab a reference to node's real process object right away
133var process$1 = rollup.commonjsGlobal.process;
134
135const 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// some kind of non-node environment, just no-op
148/* istanbul ignore if */
149if (!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 /* istanbul ignore if */
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 // Because this emitter is a global, we have to check to see if a
172 // previous version of this library failed to enable infinite listeners.
173 // I know what you're about to say. But literally everything about
174 // signal-exit is a compromise with evil. Get used to it.
175 if (!emitter.infinite) {
176 emitter.setMaxListeners(Infinity);
177 emitter.infinite = true;
178 }
179
180 signalExit.exports = function (cb, opts) {
181 /* istanbul ignore if */
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 /* istanbul ignore if */
227 if (emitter.emitted[event]) {
228 return
229 }
230 emitter.emitted[event] = true;
231 emitter.emit(event, code, signal);
232 };
233
234 // { <signal>: <listener fn>, ... }
235 var sigListeners = {};
236 signals.forEach(function (sig) {
237 sigListeners[sig] = function listener () {
238 /* istanbul ignore if */
239 if (!processOk(rollup.commonjsGlobal.process)) {
240 return
241 }
242 // If there are no other listeners, an exit is coming!
243 // Simplest way: remove us and then re-send the signal.
244 // We know that this will kill the process, so we can
245 // safely emit now.
246 var listeners = process$1.listeners(sig);
247 if (listeners.length === emitter.count) {
248 unload();
249 emit('exit', null, sig);
250 /* istanbul ignore next */
251 emit('afterexit', null, sig);
252 /* istanbul ignore next */
253 if (isWin && sig === 'SIGHUP') {
254 // "SIGHUP" throws an `ENOSYS` error on Windows,
255 // so use a supported signal instead
256 sig = 'SIGINT';
257 }
258 /* istanbul ignore next */
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 // This is the number of onSignalExit's that are in play.
277 // It's important so that we can count the correct number of
278 // listeners on signals, and don't wait for the other one to
279 // handle it instead of us.
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 /* istanbul ignore if */
299 if (!processOk(rollup.commonjsGlobal.process)) {
300 return
301 }
302 process$1.exitCode = code || /* istanbul ignore next */ 0;
303 emit('exit', process$1.exitCode, null);
304 /* istanbul ignore next */
305 emit('afterexit', process$1.exitCode, null);
306 /* istanbul ignore next */
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 /* istanbul ignore else */
314 if (arg !== undefined) {
315 process$1.exitCode = arg;
316 }
317 var ret = originalProcessEmit.apply(this, arguments);
318 /* istanbul ignore next */
319 emit('exit', process$1.exitCode, null);
320 /* istanbul ignore next */
321 emit('afterexit', process$1.exitCode, null);
322 /* istanbul ignore next */
323 return ret
324 } else {
325 return originalProcessEmit.apply(this, arguments)
326 }
327 };
328}
329
330const onExit = signalExit.exports;
331
332const CLEAR_SCREEN = '\u001Bc';
333function 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
352async 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 // removing a non-existent listener is a no-op
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
477exports.watch = watch;
478//# sourceMappingURL=watch-cli.js.map