Vite Plugin Clean Build
vite-plugin-clean-build is a Vite plugin designed to remove or clean specified files and directories after the build process completes. It is currently at version 1.4.1 and maintains an active release cadence, with frequent minor updates addressing bug fixes, performance optimizations, and compatibility with new Vite versions (e.g., supporting Vite 5.0 and 6.0 in recent releases). Key differentiators include its straightforward configuration for specifying an `outputDir` and glob patterns for removal, alongside an optional verbose logging mode to track deleted files. This plugin is particularly useful for tasks such as cleaning up temporary assets, specific cache directories, or unwanted build artifacts that Vite's default build process might leave behind, ensuring a leaner and more controlled final build output.
Common errors
-
TypeError: (0 , vite_plugin_clean_build_1.default) is not a function
cause The imported `CleanBuild` function was not invoked with `()` when added to the `plugins` array.fixEnsure the plugin is called: `plugins: [CleanBuild()]` instead of `plugins: [CleanBuild]`. -
[vite-plugin-clean-build] The output directory 'dist' does not exist.
cause The `outputDir` configured in the plugin does not match Vite's actual `build.outDir`, or the build failed before creating the directory.fixEnsure `outputDir` in the plugin options precisely matches `build.outDir` in your Vite configuration. Verify that your Vite build completes successfully. -
Could not resolve "vite-plugin-clean-build" from "vite.config.ts"
cause The `vite-plugin-clean-build` package is not installed or incorrectly installed in your project's `node_modules`.fixRun `npm install vite-plugin-clean-build -D`, `yarn add vite-plugin-clean-build -D`, or `pnpm add vite-plugin-clean-build -D` to install the package as a dev dependency. -
Plugin "vite-plugin-clean-build" requires Vite version ">=3.0.0", but you are running "2.x.x".
cause Your installed `vite` version does not meet the peer dependency requirement of `vite-plugin-clean-build`.fixUpgrade your Vite installation to a compatible version (e.g., `npm update vite`) or, if necessary, downgrade `vite-plugin-clean-build` to a version compatible with your current Vite installation.
Warnings
- breaking The plugin's minor versions often align with new Vite major versions. For instance, v1.2.1 supports Vite 5.0, and v1.3.0 supports Vite 6.0. Using an incompatible plugin version with your Vite installation can lead to build failures or unexpected behavior.
- gotcha By default, `vite-plugin-clean-build` targets the `dist` directory. If your Vite project uses a different `build.outDir`, you must explicitly configure the `outputDir` option to match it, otherwise, the plugin will clean the wrong directory or do nothing.
- gotcha The `patterns` option uses `glob` syntax. Ensure you understand how glob patterns work, especially for negation (`!`) and directory matching, to avoid accidentally deleting critical files or failing to delete intended ones. Patterns are relative to `outputDir`.
- gotcha The cleaning process occurs after Vite's build step completes. If subsequent build processes or scripts rely on intermediate files in the output directory, those files might be removed before they can be used.
Install
-
npm install vite-plugin-clean-build -
yarn add vite-plugin-clean-build -
pnpm add vite-plugin-clean-build
Imports
- CleanBuild
const CleanBuild = require('vite-plugin-clean-build');import CleanBuild from 'vite-plugin-clean-build';
- CleanBuild (invocation)
plugins: [CleanBuild]
plugins: [CleanBuild()]
- Options (type)
import { Options } from 'vite-plugin-clean-build';import type { Options } from 'vite-plugin-clean-build';
Quickstart
import { defineConfig } from 'vite';
import CleanBuild from 'vite-plugin-clean-build';
import type { Options } from 'vite-plugin-clean-build'; // Demonstrate type import
const cleanBuildOptions: Options = {
outputDir: 'dist', // Default, but explicit for clarity
patterns: [
'images/**', // Clean all images within 'dist/images'
'!images/logo.png',// Except the logo file in 'dist/images'
'temp/*' // Also clean temporary files in 'dist/temp'
],
verbose: true, // Log deleted files to console
};
export default defineConfig({
plugins: [
CleanBuild(cleanBuildOptions),
],
build: {
outDir: cleanBuildOptions.outputDir, // Ensure Vite builds to the same directory
},
});