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