UNPKG

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