1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | 'use strict';
|
12 |
|
13 | var path = require('path');
|
14 | var require$$0 = require('util');
|
15 | var index = require('./index.js');
|
16 | var rollup = require('./rollup.js');
|
17 | var mergeOptions = require('./mergeOptions.js');
|
18 | var require$$2 = require('os');
|
19 | require('events');
|
20 | require('fs');
|
21 | require('stream');
|
22 | require('crypto');
|
23 |
|
24 | function _interopNamespaceDefault(e) {
|
25 | var n = Object.create(null);
|
26 | if (e) {
|
27 | Object.keys(e).forEach(function (k) {
|
28 | n[k] = e[k];
|
29 | });
|
30 | }
|
31 | n['default'] = e;
|
32 | return n;
|
33 | }
|
34 |
|
35 | var path__namespace = _interopNamespaceDefault(path);
|
36 |
|
37 | const util = require$$0;
|
38 | const braces = index.braces_1;
|
39 | const picomatch = index.picomatch;
|
40 | const utils = index.utils;
|
41 | const isEmptyString = val => val === '' || val === './';
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | const micromatch = (list, patterns, options) => {
|
62 | patterns = [].concat(patterns);
|
63 | list = [].concat(list);
|
64 |
|
65 | let omit = new Set();
|
66 | let keep = new Set();
|
67 | let items = new Set();
|
68 | let negatives = 0;
|
69 |
|
70 | let onResult = state => {
|
71 | items.add(state.output);
|
72 | if (options && options.onResult) {
|
73 | options.onResult(state);
|
74 | }
|
75 | };
|
76 |
|
77 | for (let i = 0; i < patterns.length; i++) {
|
78 | let isMatch = picomatch(String(patterns[i]), { ...options, onResult }, true);
|
79 | let negated = isMatch.state.negated || isMatch.state.negatedExtglob;
|
80 | if (negated) negatives++;
|
81 |
|
82 | for (let item of list) {
|
83 | let matched = isMatch(item, true);
|
84 |
|
85 | let match = negated ? !matched.isMatch : matched.isMatch;
|
86 | if (!match) continue;
|
87 |
|
88 | if (negated) {
|
89 | omit.add(matched.output);
|
90 | } else {
|
91 | omit.delete(matched.output);
|
92 | keep.add(matched.output);
|
93 | }
|
94 | }
|
95 | }
|
96 |
|
97 | let result = negatives === patterns.length ? [...items] : [...keep];
|
98 | let matches = result.filter(item => !omit.has(item));
|
99 |
|
100 | if (options && matches.length === 0) {
|
101 | if (options.failglob === true) {
|
102 | throw new Error(`No matches found for "${patterns.join(', ')}"`);
|
103 | }
|
104 |
|
105 | if (options.nonull === true || options.nullglob === true) {
|
106 | return options.unescape ? patterns.map(p => p.replace(/\\/g, '')) : patterns;
|
107 | }
|
108 | }
|
109 |
|
110 | return matches;
|
111 | };
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | micromatch.match = micromatch;
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | micromatch.matcher = (pattern, options) => picomatch(pattern, options);
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | micromatch.isMatch = (str, patterns, options) => picomatch(patterns, options)(str);
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 | micromatch.any = micromatch.isMatch;
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 | micromatch.not = (list, patterns, options = {}) => {
|
183 | patterns = [].concat(patterns).map(String);
|
184 | let result = new Set();
|
185 | let items = [];
|
186 |
|
187 | let onResult = state => {
|
188 | if (options.onResult) options.onResult(state);
|
189 | items.push(state.output);
|
190 | };
|
191 |
|
192 | let matches = micromatch(list, patterns, { ...options, onResult });
|
193 |
|
194 | for (let item of items) {
|
195 | if (!matches.includes(item)) {
|
196 | result.add(item);
|
197 | }
|
198 | }
|
199 | return [...result];
|
200 | };
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 | micromatch.contains = (str, pattern, options) => {
|
223 | if (typeof str !== 'string') {
|
224 | throw new TypeError(`Expected a string: "${util.inspect(str)}"`);
|
225 | }
|
226 |
|
227 | if (Array.isArray(pattern)) {
|
228 | return pattern.some(p => micromatch.contains(str, p, options));
|
229 | }
|
230 |
|
231 | if (typeof pattern === 'string') {
|
232 | if (isEmptyString(str) || isEmptyString(pattern)) {
|
233 | return false;
|
234 | }
|
235 |
|
236 | if (str.includes(pattern) || (str.startsWith('./') && str.slice(2).includes(pattern))) {
|
237 | return true;
|
238 | }
|
239 | }
|
240 |
|
241 | return micromatch.isMatch(str, pattern, { ...options, contains: true });
|
242 | };
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 | micromatch.matchKeys = (obj, patterns, options) => {
|
265 | if (!utils.isObject(obj)) {
|
266 | throw new TypeError('Expected the first argument to be an object');
|
267 | }
|
268 | let keys = micromatch(Object.keys(obj), patterns, options);
|
269 | let res = {};
|
270 | for (let key of keys) res[key] = obj[key];
|
271 | return res;
|
272 | };
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 | micromatch.some = (list, patterns, options) => {
|
294 | let items = [].concat(list);
|
295 |
|
296 | for (let pattern of [].concat(patterns)) {
|
297 | let isMatch = picomatch(String(pattern), options);
|
298 | if (items.some(item => isMatch(item))) {
|
299 | return true;
|
300 | }
|
301 | }
|
302 | return false;
|
303 | };
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 | micromatch.every = (list, patterns, options) => {
|
330 | let items = [].concat(list);
|
331 |
|
332 | for (let pattern of [].concat(patterns)) {
|
333 | let isMatch = picomatch(String(pattern), options);
|
334 | if (!items.every(item => isMatch(item))) {
|
335 | return false;
|
336 | }
|
337 | }
|
338 | return true;
|
339 | };
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 | micromatch.all = (str, patterns, options) => {
|
369 | if (typeof str !== 'string') {
|
370 | throw new TypeError(`Expected a string: "${util.inspect(str)}"`);
|
371 | }
|
372 |
|
373 | return [].concat(patterns).every(p => picomatch(p, options)(str));
|
374 | };
|
375 |
|
376 |
|
377 |
|
378 |
|
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 | micromatch.capture = (glob, input, options) => {
|
396 | let posix = utils.isWindows(options);
|
397 | let regex = picomatch.makeRe(String(glob), { ...options, capture: true });
|
398 | let match = regex.exec(posix ? utils.toPosixSlashes(input) : input);
|
399 |
|
400 | if (match) {
|
401 | return match.slice(1).map(v => v === void 0 ? '' : v);
|
402 | }
|
403 | };
|
404 |
|
405 |
|
406 |
|
407 |
|
408 |
|
409 |
|
410 |
|
411 |
|
412 |
|
413 |
|
414 |
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 | micromatch.makeRe = (...args) => picomatch.makeRe(...args);
|
422 |
|
423 |
|
424 |
|
425 |
|
426 |
|
427 |
|
428 |
|
429 |
|
430 |
|
431 |
|
432 |
|
433 |
|
434 |
|
435 |
|
436 |
|
437 | micromatch.scan = (...args) => picomatch.scan(...args);
|
438 |
|
439 |
|
440 |
|
441 |
|
442 |
|
443 |
|
444 |
|
445 |
|
446 |
|
447 |
|
448 |
|
449 |
|
450 |
|
451 |
|
452 |
|
453 | micromatch.parse = (patterns, options) => {
|
454 | let res = [];
|
455 | for (let pattern of [].concat(patterns || [])) {
|
456 | for (let str of braces(String(pattern), options)) {
|
457 | res.push(picomatch.parse(str, options));
|
458 | }
|
459 | }
|
460 | return res;
|
461 | };
|
462 |
|
463 |
|
464 |
|
465 |
|
466 |
|
467 |
|
468 |
|
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 |
|
477 |
|
478 |
|
479 |
|
480 | micromatch.braces = (pattern, options) => {
|
481 | if (typeof pattern !== 'string') throw new TypeError('Expected a string');
|
482 | if ((options && options.nobrace === true) || !/\{.*\}/.test(pattern)) {
|
483 | return [pattern];
|
484 | }
|
485 | return braces(pattern, options);
|
486 | };
|
487 |
|
488 |
|
489 |
|
490 |
|
491 |
|
492 | micromatch.braceExpand = (pattern, options) => {
|
493 | if (typeof pattern !== 'string') throw new TypeError('Expected a string');
|
494 | return micromatch.braces(pattern, { ...options, expand: true });
|
495 | };
|
496 |
|
497 |
|
498 |
|
499 |
|
500 |
|
501 | var micromatch_1 = micromatch;
|
502 |
|
503 | var mm = micromatch_1;
|
504 |
|
505 | function ensureArray(thing) {
|
506 | if (Array.isArray(thing))
|
507 | return thing;
|
508 | if (thing == undefined)
|
509 | return [];
|
510 | return [thing];
|
511 | }
|
512 |
|
513 | function getMatcherString(id, resolutionBase) {
|
514 | if (resolutionBase === false) {
|
515 | return id;
|
516 | }
|
517 | return path.resolve(...(typeof resolutionBase === 'string' ? [resolutionBase, id] : [id]));
|
518 | }
|
519 | const createFilter = function createFilter(include, exclude, options) {
|
520 | const resolutionBase = options && options.resolve;
|
521 | const getMatcher = (id) => {
|
522 | return id instanceof RegExp
|
523 | ? id
|
524 | : {
|
525 | test: mm.matcher(getMatcherString(id, resolutionBase)
|
526 | .split(path.sep)
|
527 | .join('/'), { dot: true })
|
528 | };
|
529 | };
|
530 | const includeMatchers = ensureArray(include).map(getMatcher);
|
531 | const excludeMatchers = ensureArray(exclude).map(getMatcher);
|
532 | return function (id) {
|
533 | if (typeof id !== 'string')
|
534 | return false;
|
535 | if (/\0/.test(id))
|
536 | return false;
|
537 | id = id.split(path.sep).join('/');
|
538 | for (let i = 0; i < excludeMatchers.length; ++i) {
|
539 | const matcher = excludeMatchers[i];
|
540 | if (matcher.test(id))
|
541 | return false;
|
542 | }
|
543 | for (let i = 0; i < includeMatchers.length; ++i) {
|
544 | const matcher = includeMatchers[i];
|
545 | if (matcher.test(id))
|
546 | return true;
|
547 | }
|
548 | return !includeMatchers.length;
|
549 | };
|
550 | };
|
551 |
|
552 | class FileWatcher {
|
553 | constructor(task, chokidarOptions) {
|
554 | this.transformWatchers = new Map();
|
555 | this.chokidarOptions = chokidarOptions;
|
556 | this.task = task;
|
557 | this.watcher = this.createWatcher(null);
|
558 | }
|
559 | close() {
|
560 | this.watcher.close();
|
561 | for (const watcher of this.transformWatchers.values()) {
|
562 | watcher.close();
|
563 | }
|
564 | }
|
565 | unwatch(id) {
|
566 | this.watcher.unwatch(id);
|
567 | const transformWatcher = this.transformWatchers.get(id);
|
568 | if (transformWatcher) {
|
569 | this.transformWatchers.delete(id);
|
570 | transformWatcher.close();
|
571 | }
|
572 | }
|
573 | watch(id, isTransformDependency) {
|
574 | if (isTransformDependency) {
|
575 | const watcher = this.transformWatchers.get(id) || this.createWatcher(id);
|
576 | watcher.add(id);
|
577 | this.transformWatchers.set(id, watcher);
|
578 | }
|
579 | else {
|
580 | this.watcher.add(id);
|
581 | }
|
582 | }
|
583 | createWatcher(transformWatcherId) {
|
584 | const task = this.task;
|
585 | const isLinux = require$$2.platform() === 'linux';
|
586 | const isTransformDependency = transformWatcherId !== null;
|
587 | const handleChange = (id, event) => {
|
588 | const changedId = transformWatcherId || id;
|
589 | if (isLinux) {
|
590 |
|
591 |
|
592 |
|
593 | watcher.unwatch(changedId);
|
594 | watcher.add(changedId);
|
595 | }
|
596 | task.invalidate(changedId, { event, isTransformDependency });
|
597 | };
|
598 | const watcher = index.chokidar
|
599 | .watch([], this.chokidarOptions)
|
600 | .on('add', id => handleChange(id, 'create'))
|
601 | .on('change', id => handleChange(id, 'update'))
|
602 | .on('unlink', id => handleChange(id, 'delete'));
|
603 | return watcher;
|
604 | }
|
605 | }
|
606 |
|
607 | const eventsRewrites = {
|
608 | create: {
|
609 | create: 'buggy',
|
610 | delete: null,
|
611 | update: 'create'
|
612 | },
|
613 | delete: {
|
614 | create: 'update',
|
615 | delete: 'buggy',
|
616 | update: 'buggy'
|
617 | },
|
618 | update: {
|
619 | create: 'buggy',
|
620 | delete: 'delete',
|
621 | update: 'update'
|
622 | }
|
623 | };
|
624 | class Watcher {
|
625 | constructor(configs, emitter) {
|
626 | this.buildDelay = 0;
|
627 | this.buildTimeout = null;
|
628 | this.invalidatedIds = new Map();
|
629 | this.rerun = false;
|
630 | this.emitter = emitter;
|
631 | emitter.close = this.close.bind(this);
|
632 | this.tasks = configs.map(config => new Task(this, config));
|
633 | this.buildDelay = configs.reduce((buildDelay, { watch }) => watch && typeof watch.buildDelay === 'number'
|
634 | ? Math.max(buildDelay, watch.buildDelay)
|
635 | : buildDelay, this.buildDelay);
|
636 | this.running = true;
|
637 | process.nextTick(() => this.run());
|
638 | }
|
639 | close() {
|
640 | if (this.buildTimeout)
|
641 | clearTimeout(this.buildTimeout);
|
642 | for (const task of this.tasks) {
|
643 | task.close();
|
644 | }
|
645 | this.emitter.emit('close');
|
646 | this.emitter.removeAllListeners();
|
647 | }
|
648 | invalidate(file) {
|
649 | if (file) {
|
650 | const prevEvent = this.invalidatedIds.get(file.id);
|
651 | const event = prevEvent ? eventsRewrites[prevEvent][file.event] : file.event;
|
652 | if (event === 'buggy') {
|
653 |
|
654 | this.invalidatedIds.set(file.id, file.event);
|
655 | }
|
656 | else if (event === null) {
|
657 | this.invalidatedIds.delete(file.id);
|
658 | }
|
659 | else {
|
660 | this.invalidatedIds.set(file.id, event);
|
661 | }
|
662 | }
|
663 | if (this.running) {
|
664 | this.rerun = true;
|
665 | return;
|
666 | }
|
667 | if (this.buildTimeout)
|
668 | clearTimeout(this.buildTimeout);
|
669 | this.buildTimeout = setTimeout(() => {
|
670 | this.buildTimeout = null;
|
671 | for (const [id, event] of this.invalidatedIds.entries()) {
|
672 | this.emitter.emit('change', id, { event });
|
673 | }
|
674 | this.invalidatedIds.clear();
|
675 | this.emitter.emit('restart');
|
676 | this.run();
|
677 | }, this.buildDelay);
|
678 | }
|
679 | async run() {
|
680 | this.running = true;
|
681 | this.emitter.emit('event', {
|
682 | code: 'START'
|
683 | });
|
684 | for (const task of this.tasks) {
|
685 | await task.run();
|
686 | }
|
687 | this.running = false;
|
688 | this.emitter.emit('event', {
|
689 | code: 'END'
|
690 | });
|
691 | if (this.rerun) {
|
692 | this.rerun = false;
|
693 | this.invalidate();
|
694 | }
|
695 | }
|
696 | }
|
697 | class Task {
|
698 | constructor(watcher, config) {
|
699 | this.cache = { modules: [] };
|
700 | this.watchFiles = [];
|
701 | this.invalidated = true;
|
702 | this.watcher = watcher;
|
703 | this.closed = false;
|
704 | this.watched = new Set();
|
705 | this.skipWrite = Boolean(config.watch && config.watch.skipWrite);
|
706 | this.options = mergeOptions.mergeOptions(config);
|
707 | this.outputs = this.options.output;
|
708 | this.outputFiles = this.outputs.map(output => {
|
709 | if (output.file || output.dir)
|
710 | return path__namespace.resolve(output.file || output.dir);
|
711 | return undefined;
|
712 | });
|
713 | const watchOptions = this.options.watch || {};
|
714 | this.filter = createFilter(watchOptions.include, watchOptions.exclude);
|
715 | this.fileWatcher = new FileWatcher(this, {
|
716 | ...watchOptions.chokidar,
|
717 | disableGlobbing: true,
|
718 | ignoreInitial: true
|
719 | });
|
720 | }
|
721 | close() {
|
722 | this.closed = true;
|
723 | this.fileWatcher.close();
|
724 | }
|
725 | invalidate(id, details) {
|
726 | this.invalidated = true;
|
727 | if (details.isTransformDependency) {
|
728 | for (const module of this.cache.modules) {
|
729 | if (module.transformDependencies.indexOf(id) === -1)
|
730 | continue;
|
731 |
|
732 | module.originalCode = null;
|
733 | }
|
734 | }
|
735 | this.watcher.invalidate({ event: details.event, id });
|
736 | }
|
737 | async run() {
|
738 | if (!this.invalidated)
|
739 | return;
|
740 | this.invalidated = false;
|
741 | const options = {
|
742 | ...this.options,
|
743 | cache: this.cache
|
744 | };
|
745 | const start = Date.now();
|
746 | this.watcher.emitter.emit('event', {
|
747 | code: 'BUNDLE_START',
|
748 | input: this.options.input,
|
749 | output: this.outputFiles
|
750 | });
|
751 | let result = null;
|
752 | try {
|
753 | result = await rollup.rollupInternal(options, this.watcher.emitter);
|
754 | if (this.closed) {
|
755 | return;
|
756 | }
|
757 | this.updateWatchedFiles(result);
|
758 | this.skipWrite || (await Promise.all(this.outputs.map(output => result.write(output))));
|
759 | this.watcher.emitter.emit('event', {
|
760 | code: 'BUNDLE_END',
|
761 | duration: Date.now() - start,
|
762 | input: this.options.input,
|
763 | output: this.outputFiles,
|
764 | result
|
765 | });
|
766 | }
|
767 | catch (error) {
|
768 | if (!this.closed) {
|
769 | if (Array.isArray(error.watchFiles)) {
|
770 | for (const id of error.watchFiles) {
|
771 | this.watchFile(id);
|
772 | }
|
773 | }
|
774 | if (error.id) {
|
775 | this.cache.modules = this.cache.modules.filter(module => module.id !== error.id);
|
776 | }
|
777 | }
|
778 | this.watcher.emitter.emit('event', {
|
779 | code: 'ERROR',
|
780 | error,
|
781 | result
|
782 | });
|
783 | }
|
784 | }
|
785 | updateWatchedFiles(result) {
|
786 | const previouslyWatched = this.watched;
|
787 | this.watched = new Set();
|
788 | this.watchFiles = result.watchFiles;
|
789 | this.cache = result.cache;
|
790 | for (const id of this.watchFiles) {
|
791 | this.watchFile(id);
|
792 | }
|
793 | for (const module of this.cache.modules) {
|
794 | for (const depId of module.transformDependencies) {
|
795 | this.watchFile(depId, true);
|
796 | }
|
797 | }
|
798 | for (const id of previouslyWatched) {
|
799 | if (!this.watched.has(id)) {
|
800 | this.fileWatcher.unwatch(id);
|
801 | }
|
802 | }
|
803 | }
|
804 | watchFile(id, isTransformDependency = false) {
|
805 | if (!this.filter(id))
|
806 | return;
|
807 | this.watched.add(id);
|
808 | if (this.outputFiles.some(file => file === id)) {
|
809 | throw new Error('Cannot import the generated bundle');
|
810 | }
|
811 |
|
812 |
|
813 | this.fileWatcher.watch(id, isTransformDependency);
|
814 | }
|
815 | }
|
816 |
|
817 | exports.Task = Task;
|
818 | exports.Watcher = Watcher;
|
819 |
|