1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Sergey Melyukov @smelukov
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const asyncLib = require("neo-async");
|
---|
9 | const { SyncBailHook } = require("tapable");
|
---|
10 | const Compilation = require("./Compilation");
|
---|
11 | const createSchemaValidation = require("./util/create-schema-validation");
|
---|
12 | const { join } = require("./util/fs");
|
---|
13 | const processAsyncTree = require("./util/processAsyncTree");
|
---|
14 |
|
---|
15 | /** @typedef {import("../declarations/WebpackOptions").CleanOptions} CleanOptions */
|
---|
16 | /** @typedef {import("./Compiler")} Compiler */
|
---|
17 | /** @typedef {import("./logging/Logger").Logger} Logger */
|
---|
18 | /** @typedef {import("./util/fs").IStats} IStats */
|
---|
19 | /** @typedef {import("./util/fs").OutputFileSystem} OutputFileSystem */
|
---|
20 | /** @typedef {import("./util/fs").StatsCallback} StatsCallback */
|
---|
21 |
|
---|
22 | /** @typedef {(function(string):boolean)|RegExp} IgnoreItem */
|
---|
23 | /** @typedef {Map<string, number>} Assets */
|
---|
24 | /** @typedef {function(IgnoreItem): void} AddToIgnoreCallback */
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * @typedef {object} CleanPluginCompilationHooks
|
---|
28 | * @property {SyncBailHook<[string], boolean | void>} keep when returning true the file/directory will be kept during cleaning, returning false will clean it and ignore the following plugins and config
|
---|
29 | */
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * @callback KeepFn
|
---|
33 | * @param {string} path path
|
---|
34 | * @returns {boolean | void} true, if the path should be kept
|
---|
35 | */
|
---|
36 |
|
---|
37 | const validate = createSchemaValidation(
|
---|
38 | undefined,
|
---|
39 | () => {
|
---|
40 | const { definitions } = require("../schemas/WebpackOptions.json");
|
---|
41 | return {
|
---|
42 | definitions,
|
---|
43 | oneOf: [{ $ref: "#/definitions/CleanOptions" }]
|
---|
44 | };
|
---|
45 | },
|
---|
46 | {
|
---|
47 | name: "Clean Plugin",
|
---|
48 | baseDataPath: "options"
|
---|
49 | }
|
---|
50 | );
|
---|
51 | const _10sec = 10 * 1000;
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * marge assets map 2 into map 1
|
---|
55 | * @param {Assets} as1 assets
|
---|
56 | * @param {Assets} as2 assets
|
---|
57 | * @returns {void}
|
---|
58 | */
|
---|
59 | const mergeAssets = (as1, as2) => {
|
---|
60 | for (const [key, value1] of as2) {
|
---|
61 | const value2 = as1.get(key);
|
---|
62 | if (!value2 || value1 > value2) as1.set(key, value1);
|
---|
63 | }
|
---|
64 | };
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * @param {OutputFileSystem} fs filesystem
|
---|
68 | * @param {string} outputPath output path
|
---|
69 | * @param {Map<string, number>} currentAssets filename of the current assets (must not start with .. or ., must only use / as path separator)
|
---|
70 | * @param {function((Error | null)=, Set<string>=): void} callback returns the filenames of the assets that shouldn't be there
|
---|
71 | * @returns {void}
|
---|
72 | */
|
---|
73 | const getDiffToFs = (fs, outputPath, currentAssets, callback) => {
|
---|
74 | const directories = new Set();
|
---|
75 | // get directories of assets
|
---|
76 | for (const [asset] of currentAssets) {
|
---|
77 | directories.add(asset.replace(/(^|\/)[^/]*$/, ""));
|
---|
78 | }
|
---|
79 | // and all parent directories
|
---|
80 | for (const directory of directories) {
|
---|
81 | directories.add(directory.replace(/(^|\/)[^/]*$/, ""));
|
---|
82 | }
|
---|
83 | const diff = new Set();
|
---|
84 | asyncLib.forEachLimit(
|
---|
85 | directories,
|
---|
86 | 10,
|
---|
87 | (directory, callback) => {
|
---|
88 | /** @type {NonNullable<OutputFileSystem["readdir"]>} */
|
---|
89 | (fs.readdir)(join(fs, outputPath, directory), (err, entries) => {
|
---|
90 | if (err) {
|
---|
91 | if (err.code === "ENOENT") return callback();
|
---|
92 | if (err.code === "ENOTDIR") {
|
---|
93 | diff.add(directory);
|
---|
94 | return callback();
|
---|
95 | }
|
---|
96 | return callback(err);
|
---|
97 | }
|
---|
98 | for (const entry of /** @type {string[]} */ (entries)) {
|
---|
99 | const file = entry;
|
---|
100 | const filename = directory ? `${directory}/${file}` : file;
|
---|
101 | if (!directories.has(filename) && !currentAssets.has(filename)) {
|
---|
102 | diff.add(filename);
|
---|
103 | }
|
---|
104 | }
|
---|
105 | callback();
|
---|
106 | });
|
---|
107 | },
|
---|
108 | err => {
|
---|
109 | if (err) return callback(err);
|
---|
110 |
|
---|
111 | callback(null, diff);
|
---|
112 | }
|
---|
113 | );
|
---|
114 | };
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * @param {Assets} currentAssets assets list
|
---|
118 | * @param {Assets} oldAssets old assets list
|
---|
119 | * @returns {Set<string>} diff
|
---|
120 | */
|
---|
121 | const getDiffToOldAssets = (currentAssets, oldAssets) => {
|
---|
122 | const diff = new Set();
|
---|
123 | const now = Date.now();
|
---|
124 | for (const [asset, ts] of oldAssets) {
|
---|
125 | if (ts >= now) continue;
|
---|
126 | if (!currentAssets.has(asset)) diff.add(asset);
|
---|
127 | }
|
---|
128 | return diff;
|
---|
129 | };
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * @param {OutputFileSystem} fs filesystem
|
---|
133 | * @param {string} filename path to file
|
---|
134 | * @param {StatsCallback} callback callback for provided filename
|
---|
135 | * @returns {void}
|
---|
136 | */
|
---|
137 | const doStat = (fs, filename, callback) => {
|
---|
138 | if ("lstat" in fs) {
|
---|
139 | /** @type {NonNullable<OutputFileSystem["lstat"]>} */
|
---|
140 | (fs.lstat)(filename, callback);
|
---|
141 | } else {
|
---|
142 | fs.stat(filename, callback);
|
---|
143 | }
|
---|
144 | };
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * @param {OutputFileSystem} fs filesystem
|
---|
148 | * @param {string} outputPath output path
|
---|
149 | * @param {boolean} dry only log instead of fs modification
|
---|
150 | * @param {Logger} logger logger
|
---|
151 | * @param {Set<string>} diff filenames of the assets that shouldn't be there
|
---|
152 | * @param {function(string): boolean | void} isKept check if the entry is ignored
|
---|
153 | * @param {function(Error=, Assets=): void} callback callback
|
---|
154 | * @returns {void}
|
---|
155 | */
|
---|
156 | const applyDiff = (fs, outputPath, dry, logger, diff, isKept, callback) => {
|
---|
157 | /**
|
---|
158 | * @param {string} msg message
|
---|
159 | */
|
---|
160 | const log = msg => {
|
---|
161 | if (dry) {
|
---|
162 | logger.info(msg);
|
---|
163 | } else {
|
---|
164 | logger.log(msg);
|
---|
165 | }
|
---|
166 | };
|
---|
167 | /** @typedef {{ type: "check" | "unlink" | "rmdir", filename: string, parent: { remaining: number, job: Job } | undefined }} Job */
|
---|
168 | /** @type {Job[]} */
|
---|
169 | const jobs = Array.from(diff.keys(), filename => ({
|
---|
170 | type: "check",
|
---|
171 | filename,
|
---|
172 | parent: undefined
|
---|
173 | }));
|
---|
174 | /** @type {Assets} */
|
---|
175 | const keptAssets = new Map();
|
---|
176 | processAsyncTree(
|
---|
177 | jobs,
|
---|
178 | 10,
|
---|
179 | ({ type, filename, parent }, push, callback) => {
|
---|
180 | /**
|
---|
181 | * @param {Error & { code?: string }} err error
|
---|
182 | * @returns {void}
|
---|
183 | */
|
---|
184 | const handleError = err => {
|
---|
185 | if (err.code === "ENOENT") {
|
---|
186 | log(`${filename} was removed during cleaning by something else`);
|
---|
187 | handleParent();
|
---|
188 | return callback();
|
---|
189 | }
|
---|
190 | return callback(err);
|
---|
191 | };
|
---|
192 | const handleParent = () => {
|
---|
193 | if (parent && --parent.remaining === 0) push(parent.job);
|
---|
194 | };
|
---|
195 | const path = join(fs, outputPath, filename);
|
---|
196 | switch (type) {
|
---|
197 | case "check":
|
---|
198 | if (isKept(filename)) {
|
---|
199 | keptAssets.set(filename, 0);
|
---|
200 | // do not decrement parent entry as we don't want to delete the parent
|
---|
201 | log(`${filename} will be kept`);
|
---|
202 | return process.nextTick(callback);
|
---|
203 | }
|
---|
204 | doStat(fs, path, (err, stats) => {
|
---|
205 | if (err) return handleError(err);
|
---|
206 | if (!(/** @type {IStats} */ (stats).isDirectory())) {
|
---|
207 | push({
|
---|
208 | type: "unlink",
|
---|
209 | filename,
|
---|
210 | parent
|
---|
211 | });
|
---|
212 | return callback();
|
---|
213 | }
|
---|
214 |
|
---|
215 | /** @type {NonNullable<OutputFileSystem["readdir"]>} */
|
---|
216 | (fs.readdir)(path, (err, _entries) => {
|
---|
217 | if (err) return handleError(err);
|
---|
218 | /** @type {Job} */
|
---|
219 | const deleteJob = {
|
---|
220 | type: "rmdir",
|
---|
221 | filename,
|
---|
222 | parent
|
---|
223 | };
|
---|
224 | const entries = /** @type {string[]} */ (_entries);
|
---|
225 | if (entries.length === 0) {
|
---|
226 | push(deleteJob);
|
---|
227 | } else {
|
---|
228 | const parentToken = {
|
---|
229 | remaining: entries.length,
|
---|
230 | job: deleteJob
|
---|
231 | };
|
---|
232 | for (const entry of entries) {
|
---|
233 | const file = /** @type {string} */ (entry);
|
---|
234 | if (file.startsWith(".")) {
|
---|
235 | log(
|
---|
236 | `${filename} will be kept (dot-files will never be removed)`
|
---|
237 | );
|
---|
238 | continue;
|
---|
239 | }
|
---|
240 | push({
|
---|
241 | type: "check",
|
---|
242 | filename: `${filename}/${file}`,
|
---|
243 | parent: parentToken
|
---|
244 | });
|
---|
245 | }
|
---|
246 | }
|
---|
247 | return callback();
|
---|
248 | });
|
---|
249 | });
|
---|
250 | break;
|
---|
251 | case "rmdir":
|
---|
252 | log(`${filename} will be removed`);
|
---|
253 | if (dry) {
|
---|
254 | handleParent();
|
---|
255 | return process.nextTick(callback);
|
---|
256 | }
|
---|
257 | if (!fs.rmdir) {
|
---|
258 | logger.warn(
|
---|
259 | `${filename} can't be removed because output file system doesn't support removing directories (rmdir)`
|
---|
260 | );
|
---|
261 | return process.nextTick(callback);
|
---|
262 | }
|
---|
263 | fs.rmdir(path, err => {
|
---|
264 | if (err) return handleError(err);
|
---|
265 | handleParent();
|
---|
266 | callback();
|
---|
267 | });
|
---|
268 | break;
|
---|
269 | case "unlink":
|
---|
270 | log(`${filename} will be removed`);
|
---|
271 | if (dry) {
|
---|
272 | handleParent();
|
---|
273 | return process.nextTick(callback);
|
---|
274 | }
|
---|
275 | if (!fs.unlink) {
|
---|
276 | logger.warn(
|
---|
277 | `${filename} can't be removed because output file system doesn't support removing files (rmdir)`
|
---|
278 | );
|
---|
279 | return process.nextTick(callback);
|
---|
280 | }
|
---|
281 | fs.unlink(path, err => {
|
---|
282 | if (err) return handleError(err);
|
---|
283 | handleParent();
|
---|
284 | callback();
|
---|
285 | });
|
---|
286 | break;
|
---|
287 | }
|
---|
288 | },
|
---|
289 | err => {
|
---|
290 | if (err) return callback(err);
|
---|
291 | callback(undefined, keptAssets);
|
---|
292 | }
|
---|
293 | );
|
---|
294 | };
|
---|
295 |
|
---|
296 | /** @type {WeakMap<Compilation, CleanPluginCompilationHooks>} */
|
---|
297 | const compilationHooksMap = new WeakMap();
|
---|
298 |
|
---|
299 | class CleanPlugin {
|
---|
300 | /**
|
---|
301 | * @param {Compilation} compilation the compilation
|
---|
302 | * @returns {CleanPluginCompilationHooks} the attached hooks
|
---|
303 | */
|
---|
304 | static getCompilationHooks(compilation) {
|
---|
305 | if (!(compilation instanceof Compilation)) {
|
---|
306 | throw new TypeError(
|
---|
307 | "The 'compilation' argument must be an instance of Compilation"
|
---|
308 | );
|
---|
309 | }
|
---|
310 | let hooks = compilationHooksMap.get(compilation);
|
---|
311 | if (hooks === undefined) {
|
---|
312 | hooks = {
|
---|
313 | keep: new SyncBailHook(["ignore"])
|
---|
314 | };
|
---|
315 | compilationHooksMap.set(compilation, hooks);
|
---|
316 | }
|
---|
317 | return hooks;
|
---|
318 | }
|
---|
319 |
|
---|
320 | /** @param {CleanOptions} options options */
|
---|
321 | constructor(options = {}) {
|
---|
322 | validate(options);
|
---|
323 | this.options = { dry: false, ...options };
|
---|
324 | }
|
---|
325 |
|
---|
326 | /**
|
---|
327 | * Apply the plugin
|
---|
328 | * @param {Compiler} compiler the compiler instance
|
---|
329 | * @returns {void}
|
---|
330 | */
|
---|
331 | apply(compiler) {
|
---|
332 | const { dry, keep } = this.options;
|
---|
333 |
|
---|
334 | /** @type {KeepFn} */
|
---|
335 | const keepFn =
|
---|
336 | typeof keep === "function"
|
---|
337 | ? keep
|
---|
338 | : typeof keep === "string"
|
---|
339 | ? path => path.startsWith(keep)
|
---|
340 | : typeof keep === "object" && keep.test
|
---|
341 | ? path => keep.test(path)
|
---|
342 | : () => false;
|
---|
343 |
|
---|
344 | // We assume that no external modification happens while the compiler is active
|
---|
345 | // So we can store the old assets and only diff to them to avoid fs access on
|
---|
346 | // incremental builds
|
---|
347 | /** @type {undefined|Assets} */
|
---|
348 | let oldAssets;
|
---|
349 |
|
---|
350 | compiler.hooks.emit.tapAsync(
|
---|
351 | {
|
---|
352 | name: "CleanPlugin",
|
---|
353 | stage: 100
|
---|
354 | },
|
---|
355 | (compilation, callback) => {
|
---|
356 | const hooks = CleanPlugin.getCompilationHooks(compilation);
|
---|
357 | const logger = compilation.getLogger("webpack.CleanPlugin");
|
---|
358 | const fs = /** @type {OutputFileSystem} */ (compiler.outputFileSystem);
|
---|
359 |
|
---|
360 | if (!fs.readdir) {
|
---|
361 | return callback(
|
---|
362 | new Error(
|
---|
363 | "CleanPlugin: Output filesystem doesn't support listing directories (readdir)"
|
---|
364 | )
|
---|
365 | );
|
---|
366 | }
|
---|
367 |
|
---|
368 | /** @type {Assets} */
|
---|
369 | const currentAssets = new Map();
|
---|
370 | const now = Date.now();
|
---|
371 | for (const asset of Object.keys(compilation.assets)) {
|
---|
372 | if (/^[A-Za-z]:\\|^\/|^\\\\/.test(asset)) continue;
|
---|
373 | let normalizedAsset;
|
---|
374 | let newNormalizedAsset = asset.replace(/\\/g, "/");
|
---|
375 | do {
|
---|
376 | normalizedAsset = newNormalizedAsset;
|
---|
377 | newNormalizedAsset = normalizedAsset.replace(
|
---|
378 | /(^|\/)(?!\.\.)[^/]+\/\.\.\//g,
|
---|
379 | "$1"
|
---|
380 | );
|
---|
381 | } while (newNormalizedAsset !== normalizedAsset);
|
---|
382 | if (normalizedAsset.startsWith("../")) continue;
|
---|
383 | const assetInfo = compilation.assetsInfo.get(asset);
|
---|
384 | if (assetInfo && assetInfo.hotModuleReplacement) {
|
---|
385 | currentAssets.set(normalizedAsset, now + _10sec);
|
---|
386 | } else {
|
---|
387 | currentAssets.set(normalizedAsset, 0);
|
---|
388 | }
|
---|
389 | }
|
---|
390 |
|
---|
391 | const outputPath = compilation.getPath(compiler.outputPath, {});
|
---|
392 |
|
---|
393 | /**
|
---|
394 | * @param {string} path path
|
---|
395 | * @returns {boolean | void} true, if needs to be kept
|
---|
396 | */
|
---|
397 | const isKept = path => {
|
---|
398 | const result = hooks.keep.call(path);
|
---|
399 | if (result !== undefined) return result;
|
---|
400 | return keepFn(path);
|
---|
401 | };
|
---|
402 |
|
---|
403 | /**
|
---|
404 | * @param {(Error | null)=} err err
|
---|
405 | * @param {Set<string>=} diff diff
|
---|
406 | */
|
---|
407 | const diffCallback = (err, diff) => {
|
---|
408 | if (err) {
|
---|
409 | oldAssets = undefined;
|
---|
410 | callback(err);
|
---|
411 | return;
|
---|
412 | }
|
---|
413 | applyDiff(
|
---|
414 | fs,
|
---|
415 | outputPath,
|
---|
416 | dry,
|
---|
417 | logger,
|
---|
418 | /** @type {Set<string>} */ (diff),
|
---|
419 | isKept,
|
---|
420 | (err, keptAssets) => {
|
---|
421 | if (err) {
|
---|
422 | oldAssets = undefined;
|
---|
423 | } else {
|
---|
424 | if (oldAssets) mergeAssets(currentAssets, oldAssets);
|
---|
425 | oldAssets = currentAssets;
|
---|
426 | if (keptAssets) mergeAssets(oldAssets, keptAssets);
|
---|
427 | }
|
---|
428 | callback(err);
|
---|
429 | }
|
---|
430 | );
|
---|
431 | };
|
---|
432 |
|
---|
433 | if (oldAssets) {
|
---|
434 | diffCallback(null, getDiffToOldAssets(currentAssets, oldAssets));
|
---|
435 | } else {
|
---|
436 | getDiffToFs(fs, outputPath, currentAssets, diffCallback);
|
---|
437 | }
|
---|
438 | }
|
---|
439 | );
|
---|
440 | }
|
---|
441 | }
|
---|
442 |
|
---|
443 | module.exports = CleanPlugin;
|
---|