UNPKG

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