{"version":3,"sources":["../src/clean-webpack-plugin.ts"],"names":["isPlainObject","value","Object","prototype","toString","call","getPrototypeOf","CleanWebpackPlugin","constructor","options","Error","allowExternal","dangerouslyAllowCleanPatternsOutsideProject","dry","console","warn","verbose","cleanStaleWebpackAssets","protectWebpackAssets","cleanAfterEveryBuildPatterns","Array","isArray","cleanOnceBeforeBuildPatterns","currentAssets","initialClean","outputPath","apply","bind","handleInitial","handleDone","removeFiles","compiler","output","path","hooks","length","emit","tap","compilation","done","stats","getStats","hasErrors","assetList","keys","assets","staleFiles","filter","previousAsset","assetCurrent","includes","sort","removePatterns","push","patterns","deleted","force","cwd","dryRun","dot","ignore","forEach","file","filename","relative","process","message","error","needsForce","test"],"mappings":";;;;;;;AAAA;;AACA;;;;AA+DA;AACA,SAASA,aAAT,CAAuBC,KAAvB,EAAgD;AAC5C,MAAIC,MAAM,CAACC,SAAP,CAAiBC,QAAjB,CAA0BC,IAA1B,CAA+BJ,KAA/B,MAA0C,iBAA9C,EAAiE;AAC7D,WAAO,KAAP;AACH;;AAED,QAAME,SAAS,GAAGD,MAAM,CAACI,cAAP,CAAsBL,KAAtB,CAAlB;AACA,SAAOE,SAAS,KAAK,IAAd,IAAsBA,SAAS,KAAKD,MAAM,CAACI,cAAP,CAAsB,EAAtB,CAA3C;AACH;;AAED,MAAMC,kBAAN,CAAyB;AAYrBC,EAAAA,WAAW,CAACC,OAAgB,GAAG,EAApB,EAAwB;AAC/B,QAAIT,aAAa,CAACS,OAAD,CAAb,KAA2B,KAA/B,EAAsC;AAClC,YAAM,IAAIC,KAAJ,CAAW;AAC7B,2FADkB,CAAN;AAEH,KAJ8B,CAM/B;;;AACA,QAAID,OAAO,CAACE,aAAZ,EAA2B;AACvB,YAAM,IAAID,KAAJ,CACF,qHADE,CAAN;AAGH;;AAED,QACID,OAAO,CAACG,2CAAR,KAAwD,IAAxD,IACAH,OAAO,CAACI,GAAR,KAAgB,IADhB,IAEAJ,OAAO,CAACI,GAAR,KAAgB,KAH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QAAAA,MAAM,EAAE,KAAK7C,GAJiB;AAK9B8C,QAAAA,GAAG,EAAE,IALyB;AAM9BC,QAAAA,MAAM,EAAE,KAAK1C,oBAAL,GAA4B,KAAKK,aAAjC,GAAiD;AAN3B,OAAlB,CAAhB;AASA;AACZ;AACA;;AACY,UAAI,KAAKP,OAAT,EAAkB;AACduC,QAAAA,OAAO,CAACM,OAAR,CAAiBC,IAAD,IAAU;AACtB,gBAAMC,QAAQ,GAAG9B,cAAK+B,QAAL,CAAcC,OAAO,CAACR,GAAR,EAAd,EAA6BK,IAA7B,CAAjB;;AAEA,gBAAMI,OAAO,GAAG,KAAKrD,GAAL,GAAW,KAAX,GAAmB,SAAnC;AAEA;AACpB;AACA;AACA;AACA;AACoB;;AACAC,UAAAA,OAAO,CAACC,IAAR,CACK,yBAAwBmD,OAAQ,IAAGH,QAAS,EADjD;AAGH,SAdD;AAeH;AACJ,KA9BD,CA8BE,OAAOI,KAAP,EAAc;AACZ,YAAMC,UAAU,GAAG,uEAAuEC,IAAvE,CACfF,KAAK,CAACD,OADS,CAAnB;;AAIA,UAAIE,UAAJ,EAAgB;AACZ,cAAMF,OAAO,GACT,2KADJ;AAGA,cAAM,IAAIxD,KAAJ,CAAUwD,OAAV,CAAN;AACH;AAED;;;AACA,YAAMC,KAAN;AACH;AACJ;;AA3PoB","sourcesContent":["import { sync as delSync } from 'del';\nimport path from 'path';\nimport { Compilation, Compiler, Stats } from 'webpack';\n\nexport interface Options {\n /**\n * Simulate the removal of files\n *\n * default: false\n */\n dry?: boolean;\n\n /**\n * Write Logs to Console\n * (Always enabled when dry is true)\n *\n * default: false\n */\n verbose?: boolean;\n\n /**\n * Automatically remove all unused webpack assets on rebuild\n *\n * default: true\n */\n cleanStaleWebpackAssets?: boolean;\n\n /**\n * Do not allow removal of current webpack assets\n *\n * default: true\n */\n protectWebpackAssets?: boolean;\n\n /**\n * Removes files once prior to Webpack compilation\n * Not included in rebuilds (watch mode)\n *\n * Use !negative patterns to exclude files\n *\n * default: ['**\\/*']\n */\n cleanOnceBeforeBuildPatterns?: string[];\n\n /**\n * Removes files after every build (including watch mode) that match this pattern.\n * Used for files that are not created directly by Webpack.\n *\n * Use !negative patterns to exclude files\n *\n * default: []\n */\n cleanAfterEveryBuildPatterns?: string[];\n\n /**\n * Allow clean patterns outside of process.cwd()\n *\n * requires dry option to be explicitly set\n *\n * default: false\n */\n dangerouslyAllowCleanPatternsOutsideProject?: boolean;\n}\n\n// Copied from https://github.com/sindresorhus/is-plain-obj/blob/97480673cf12145b32ec2ee924980d66572e8a86/index.js\nfunction isPlainObject(value: unknown): boolean {\n if (Object.prototype.toString.call(value) !== '[object Object]') {\n return false;\n }\n\n const prototype = Object.getPrototypeOf(value);\n return prototype === null || prototype === Object.getPrototypeOf({});\n}\n\nclass CleanWebpackPlugin {\n private readonly dry: boolean;\n private readonly verbose: boolean;\n private readonly cleanStaleWebpackAssets: boolean;\n private readonly protectWebpackAssets: boolean;\n private readonly cleanAfterEveryBuildPatterns: string[];\n private readonly cleanOnceBeforeBuildPatterns: string[];\n private readonly dangerouslyAllowCleanPatternsOutsideProject: boolean;\n private currentAssets: string[];\n private initialClean: boolean;\n private outputPath: string;\n\n constructor(options: Options = {}) {\n if (isPlainObject(options) === false) {\n throw new Error(`clean-webpack-plugin only accepts an options object. See:\n https://github.com/johnagan/clean-webpack-plugin#options-and-defaults-optional`);\n }\n\n // @ts-ignore\n if (options.allowExternal) {\n throw new Error(\n 'clean-webpack-plugin: `allowExternal` option no longer supported. Use `dangerouslyAllowCleanPatternsOutsideProject`',\n );\n }\n\n if (\n options.dangerouslyAllowCleanPatternsOutsideProject === true &&\n options.dry !== true &&\n options.dry !== false\n ) {\n // eslint-disable-next-line no-console\n console.warn(\n 'clean-webpack-plugin: dangerouslyAllowCleanPatternsOutsideProject requires dry: false to be explicitly set. Enabling dry mode',\n );\n }\n\n this.dangerouslyAllowCleanPatternsOutsideProject =\n options.dangerouslyAllowCleanPatternsOutsideProject === true ||\n false;\n\n this.dry =\n options.dry === true || options.dry === false\n ? options.dry\n : this.dangerouslyAllowCleanPatternsOutsideProject === true ||\n false;\n\n this.verbose = this.dry === true || options.verbose === true || false;\n\n this.cleanStaleWebpackAssets =\n options.cleanStaleWebpackAssets === true ||\n options.cleanStaleWebpackAssets === false\n ? options.cleanStaleWebpackAssets\n : true;\n\n this.protectWebpackAssets =\n options.protectWebpackAssets === true ||\n options.protectWebpackAssets === false\n ? options.protectWebpackAssets\n : true;\n\n this.cleanAfterEveryBuildPatterns = Array.isArray(\n options.cleanAfterEveryBuildPatterns,\n )\n ? options.cleanAfterEveryBuildPatterns\n : [];\n\n this.cleanOnceBeforeBuildPatterns = Array.isArray(\n options.cleanOnceBeforeBuildPatterns,\n )\n ? options.cleanOnceBeforeBuildPatterns\n : ['**/*'];\n\n /**\n * Store webpack build assets\n */\n this.currentAssets = [];\n\n /**\n * Only used with cleanOnceBeforeBuildPatterns\n */\n this.initialClean = false;\n\n this.outputPath = '';\n\n this.apply = this.apply.bind(this);\n this.handleInitial = this.handleInitial.bind(this);\n this.handleDone = this.handleDone.bind(this);\n this.removeFiles = this.removeFiles.bind(this);\n }\n\n apply(compiler: Compiler) {\n if (!compiler.options.output || !compiler.options.output.path) {\n // eslint-disable-next-line no-console\n console.warn(\n 'clean-webpack-plugin: options.output.path not defined. Plugin disabled...',\n );\n\n return;\n }\n\n this.outputPath = compiler.options.output.path;\n\n /**\n * webpack 4+ comes with a new plugin system.\n *\n * Check for hooks in-order to support old plugin system\n * webpack 5+ removed the old system, the check now breaks\n */\n const hooks = compiler.hooks;\n\n if (this.cleanOnceBeforeBuildPatterns.length !== 0) {\n hooks.emit.tap('clean-webpack-plugin', (compilation) => {\n this.handleInitial(compilation);\n });\n }\n\n hooks.done.tap('clean-webpack-plugin', (stats) => {\n this.handleDone(stats);\n });\n }\n\n /**\n * Initially remove files from output directory prior to build.\n *\n * Only happens once.\n *\n * Warning: It is recommended to initially clean your build directory outside of webpack to minimize unexpected behavior.\n */\n handleInitial(compilation: Compilation) {\n if (this.initialClean) {\n return;\n }\n\n /**\n * Do not remove files if there are compilation errors\n *\n * Handle logging inside this.handleDone\n */\n const stats = compilation.getStats();\n if (stats.hasErrors()) {\n return;\n }\n\n this.initialClean = true;\n\n this.removeFiles(this.cleanOnceBeforeBuildPatterns);\n }\n\n handleDone(stats: Stats) {\n /**\n * Do nothing if there is a webpack error\n */\n if (stats.hasErrors()) {\n if (this.verbose) {\n // eslint-disable-next-line no-console\n console.warn(\n 'clean-webpack-plugin: pausing due to webpack errors',\n );\n }\n\n return;\n }\n\n /**\n * Fetch Webpack's output asset files\n */\n const assetList = Object.keys(stats.compilation.assets);\n\n /**\n * Get all files that were in the previous build but not the current\n *\n * (relies on del's cwd: outputPath option)\n */\n const staleFiles = this.currentAssets.filter((previousAsset) => {\n const assetCurrent = assetList.includes(previousAsset) === false;\n\n return assetCurrent;\n });\n\n /**\n * Save assets for next compilation\n */\n this.currentAssets = assetList.sort();\n\n const removePatterns = [];\n\n /**\n * Remove unused webpack assets\n */\n if (this.cleanStaleWebpackAssets === true && staleFiles.length !== 0) {\n removePatterns.push(...staleFiles);\n }\n\n /**\n * Remove cleanAfterEveryBuildPatterns\n */\n if (this.cleanAfterEveryBuildPatterns.length !== 0) {\n removePatterns.push(...this.cleanAfterEveryBuildPatterns);\n }\n\n if (removePatterns.length !== 0) {\n this.removeFiles(removePatterns);\n }\n }\n\n removeFiles(patterns: string[]) {\n try {\n const deleted = delSync(patterns, {\n force: this.dangerouslyAllowCleanPatternsOutsideProject,\n // Change context to build directory\n cwd: this.outputPath,\n dryRun: this.dry,\n dot: true,\n ignore: this.protectWebpackAssets ? this.currentAssets : [],\n });\n\n /**\n * Log if verbose is enabled\n */\n if (this.verbose) {\n deleted.forEach((file) => {\n const filename = path.relative(process.cwd(), file);\n\n const message = this.dry ? 'dry' : 'removed';\n\n /**\n * Use console.warn over .log\n * https://github.com/webpack/webpack/issues/1904\n * https://github.com/johnagan/clean-webpack-plugin/issues/11\n */\n // eslint-disable-next-line no-console\n console.warn(\n `clean-webpack-plugin: ${message} ${filename}`,\n );\n });\n }\n } catch (error) {\n const needsForce = /Cannot delete files\\/folders outside the current working directory\\./.test(\n error.message,\n );\n\n if (needsForce) {\n const message =\n 'clean-webpack-plugin: Cannot delete files/folders outside the current working directory. Can be overridden with the `dangerouslyAllowCleanPatternsOutsideProject` option.';\n\n throw new Error(message);\n }\n\n /* istanbul ignore next */\n throw error;\n }\n }\n}\n\nexport { CleanWebpackPlugin };\n"],"file":"clean-webpack-plugin.js"}