webpack/lib/WatchIgnorePlugin.js

156 lines
4.0 KiB
JavaScript
Raw Normal View History

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2018-07-30 23:08:51 +08:00
2017-01-11 17:51:58 +08:00
"use strict";
const { groupBy } = require("./util/ArrayHelpers");
const createSchemaValidation = require("./util/create-schema-validation");
2017-10-28 05:23:38 +08:00
/** @typedef {import("../declarations/plugins/WatchIgnorePlugin").WatchIgnorePluginOptions} WatchIgnorePluginOptions */
2024-08-09 01:03:17 +08:00
/** @typedef {import("../declarations/WebpackOptions").WatchOptions} WatchOptions */
2018-11-09 05:59:19 +08:00
/** @typedef {import("./Compiler")} Compiler */
2024-08-09 01:03:17 +08:00
/** @typedef {import("./util/fs").TimeInfoEntries} TimeInfoEntries */
2021-01-12 05:15:31 +08:00
/** @typedef {import("./util/fs").WatchFileSystem} WatchFileSystem */
2024-08-09 01:03:17 +08:00
/** @typedef {import("./util/fs").WatchMethod} WatchMethod */
/** @typedef {import("./util/fs").Watcher} Watcher */
const validate = createSchemaValidation(
require("../schemas/plugins/WatchIgnorePlugin.check.js"),
() => require("../schemas/plugins/WatchIgnorePlugin.json"),
{
name: "Watch Ignore Plugin",
baseDataPath: "options"
}
);
2019-11-06 06:46:34 +08:00
const IGNORE_TIME_ENTRY = "ignore";
class IgnoringWatchFileSystem {
2021-01-12 05:15:31 +08:00
/**
* @param {WatchFileSystem} wfs original file system
2024-08-09 01:03:17 +08:00
* @param {WatchIgnorePluginOptions["paths"]} paths ignored paths
2021-01-12 05:15:31 +08:00
*/
constructor(wfs, paths) {
this.wfs = wfs;
this.paths = paths;
}
2024-08-09 01:03:17 +08:00
/** @type {WatchMethod} */
watch(files, dirs, missing, startTime, options, callback, callbackUndelayed) {
files = Array.from(files);
dirs = Array.from(dirs);
2023-05-25 06:41:32 +08:00
/**
* @param {string} path path to check
* @returns {boolean} true, if path is ignored
*/
2018-02-25 09:00:20 +08:00
const ignored = path =>
2019-02-05 17:06:32 +08:00
this.paths.some(p =>
p instanceof RegExp ? p.test(path) : path.indexOf(p) === 0
2018-02-25 09:00:20 +08:00
);
2024-08-09 01:03:17 +08:00
const [ignoredFiles, notIgnoredFiles] = groupBy(
/** @type {Array<string>} */
(files),
ignored
);
const [ignoredDirs, notIgnoredDirs] = groupBy(
/** @type {Array<string>} */
(dirs),
ignored
);
2015-08-25 16:32:06 +08:00
2018-02-25 09:00:20 +08:00
const watcher = this.wfs.watch(
notIgnoredFiles,
notIgnoredDirs,
2018-02-25 09:00:20 +08:00
missing,
startTime,
options,
(err, fileTimestamps, dirTimestamps, changedFiles, removedFiles) => {
2018-02-25 09:00:20 +08:00
if (err) return callback(err);
for (const path of ignoredFiles) {
2024-08-09 01:03:17 +08:00
/** @type {TimeInfoEntries} */
(fileTimestamps).set(path, IGNORE_TIME_ENTRY);
2018-02-25 09:00:20 +08:00
}
2018-02-25 09:00:20 +08:00
for (const path of ignoredDirs) {
2024-08-09 01:03:17 +08:00
/** @type {TimeInfoEntries} */
(dirTimestamps).set(path, IGNORE_TIME_ENTRY);
2018-02-25 09:00:20 +08:00
}
callback(
2024-08-09 01:03:17 +08:00
null,
fileTimestamps,
dirTimestamps,
changedFiles,
removedFiles
);
2018-02-25 09:00:20 +08:00
},
callbackUndelayed
);
return {
close: () => watcher.close(),
pause: () => watcher.pause(),
getContextTimeInfoEntries: () => {
const dirTimestamps = watcher.getContextTimeInfoEntries();
2018-02-25 09:00:20 +08:00
for (const path of ignoredDirs) {
2019-11-06 06:46:34 +08:00
dirTimestamps.set(path, IGNORE_TIME_ENTRY);
2018-01-22 20:52:43 +08:00
}
return dirTimestamps;
},
getFileTimeInfoEntries: () => {
const fileTimestamps = watcher.getFileTimeInfoEntries();
2018-02-25 09:00:20 +08:00
for (const path of ignoredFiles) {
2019-11-06 06:46:34 +08:00
fileTimestamps.set(path, IGNORE_TIME_ENTRY);
2018-01-22 20:52:43 +08:00
}
return fileTimestamps;
},
getInfo:
watcher.getInfo &&
(() => {
2024-08-09 01:03:17 +08:00
const info =
/** @type {NonNullable<Watcher["getInfo"]>} */
(watcher.getInfo)();
const { fileTimeInfoEntries, contextTimeInfoEntries } = info;
for (const path of ignoredFiles) {
fileTimeInfoEntries.set(path, IGNORE_TIME_ENTRY);
}
for (const path of ignoredDirs) {
contextTimeInfoEntries.set(path, IGNORE_TIME_ENTRY);
}
return info;
})
};
}
}
2018-07-31 14:17:44 +08:00
2025-04-23 20:03:37 +08:00
const PLUGIN_NAME = "WatchIgnorePlugin";
2018-07-31 14:17:44 +08:00
class WatchIgnorePlugin {
/**
2019-08-08 00:15:41 +08:00
* @param {WatchIgnorePluginOptions} options options
*/
2019-08-08 00:15:41 +08:00
constructor(options) {
validate(options);
2019-08-08 00:15:41 +08:00
this.paths = options.paths;
2018-07-31 14:17:44 +08:00
}
2018-11-09 05:59:19 +08:00
/**
2020-04-23 16:48:36 +08:00
* Apply the plugin
2018-11-09 05:59:19 +08:00
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
2018-07-31 14:17:44 +08:00
apply(compiler) {
2025-04-23 20:03:37 +08:00
compiler.hooks.afterEnvironment.tap(PLUGIN_NAME, () => {
2018-07-31 14:17:44 +08:00
compiler.watchFileSystem = new IgnoringWatchFileSystem(
2024-08-09 01:03:17 +08:00
/** @type {WatchFileSystem} */
(compiler.watchFileSystem),
2018-07-31 14:17:44 +08:00
this.paths
);
});
}
}
module.exports = WatchIgnorePlugin;