CleanPlugin.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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("../lib/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").OutputFileSystem} OutputFileSystem */
  16. /** @typedef {import("./util/fs").StatsCallback} StatsCallback */
  17. /** @typedef {(function(string):boolean)|RegExp} IgnoreItem */
  18. /** @typedef {function(IgnoreItem): void} AddToIgnoreCallback */
  19. /**
  20. * @typedef {Object} CleanPluginCompilationHooks
  21. * @property {SyncBailHook<[string], boolean>} 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. const validate = createSchemaValidation(
  24. undefined,
  25. () => {
  26. const { definitions } = require("../schemas/WebpackOptions.json");
  27. return {
  28. definitions,
  29. oneOf: [{ $ref: "#/definitions/CleanOptions" }]
  30. };
  31. },
  32. {
  33. name: "Clean Plugin",
  34. baseDataPath: "options"
  35. }
  36. );
  37. /**
  38. * @param {OutputFileSystem} fs filesystem
  39. * @param {string} outputPath output path
  40. * @param {Set<string>} currentAssets filename of the current assets (must not start with .. or ., must only use / as path separator)
  41. * @param {function(Error=, Set<string>=): void} callback returns the filenames of the assets that shouldn't be there
  42. * @returns {void}
  43. */
  44. const getDiffToFs = (fs, outputPath, currentAssets, callback) => {
  45. const directories = new Set();
  46. // get directories of assets
  47. for (const asset of currentAssets) {
  48. directories.add(asset.replace(/(^|\/)[^/]*$/, ""));
  49. }
  50. // and all parent directories
  51. for (const directory of directories) {
  52. directories.add(directory.replace(/(^|\/)[^/]*$/, ""));
  53. }
  54. const diff = new Set();
  55. asyncLib.forEachLimit(
  56. directories,
  57. 10,
  58. (directory, callback) => {
  59. fs.readdir(join(fs, outputPath, directory), (err, entries) => {
  60. if (err) {
  61. if (err.code === "ENOENT") return callback();
  62. if (err.code === "ENOTDIR") {
  63. diff.add(directory);
  64. return callback();
  65. }
  66. return callback(err);
  67. }
  68. for (const entry of entries) {
  69. const file = /** @type {string} */ (entry);
  70. const filename = directory ? `${directory}/${file}` : file;
  71. if (!directories.has(filename) && !currentAssets.has(filename)) {
  72. diff.add(filename);
  73. }
  74. }
  75. callback();
  76. });
  77. },
  78. err => {
  79. if (err) return callback(err);
  80. callback(null, diff);
  81. }
  82. );
  83. };
  84. /**
  85. * @param {Set<string>} currentAssets assets list
  86. * @param {Set<string>} oldAssets old assets list
  87. * @returns {Set<string>} diff
  88. */
  89. const getDiffToOldAssets = (currentAssets, oldAssets) => {
  90. const diff = new Set();
  91. for (const asset of oldAssets) {
  92. if (!currentAssets.has(asset)) diff.add(asset);
  93. }
  94. return diff;
  95. };
  96. /**
  97. * @param {OutputFileSystem} fs filesystem
  98. * @param {string} filename path to file
  99. * @param {StatsCallback} callback callback for provided filename
  100. * @returns {void}
  101. */
  102. const doStat = (fs, filename, callback) => {
  103. if ("lstat" in fs) {
  104. fs.lstat(filename, callback);
  105. } else {
  106. fs.stat(filename, callback);
  107. }
  108. };
  109. /**
  110. * @param {OutputFileSystem} fs filesystem
  111. * @param {string} outputPath output path
  112. * @param {boolean} dry only log instead of fs modification
  113. * @param {Logger} logger logger
  114. * @param {Set<string>} diff filenames of the assets that shouldn't be there
  115. * @param {function(string): boolean} isKept check if the entry is ignored
  116. * @param {function(Error=): void} callback callback
  117. * @returns {void}
  118. */
  119. const applyDiff = (fs, outputPath, dry, logger, diff, isKept, callback) => {
  120. const log = msg => {
  121. if (dry) {
  122. logger.info(msg);
  123. } else {
  124. logger.log(msg);
  125. }
  126. };
  127. /** @typedef {{ type: "check" | "unlink" | "rmdir", filename: string, parent: { remaining: number, job: Job } | undefined }} Job */
  128. /** @type {Job[]} */
  129. const jobs = Array.from(diff, filename => ({
  130. type: "check",
  131. filename,
  132. parent: undefined
  133. }));
  134. processAsyncTree(
  135. jobs,
  136. 10,
  137. ({ type, filename, parent }, push, callback) => {
  138. const handleError = err => {
  139. if (err.code === "ENOENT") {
  140. log(`${filename} was removed during cleaning by something else`);
  141. handleParent();
  142. return callback();
  143. }
  144. return callback(err);
  145. };
  146. const handleParent = () => {
  147. if (parent && --parent.remaining === 0) push(parent.job);
  148. };
  149. const path = join(fs, outputPath, filename);
  150. switch (type) {
  151. case "check":
  152. if (isKept(filename)) {
  153. // do not decrement parent entry as we don't want to delete the parent
  154. log(`${filename} will be kept`);
  155. return process.nextTick(callback);
  156. }
  157. doStat(fs, path, (err, stats) => {
  158. if (err) return handleError(err);
  159. if (!stats.isDirectory()) {
  160. push({
  161. type: "unlink",
  162. filename,
  163. parent
  164. });
  165. return callback();
  166. }
  167. fs.readdir(path, (err, entries) => {
  168. if (err) return handleError(err);
  169. /** @type {Job} */
  170. const deleteJob = {
  171. type: "rmdir",
  172. filename,
  173. parent
  174. };
  175. if (entries.length === 0) {
  176. push(deleteJob);
  177. } else {
  178. const parentToken = {
  179. remaining: entries.length,
  180. job: deleteJob
  181. };
  182. for (const entry of entries) {
  183. const file = /** @type {string} */ (entry);
  184. if (file.startsWith(".")) {
  185. log(
  186. `${filename} will be kept (dot-files will never be removed)`
  187. );
  188. continue;
  189. }
  190. push({
  191. type: "check",
  192. filename: `${filename}/${file}`,
  193. parent: parentToken
  194. });
  195. }
  196. }
  197. return callback();
  198. });
  199. });
  200. break;
  201. case "rmdir":
  202. log(`${filename} will be removed`);
  203. if (dry) {
  204. handleParent();
  205. return process.nextTick(callback);
  206. }
  207. if (!fs.rmdir) {
  208. logger.warn(
  209. `${filename} can't be removed because output file system doesn't support removing directories (rmdir)`
  210. );
  211. return process.nextTick(callback);
  212. }
  213. fs.rmdir(path, err => {
  214. if (err) return handleError(err);
  215. handleParent();
  216. callback();
  217. });
  218. break;
  219. case "unlink":
  220. log(`${filename} will be removed`);
  221. if (dry) {
  222. handleParent();
  223. return process.nextTick(callback);
  224. }
  225. if (!fs.unlink) {
  226. logger.warn(
  227. `${filename} can't be removed because output file system doesn't support removing files (rmdir)`
  228. );
  229. return process.nextTick(callback);
  230. }
  231. fs.unlink(path, err => {
  232. if (err) return handleError(err);
  233. handleParent();
  234. callback();
  235. });
  236. break;
  237. }
  238. },
  239. callback
  240. );
  241. };
  242. /** @type {WeakMap<Compilation, CleanPluginCompilationHooks>} */
  243. const compilationHooksMap = new WeakMap();
  244. class CleanPlugin {
  245. /**
  246. * @param {Compilation} compilation the compilation
  247. * @returns {CleanPluginCompilationHooks} the attached hooks
  248. */
  249. static getCompilationHooks(compilation) {
  250. if (!(compilation instanceof Compilation)) {
  251. throw new TypeError(
  252. "The 'compilation' argument must be an instance of Compilation"
  253. );
  254. }
  255. let hooks = compilationHooksMap.get(compilation);
  256. if (hooks === undefined) {
  257. hooks = {
  258. /** @type {SyncBailHook<[string], boolean>} */
  259. keep: new SyncBailHook(["ignore"])
  260. };
  261. compilationHooksMap.set(compilation, hooks);
  262. }
  263. return hooks;
  264. }
  265. /** @param {CleanOptions} options options */
  266. constructor(options = {}) {
  267. validate(options);
  268. this.options = { dry: false, ...options };
  269. }
  270. /**
  271. * Apply the plugin
  272. * @param {Compiler} compiler the compiler instance
  273. * @returns {void}
  274. */
  275. apply(compiler) {
  276. const { dry, keep } = this.options;
  277. const keepFn =
  278. typeof keep === "function"
  279. ? keep
  280. : typeof keep === "string"
  281. ? path => path.startsWith(keep)
  282. : typeof keep === "object" && keep.test
  283. ? path => keep.test(path)
  284. : () => false;
  285. // We assume that no external modification happens while the compiler is active
  286. // So we can store the old assets and only diff to them to avoid fs access on
  287. // incremental builds
  288. let oldAssets;
  289. compiler.hooks.emit.tapAsync(
  290. {
  291. name: "CleanPlugin",
  292. stage: 100
  293. },
  294. (compilation, callback) => {
  295. const hooks = CleanPlugin.getCompilationHooks(compilation);
  296. const logger = compilation.getLogger("webpack.CleanPlugin");
  297. const fs = compiler.outputFileSystem;
  298. if (!fs.readdir) {
  299. return callback(
  300. new Error(
  301. "CleanPlugin: Output filesystem doesn't support listing directories (readdir)"
  302. )
  303. );
  304. }
  305. const currentAssets = new Set();
  306. for (const asset of Object.keys(compilation.assets)) {
  307. if (/^[A-Za-z]:\\|^\/|^\\\\/.test(asset)) continue;
  308. let normalizedAsset;
  309. let newNormalizedAsset = asset.replace(/\\/g, "/");
  310. do {
  311. normalizedAsset = newNormalizedAsset;
  312. newNormalizedAsset = normalizedAsset.replace(
  313. /(^|\/)(?!\.\.)[^/]+\/\.\.\//g,
  314. "$1"
  315. );
  316. } while (newNormalizedAsset !== normalizedAsset);
  317. if (normalizedAsset.startsWith("../")) continue;
  318. currentAssets.add(normalizedAsset);
  319. }
  320. const outputPath = compilation.getPath(compiler.outputPath, {});
  321. const isKept = path => {
  322. const result = hooks.keep.call(path);
  323. if (result !== undefined) return result;
  324. return keepFn(path);
  325. };
  326. const diffCallback = (err, diff) => {
  327. if (err) {
  328. oldAssets = undefined;
  329. return callback(err);
  330. }
  331. applyDiff(fs, outputPath, dry, logger, diff, isKept, err => {
  332. if (err) {
  333. oldAssets = undefined;
  334. } else {
  335. oldAssets = currentAssets;
  336. }
  337. callback(err);
  338. });
  339. };
  340. if (oldAssets) {
  341. diffCallback(null, getDiffToOldAssets(currentAssets, oldAssets));
  342. } else {
  343. getDiffToFs(fs, outputPath, currentAssets, diffCallback);
  344. }
  345. }
  346. );
  347. }
  348. }
  349. module.exports = CleanPlugin;