Vite Plugin Clean Build

1.4.1 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

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.

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
  },
});

view raw JSON →