1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const { groupBy } = require("./util/ArrayHelpers");
|
---|
9 | const createSchemaValidation = require("./util/create-schema-validation");
|
---|
10 |
|
---|
11 | /** @typedef {import("../declarations/plugins/WatchIgnorePlugin").WatchIgnorePluginOptions} WatchIgnorePluginOptions */
|
---|
12 | /** @typedef {import("../declarations/WebpackOptions").WatchOptions} WatchOptions */
|
---|
13 | /** @typedef {import("./Compiler")} Compiler */
|
---|
14 | /** @typedef {import("./util/fs").TimeInfoEntries} TimeInfoEntries */
|
---|
15 | /** @typedef {import("./util/fs").WatchFileSystem} WatchFileSystem */
|
---|
16 | /** @typedef {import("./util/fs").WatchMethod} WatchMethod */
|
---|
17 | /** @typedef {import("./util/fs").Watcher} Watcher */
|
---|
18 | const validate = createSchemaValidation(
|
---|
19 | require("../schemas/plugins/WatchIgnorePlugin.check.js"),
|
---|
20 | () => require("../schemas/plugins/WatchIgnorePlugin.json"),
|
---|
21 | {
|
---|
22 | name: "Watch Ignore Plugin",
|
---|
23 | baseDataPath: "options"
|
---|
24 | }
|
---|
25 | );
|
---|
26 |
|
---|
27 | const IGNORE_TIME_ENTRY = "ignore";
|
---|
28 |
|
---|
29 | class IgnoringWatchFileSystem {
|
---|
30 | /**
|
---|
31 | * @param {WatchFileSystem} wfs original file system
|
---|
32 | * @param {WatchIgnorePluginOptions["paths"]} paths ignored paths
|
---|
33 | */
|
---|
34 | constructor(wfs, paths) {
|
---|
35 | this.wfs = wfs;
|
---|
36 | this.paths = paths;
|
---|
37 | }
|
---|
38 |
|
---|
39 | /** @type {WatchMethod} */
|
---|
40 | watch(files, dirs, missing, startTime, options, callback, callbackUndelayed) {
|
---|
41 | files = Array.from(files);
|
---|
42 | dirs = Array.from(dirs);
|
---|
43 | /**
|
---|
44 | * @param {string} path path to check
|
---|
45 | * @returns {boolean} true, if path is ignored
|
---|
46 | */
|
---|
47 | const ignored = path =>
|
---|
48 | this.paths.some(p =>
|
---|
49 | p instanceof RegExp ? p.test(path) : path.indexOf(p) === 0
|
---|
50 | );
|
---|
51 |
|
---|
52 | const [ignoredFiles, notIgnoredFiles] = groupBy(
|
---|
53 | /** @type {Array<string>} */
|
---|
54 | (files),
|
---|
55 | ignored
|
---|
56 | );
|
---|
57 | const [ignoredDirs, notIgnoredDirs] = groupBy(
|
---|
58 | /** @type {Array<string>} */
|
---|
59 | (dirs),
|
---|
60 | ignored
|
---|
61 | );
|
---|
62 |
|
---|
63 | const watcher = this.wfs.watch(
|
---|
64 | notIgnoredFiles,
|
---|
65 | notIgnoredDirs,
|
---|
66 | missing,
|
---|
67 | startTime,
|
---|
68 | options,
|
---|
69 | (err, fileTimestamps, dirTimestamps, changedFiles, removedFiles) => {
|
---|
70 | if (err) return callback(err);
|
---|
71 | for (const path of ignoredFiles) {
|
---|
72 | /** @type {TimeInfoEntries} */
|
---|
73 | (fileTimestamps).set(path, IGNORE_TIME_ENTRY);
|
---|
74 | }
|
---|
75 |
|
---|
76 | for (const path of ignoredDirs) {
|
---|
77 | /** @type {TimeInfoEntries} */
|
---|
78 | (dirTimestamps).set(path, IGNORE_TIME_ENTRY);
|
---|
79 | }
|
---|
80 |
|
---|
81 | callback(
|
---|
82 | null,
|
---|
83 | fileTimestamps,
|
---|
84 | dirTimestamps,
|
---|
85 | changedFiles,
|
---|
86 | removedFiles
|
---|
87 | );
|
---|
88 | },
|
---|
89 | callbackUndelayed
|
---|
90 | );
|
---|
91 |
|
---|
92 | return {
|
---|
93 | close: () => watcher.close(),
|
---|
94 | pause: () => watcher.pause(),
|
---|
95 | getContextTimeInfoEntries: () => {
|
---|
96 | const dirTimestamps = watcher.getContextTimeInfoEntries();
|
---|
97 | for (const path of ignoredDirs) {
|
---|
98 | dirTimestamps.set(path, IGNORE_TIME_ENTRY);
|
---|
99 | }
|
---|
100 | return dirTimestamps;
|
---|
101 | },
|
---|
102 | getFileTimeInfoEntries: () => {
|
---|
103 | const fileTimestamps = watcher.getFileTimeInfoEntries();
|
---|
104 | for (const path of ignoredFiles) {
|
---|
105 | fileTimestamps.set(path, IGNORE_TIME_ENTRY);
|
---|
106 | }
|
---|
107 | return fileTimestamps;
|
---|
108 | },
|
---|
109 | getInfo:
|
---|
110 | watcher.getInfo &&
|
---|
111 | (() => {
|
---|
112 | const info =
|
---|
113 | /** @type {NonNullable<Watcher["getInfo"]>} */
|
---|
114 | (watcher.getInfo)();
|
---|
115 | const { fileTimeInfoEntries, contextTimeInfoEntries } = info;
|
---|
116 | for (const path of ignoredFiles) {
|
---|
117 | fileTimeInfoEntries.set(path, IGNORE_TIME_ENTRY);
|
---|
118 | }
|
---|
119 | for (const path of ignoredDirs) {
|
---|
120 | contextTimeInfoEntries.set(path, IGNORE_TIME_ENTRY);
|
---|
121 | }
|
---|
122 | return info;
|
---|
123 | })
|
---|
124 | };
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | class WatchIgnorePlugin {
|
---|
129 | /**
|
---|
130 | * @param {WatchIgnorePluginOptions} options options
|
---|
131 | */
|
---|
132 | constructor(options) {
|
---|
133 | validate(options);
|
---|
134 | this.paths = options.paths;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Apply the plugin
|
---|
139 | * @param {Compiler} compiler the compiler instance
|
---|
140 | * @returns {void}
|
---|
141 | */
|
---|
142 | apply(compiler) {
|
---|
143 | compiler.hooks.afterEnvironment.tap("WatchIgnorePlugin", () => {
|
---|
144 | compiler.watchFileSystem = new IgnoringWatchFileSystem(
|
---|
145 | /** @type {WatchFileSystem} */
|
---|
146 | (compiler.watchFileSystem),
|
---|
147 | this.paths
|
---|
148 | );
|
---|
149 | });
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | module.exports = WatchIgnorePlugin;
|
---|