vite-plugin-clean

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

Vite plugin to remove/clean build files or folders before building. Version 2.0.1, maintained with sparse releases. Key differentiator: simple, zero-config clean of 'dist' by default with optional custom target files. Lightweight alternative to 'rimraf' or shell commands, integrates directly into Vite's build lifecycle. No extra dependencies.

error Error: Cannot find module 'vite-plugin-clean'
cause Missing npm install or mismatched package name.
fix
Run npm install --save-dev vite-plugin-clean
error TypeError: cleanPlugin is not a function
cause Incorrect import style (named instead of default).
fix
Use import cleanPlugin from 'vite-plugin-clean' (default import).
error Clean plugin not working: nothing is deleted.
cause targetFiles paths are relative to project root but might be incorrect.
fix
Use absolute paths or ensure relative paths are correct from project root.
gotcha Plugin cleans target files before every build, including dev server start.
fix Ensure target files are not needed during dev; consider using 'buildStart' only by wrapping in conditional.
gotcha targetFiles are deleted synchronously and irreversibly.
fix Double-check file paths; test with non-critical directories first.
deprecated Option 'cleanDist' removed in v2.0.0.
fix Use targetFiles: ['dist'] instead.
npm install vite-plugin-clean
yarn add vite-plugin-clean
pnpm add vite-plugin-clean

Demonstrates plugin usage with custom targetFiles to remove 'dist' and 'temp' directories before build.

// vite.config.js
import { defineConfig } from 'vite'
import cleanPlugin from 'vite-plugin-clean'

export default defineConfig({
  plugins: [
    cleanPlugin({
      targetFiles: ['dist', 'temp']
    })
  ]
})