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