webpack-cleanup-plugin

raw JSON →
0.5.1 verified Sat Apr 25 auth: no javascript maintenance

Webpack plugin to clean up extraneous files from the output path after compilation. Version 0.5.1 is the latest stable release; the package is no longer actively maintained. It removes files not generated by the current webpack build, useful for production builds to avoid stale assets. Alternatives like clean-webpack-plugin or output.clean (webpack 5+) are preferred. Supports exclude patterns (minimatch globs), quiet mode, and preview mode to list files without deletion.

error Error: Cannot find module 'webpack-cleanup-plugin'
cause Package not installed or missing from node_modules.
fix
Run npm install webpack-cleanup-plugin --save-dev
error TypeError: WebpackCleanupPlugin is not a constructor
cause Incorrect import style; e.g. named import instead of default.
fix
Use const WebpackCleanupPlugin = require('webpack-cleanup-plugin'); or import WebpackCleanupPlugin from 'webpack-cleanup-plugin';
error Error: webpack.CleanupPlugin is not a function
cause Misunderstood as built-in webpack plugin.
fix
This plugin is separate: install and import webpack-cleanup-plugin.
breaking Plugin deletes files from output directory. Misconfigured exclude may delete important files.
fix Always use preview: true in development to verify files before deletion. Ensure exclude patterns cover non-webpack assets.
gotcha Plugin does not support webpack 5's output.clean option or hooks; may need additional configuration for webpack 5.
fix Consider using output.clean: true in webpack 5 or migrate to clean-webpack-plugin.
gotcha Exclude patterns use minimatch globbing; relative paths are relative to output path, not project root.
fix Use absolute patterns or ensure paths are relative to output.path.
npm install webpack-cleanup-plugin
yarn add webpack-cleanup-plugin
pnpm add webpack-cleanup-plugin

Shows minimal webpack config with webpack-cleanup-plugin that previews files to delete except excluded patterns.

const path = require('path');
const WebpackCleanupPlugin = require('webpack-cleanup-plugin');

module.exports = {
  output: {
    path: path.resolve(__dirname, 'dist'),
  },
  plugins: [
    new WebpackCleanupPlugin({
      exclude: ['stats.json', 'important.js', 'folder/**/*'],
      quiet: false,
      preview: true,
    }),
  ],
};