CleanPlugin.js 13 KB

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