UNPKG

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