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 util = require("util");
|
---|
9 | const Watchpack = require("watchpack");
|
---|
10 |
|
---|
11 | /** @typedef {import("../../declarations/WebpackOptions").WatchOptions} WatchOptions */
|
---|
12 | /** @typedef {import("../FileSystemInfo").FileSystemInfoEntry} FileSystemInfoEntry */
|
---|
13 | /** @typedef {import("../util/fs").InputFileSystem} InputFileSystem */
|
---|
14 | /** @typedef {import("../util/fs").WatchMethod} WatchMethod */
|
---|
15 |
|
---|
16 | class NodeWatchFileSystem {
|
---|
17 | /**
|
---|
18 | * @param {InputFileSystem} inputFileSystem input filesystem
|
---|
19 | */
|
---|
20 | constructor(inputFileSystem) {
|
---|
21 | this.inputFileSystem = inputFileSystem;
|
---|
22 | this.watcherOptions = {
|
---|
23 | aggregateTimeout: 0
|
---|
24 | };
|
---|
25 | /** @type {Watchpack | null} */
|
---|
26 | this.watcher = new Watchpack(this.watcherOptions);
|
---|
27 | }
|
---|
28 |
|
---|
29 | /** @type {WatchMethod} */
|
---|
30 | watch(
|
---|
31 | files,
|
---|
32 | directories,
|
---|
33 | missing,
|
---|
34 | startTime,
|
---|
35 | options,
|
---|
36 | callback,
|
---|
37 | callbackUndelayed
|
---|
38 | ) {
|
---|
39 | if (!files || typeof files[Symbol.iterator] !== "function") {
|
---|
40 | throw new Error("Invalid arguments: 'files'");
|
---|
41 | }
|
---|
42 | if (!directories || typeof directories[Symbol.iterator] !== "function") {
|
---|
43 | throw new Error("Invalid arguments: 'directories'");
|
---|
44 | }
|
---|
45 | if (!missing || typeof missing[Symbol.iterator] !== "function") {
|
---|
46 | throw new Error("Invalid arguments: 'missing'");
|
---|
47 | }
|
---|
48 | if (typeof callback !== "function") {
|
---|
49 | throw new Error("Invalid arguments: 'callback'");
|
---|
50 | }
|
---|
51 | if (typeof startTime !== "number" && startTime) {
|
---|
52 | throw new Error("Invalid arguments: 'startTime'");
|
---|
53 | }
|
---|
54 | if (typeof options !== "object") {
|
---|
55 | throw new Error("Invalid arguments: 'options'");
|
---|
56 | }
|
---|
57 | if (typeof callbackUndelayed !== "function" && callbackUndelayed) {
|
---|
58 | throw new Error("Invalid arguments: 'callbackUndelayed'");
|
---|
59 | }
|
---|
60 | const oldWatcher = this.watcher;
|
---|
61 | this.watcher = new Watchpack(options);
|
---|
62 |
|
---|
63 | if (callbackUndelayed) {
|
---|
64 | this.watcher.once("change", callbackUndelayed);
|
---|
65 | }
|
---|
66 |
|
---|
67 | const fetchTimeInfo = () => {
|
---|
68 | const fileTimeInfoEntries = new Map();
|
---|
69 | const contextTimeInfoEntries = new Map();
|
---|
70 | if (this.watcher) {
|
---|
71 | this.watcher.collectTimeInfoEntries(
|
---|
72 | fileTimeInfoEntries,
|
---|
73 | contextTimeInfoEntries
|
---|
74 | );
|
---|
75 | }
|
---|
76 | return { fileTimeInfoEntries, contextTimeInfoEntries };
|
---|
77 | };
|
---|
78 | this.watcher.once(
|
---|
79 | "aggregated",
|
---|
80 | /**
|
---|
81 | * @param {Set<string>} changes changes
|
---|
82 | * @param {Set<string>} removals removals
|
---|
83 | */
|
---|
84 | (changes, removals) => {
|
---|
85 | // pause emitting events (avoids clearing aggregated changes and removals on timeout)
|
---|
86 | /** @type {Watchpack} */
|
---|
87 | (this.watcher).pause();
|
---|
88 |
|
---|
89 | const fs = this.inputFileSystem;
|
---|
90 | if (fs && fs.purge) {
|
---|
91 | for (const item of changes) {
|
---|
92 | fs.purge(item);
|
---|
93 | }
|
---|
94 | for (const item of removals) {
|
---|
95 | fs.purge(item);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | const { fileTimeInfoEntries, contextTimeInfoEntries } = fetchTimeInfo();
|
---|
99 | callback(
|
---|
100 | null,
|
---|
101 | fileTimeInfoEntries,
|
---|
102 | contextTimeInfoEntries,
|
---|
103 | changes,
|
---|
104 | removals
|
---|
105 | );
|
---|
106 | }
|
---|
107 | );
|
---|
108 |
|
---|
109 | this.watcher.watch({ files, directories, missing, startTime });
|
---|
110 |
|
---|
111 | if (oldWatcher) {
|
---|
112 | oldWatcher.close();
|
---|
113 | }
|
---|
114 | return {
|
---|
115 | close: () => {
|
---|
116 | if (this.watcher) {
|
---|
117 | this.watcher.close();
|
---|
118 | this.watcher = null;
|
---|
119 | }
|
---|
120 | },
|
---|
121 | pause: () => {
|
---|
122 | if (this.watcher) {
|
---|
123 | this.watcher.pause();
|
---|
124 | }
|
---|
125 | },
|
---|
126 | getAggregatedRemovals: util.deprecate(
|
---|
127 | () => {
|
---|
128 | const items = this.watcher && this.watcher.aggregatedRemovals;
|
---|
129 | const fs = this.inputFileSystem;
|
---|
130 | if (items && fs && fs.purge) {
|
---|
131 | for (const item of items) {
|
---|
132 | fs.purge(item);
|
---|
133 | }
|
---|
134 | }
|
---|
135 | return items;
|
---|
136 | },
|
---|
137 | "Watcher.getAggregatedRemovals is deprecated in favor of Watcher.getInfo since that's more performant.",
|
---|
138 | "DEP_WEBPACK_WATCHER_GET_AGGREGATED_REMOVALS"
|
---|
139 | ),
|
---|
140 | getAggregatedChanges: util.deprecate(
|
---|
141 | () => {
|
---|
142 | const items = this.watcher && this.watcher.aggregatedChanges;
|
---|
143 | const fs = this.inputFileSystem;
|
---|
144 | if (items && fs && fs.purge) {
|
---|
145 | for (const item of items) {
|
---|
146 | fs.purge(item);
|
---|
147 | }
|
---|
148 | }
|
---|
149 | return items;
|
---|
150 | },
|
---|
151 | "Watcher.getAggregatedChanges is deprecated in favor of Watcher.getInfo since that's more performant.",
|
---|
152 | "DEP_WEBPACK_WATCHER_GET_AGGREGATED_CHANGES"
|
---|
153 | ),
|
---|
154 | getFileTimeInfoEntries: util.deprecate(
|
---|
155 | () => fetchTimeInfo().fileTimeInfoEntries,
|
---|
156 | "Watcher.getFileTimeInfoEntries is deprecated in favor of Watcher.getInfo since that's more performant.",
|
---|
157 | "DEP_WEBPACK_WATCHER_FILE_TIME_INFO_ENTRIES"
|
---|
158 | ),
|
---|
159 | getContextTimeInfoEntries: util.deprecate(
|
---|
160 | () => fetchTimeInfo().contextTimeInfoEntries,
|
---|
161 | "Watcher.getContextTimeInfoEntries is deprecated in favor of Watcher.getInfo since that's more performant.",
|
---|
162 | "DEP_WEBPACK_WATCHER_CONTEXT_TIME_INFO_ENTRIES"
|
---|
163 | ),
|
---|
164 | getInfo: () => {
|
---|
165 | const removals = this.watcher && this.watcher.aggregatedRemovals;
|
---|
166 | const changes = this.watcher && this.watcher.aggregatedChanges;
|
---|
167 | const fs = this.inputFileSystem;
|
---|
168 | if (fs && fs.purge) {
|
---|
169 | if (removals) {
|
---|
170 | for (const item of removals) {
|
---|
171 | fs.purge(item);
|
---|
172 | }
|
---|
173 | }
|
---|
174 | if (changes) {
|
---|
175 | for (const item of changes) {
|
---|
176 | fs.purge(item);
|
---|
177 | }
|
---|
178 | }
|
---|
179 | }
|
---|
180 | const { fileTimeInfoEntries, contextTimeInfoEntries } = fetchTimeInfo();
|
---|
181 | return {
|
---|
182 | changes,
|
---|
183 | removals,
|
---|
184 | fileTimeInfoEntries,
|
---|
185 | contextTimeInfoEntries
|
---|
186 | };
|
---|
187 | }
|
---|
188 | };
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | module.exports = NodeWatchFileSystem;
|
---|