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.
npm install vite-plugin-clean-buildVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to integrate `vite-plugin-clean-build` into a Vite configuration, specifying custom output directories and glob patterns for cleaning, along with verbose logging, and showing how to import the configuration type for type-safety.
Always ensure your `vite-plugin-clean-build` version is compatible with your installed `vite` version, referring to the plugin's release notes for specific compatibility matrix.
Set `outputDir` in the plugin configuration to match your `vite.config.ts`'s `build.outDir` (e.g., `outputDir: 'my-custom-output'`).
Test your glob patterns carefully, perhaps using a dedicated glob testing tool, and start with `verbose: true` in the plugin options to confirm what files are being targeted for deletion.
Plan your build pipeline carefully, ensuring that `vite-plugin-clean-build` runs at an appropriate stage, typically as the very last step involving the build output, or adjust your dependent scripts.
Ensure the plugin is called: `plugins: [CleanBuild()]` instead of `plugins: [CleanBuild]`.
Ensure `outputDir` in the plugin options precisely matches `build.outDir` in your Vite configuration. Verify that your Vite build completes successfully.
Run `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.
Upgrade 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.