CleanPlugin.js 14 KB

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