1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const { validate } = require("schema-utils");
|
9 | const schema = require("../schemas/plugins/WatchIgnorePlugin.json");
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | const IGNORE_TIME_ENTRY = "ignore";
|
16 |
|
17 | class IgnoringWatchFileSystem {
|
18 | |
19 |
|
20 |
|
21 |
|
22 | constructor(wfs, paths) {
|
23 | this.wfs = wfs;
|
24 | this.paths = paths;
|
25 | }
|
26 |
|
27 | watch(files, dirs, missing, startTime, options, callback, callbackUndelayed) {
|
28 | files = Array.from(files);
|
29 | dirs = Array.from(dirs);
|
30 | const ignored = path =>
|
31 | this.paths.some(p =>
|
32 | p instanceof RegExp ? p.test(path) : path.indexOf(p) === 0
|
33 | );
|
34 |
|
35 | const notIgnored = path => !ignored(path);
|
36 |
|
37 | const ignoredFiles = files.filter(ignored);
|
38 | const ignoredDirs = dirs.filter(ignored);
|
39 |
|
40 | const watcher = this.wfs.watch(
|
41 | files.filter(notIgnored),
|
42 | dirs.filter(notIgnored),
|
43 | missing,
|
44 | startTime,
|
45 | options,
|
46 | (err, fileTimestamps, dirTimestamps, changedFiles, removedFiles) => {
|
47 | if (err) return callback(err);
|
48 | for (const path of ignoredFiles) {
|
49 | fileTimestamps.set(path, IGNORE_TIME_ENTRY);
|
50 | }
|
51 |
|
52 | for (const path of ignoredDirs) {
|
53 | dirTimestamps.set(path, IGNORE_TIME_ENTRY);
|
54 | }
|
55 |
|
56 | callback(
|
57 | err,
|
58 | fileTimestamps,
|
59 | dirTimestamps,
|
60 | changedFiles,
|
61 | removedFiles
|
62 | );
|
63 | },
|
64 | callbackUndelayed
|
65 | );
|
66 |
|
67 | return {
|
68 | close: () => watcher.close(),
|
69 | pause: () => watcher.pause(),
|
70 | getContextTimeInfoEntries: () => {
|
71 | const dirTimestamps = watcher.getContextTimeInfoEntries();
|
72 | for (const path of ignoredDirs) {
|
73 | dirTimestamps.set(path, IGNORE_TIME_ENTRY);
|
74 | }
|
75 | return dirTimestamps;
|
76 | },
|
77 | getFileTimeInfoEntries: () => {
|
78 | const fileTimestamps = watcher.getFileTimeInfoEntries();
|
79 | for (const path of ignoredFiles) {
|
80 | fileTimestamps.set(path, IGNORE_TIME_ENTRY);
|
81 | }
|
82 | return fileTimestamps;
|
83 | }
|
84 | };
|
85 | }
|
86 | }
|
87 |
|
88 | class WatchIgnorePlugin {
|
89 | |
90 |
|
91 |
|
92 | constructor(options) {
|
93 | validate(schema, options, {
|
94 | name: "Watch Ignore Plugin",
|
95 | baseDataPath: "options"
|
96 | });
|
97 | this.paths = options.paths;
|
98 | }
|
99 |
|
100 | |
101 |
|
102 |
|
103 |
|
104 |
|
105 | apply(compiler) {
|
106 | compiler.hooks.afterEnvironment.tap("WatchIgnorePlugin", () => {
|
107 | compiler.watchFileSystem = new IgnoringWatchFileSystem(
|
108 | compiler.watchFileSystem,
|
109 | this.paths
|
110 | );
|
111 | });
|
112 | }
|
113 | }
|
114 |
|
115 | module.exports = WatchIgnorePlugin;
|