UNPKG

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