UNPKG

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