CleanPlugin.js 12 KB

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