clean-webpack-plugin is a utility designed to clean or remove build folders before or after webpack compilations. Its current stable version is 4.0.0, which supports Node.js 14+ and webpack 5+. The plugin typically sees major version updates to align with breaking changes in Node.js and webpack, ensuring compatibility with the latest ecosystem features. Key differentiators include its simplicity, with no configuration needed for standard usage (cleaning webpack's output.path directory), and its intelligent handling of webpack's watch mode where it only removes assets generated by webpack that are no longer in use. It leverages the 'del' package for robust globbing support, allowing for flexible pattern matching to define what should be cleaned. It is a fundamental tool for maintaining clean build directories in webpack projects.
npm install clean-webpack-pluginVerified import paths — ran on the pinned version, not inferred.
This configuration demonstrates the basic setup for `clean-webpack-plugin` in a webpack project. It initializes the plugin with its default settings, which automatically cleans the output directory before the initial build and manages stale assets during subsequent rebuilds, ensuring a clean build folder for a TypeScript project.
Upgrade Node.js to version 10 or higher and webpack to version 4 or higher, or use clean-webpack-plugin v3.x.
Update your import statements to `import { CleanWebpackPlugin } from 'clean-webpack-plugin';` for ESM or `const { CleanWebpackPlugin } = require('clean-webpack-plugin');` for CommonJS.Upgrade Node.js to version 8 or higher and webpack to version 3 or higher, or use clean-webpack-plugin v2.x.
Review build logs to ensure cleanup occurs at the expected phase. No code changes are typically required unless specific timing dependencies exist.
When cleaning directories outside of webpack's output path, use `path.join(process.cwd(), 'your/path/**/*')` to provide an absolute path.
Correct the import/require statement to use destructuring: `import { CleanWebpackPlugin } from 'clean-webpack-plugin';` or `const { CleanWebpackPlugin } = require('clean-webpack-plugin');`Ensure the package is installed (`npm install --save-dev clean-webpack-plugin`) and that your Node.js and webpack versions meet the plugin's requirements (Node.js 10+ and webpack 4+ for v4.0.0).
First, test with `dry: true` and `verbose: true` in the plugin options to log actions without actual file deletion. Review `cleanOnceBeforeBuildPatterns` to ensure glob patterns are correct and paths are absolute if outside `output.path`. Remember that by default, it cleans `output.path` before build and stale webpack assets during rebuilds.