CleanPlugin.js 13 KB

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