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