remove-files-webpack-plugin

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

A webpack plugin for removing files and folders before and after compilation. Current stable version is 1.5.0, released 2021-04-12. Supports webpack >= 2.2.0 and Node >= 8.3.0. Ships TypeScript types. Key differentiators: supports before, after, and watch compilation modes; handles symbolic links as ordinary files; works on all OS with platform-specific path handling. Unlike clean-webpack-plugin, it allows granular control over which files to remove and supports watch mode cleanup. Development appears dormant since 2021.

error Error: The plugin must be constructed with an object containing 'before', 'watch', or 'after' namespaces.
cause Passing flat configuration like `new RemovePlugin({ include: ['dist'] })`.
fix
Wrap options: new RemovePlugin({ before: { include: ['dist'] } }).
error Cannot read property 'tapAsync' of undefined
cause Using an unsupported webpack version (e.g., webpack 5 without compatibility layer).
fix
Upgrade to webpack 5 compatible version of the plugin or use webpack 4.
error TypeError: Cannot read property 'root' of undefined
cause Omitting the options object entirely (e.g., `new RemovePlugin()`) or passing `null`.
fix
Provide at least one namespace: new RemovePlugin({ after: { include: [] } }).
gotcha Plugin configuration must be nested under `before`, `watch`, or `after` namespaces; flat configuration with `include` at root will be silently ignored.
fix Wrap options in the appropriate namespace: `new RemovePlugin({ before: { include: [...] } })`.
gotcha Paths in `include` and `exclude` are resolved relative to the plugin's `root` option (defaults to webpack's context, typically `.`). Do not assume they are relative to the project root.
fix Set the `root` option explicitly or use absolute paths.
breaking Version 1.4.0 changed the default root from the current working directory to the webpack context (process.cwd()). Projects relying on the old default may delete the wrong files.
fix Explicitly set `root: process.cwd()` if the previous behavior was desired.
deprecated The `allowRoot` option (default `false`) is deprecated and may be removed in a future major version.
fix Avoid using `allowRoot: true`; ensure `include` and `exclude` paths are subdirectories of `root`.
npm install remove-files-webpack-plugin
yarn add remove-files-webpack-plugin
pnpm add remove-files-webpack-plugin

Demonstrates removing the 'dist' folder before compilation and a 'temp' folder after compilation using the before and after namespaces.

const RemovePlugin = require('remove-files-webpack-plugin');

module.exports = {
  context: __dirname,
  entry: './src/index.js',
  output: { path: './dist', filename: 'bundle.js' },
  plugins: [
    new RemovePlugin({
      before: {
        include: ['./dist'],
        allowRoot: false
      },
      after: {
        include: ['./dist/temp']
      }
    })
  ]
};