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