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.
Common errors
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.
Warnings
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.
Install
npm install vite-plugin-clean yarn add vite-plugin-clean pnpm add vite-plugin-clean Imports
- default import (cleanPlugin) wrong
const cleanPlugin = require('vite-plugin-clean')correctimport cleanPlugin from 'vite-plugin-clean' - default import (alias) wrong
import { clean } from 'vite-plugin-clean'correctimport clean from 'vite-plugin-clean' - TypeScript support
import cleanPlugin from 'vite-plugin-clean'
Quickstart
// vite.config.js
import { defineConfig } from 'vite'
import cleanPlugin from 'vite-plugin-clean'
export default defineConfig({
plugins: [
cleanPlugin({
targetFiles: ['dist', 'temp']
})
]
})