CleanPlugin.js 13 KB

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