rollup-plugin-clear-directory

raw JSON →
1.0.0 verified Mon Apr 27 auth: no javascript

A Rollup plugin that clears the output directory before each build, ensuring a clean state and preventing leftover files from previous builds. Version 1.0.0 is the current stable release. It is a lightweight plugin with a simple configuration, allowing users to specify target directories to clear. The release cadence is low; no recent updates. Key differentiators: dedicated solely to clearing directories, unlike other plugins that bundle multiple utilities. Use with Rollup for build automation.

error Error: ENOENT: no such file or directory, rmdir '...'
cause The target directory does not exist.
fix
Create the directory before running the plugin or ensure the path is correct.
error TypeError: clearDirectory is not a function
cause Incorrect import statement, e.g., using named import instead of default import.
fix
Use 'import clearDirectory from 'rollup-plugin-clear-directory';'
gotcha The plugin clears directories synchronously before bundling starts. If you specify a non-empty target that does not exist, it may throw.
fix Ensure all target directories exist or handle errors. Alternatively, use a plugin like 'rollup-plugin-delete' which creates directories if needed.
gotcha If you target the output directory configured in Rollup output, the plugin may interfere with Rollup's own directory creation. Use with caution.
fix Clear before build, not during. The plugin clears before the build hook, so it should be safe, but test with your configuration.
npm install rollup-plugin
yarn add rollup-plugin
pnpm add rollup-plugin

Demonstrates basic usage of clearing the 'dist' directory before each Rollup build.

// rollup.config.js
import clearDirectory from 'rollup-plugin-clear-directory';

export default {
  input: 'src/main.js',
  output: {
    dir: 'dist',
    format: 'esm'
  },
  plugins: [
    clearDirectory({
      targets: ['dist']
    })
  ]
};