esbuild-plugin-clean
raw JSON → 1.0.1 verified Mon Apr 27 auth: no javascript
An ESBuild plugin for cleaning up output directories and assets before or after a build. Version 1.0.1 requires esbuild >= 0.14.0 and uses the 'del' library under the hood for file deletion. Supports patterns for cleaning on start, on end, or both. Offers dry-run and sync/async modes. Provides TypeScript types. Differentiators include granular control over cleaning phases and verbose logging.
Common errors
error Error: The plugin "clean" is not compatible with ESBuild version 0.12.x ↓
cause Using an older version of esbuild (<0.14.0).
fix
Install esbuild >= 0.14.0: npm install esbuild@latest
error TypeError: clean is not a function ↓
cause Incorrect import: using default import instead of named import.
fix
Use import { clean } from 'esbuild-plugin-clean'
error Cannot find module 'del' ↓
cause Missing peer dependency 'del'.
fix
Install del: npm install del
Warnings
breaking The plugin requires esbuild >= 0.14.0. Using an older esbuild version will cause errors. ↓
fix Update esbuild to at least 0.14.0.
gotcha The plugin uses 'del' which may delete files outside the working directory if patterns are not careful. ↓
fix Use absolute paths or cwd option to restrict deletion scope.
deprecated Option 'dryRun' is misspelled (should be 'dryRun' but del uses 'dryRun' as well). ↓
fix It's intentional to match del's option naming.
Install
npm install esbuild-plugin-clean yarn add esbuild-plugin-clean pnpm add esbuild-plugin-clean Imports
- clean wrong
import clean from 'esbuild-plugin-clean'correctimport { clean } from 'esbuild-plugin-clean' - CleanOptions wrong
import { CleanOptions } from 'esbuild-plugin-clean'correctimport type { CleanOptions } from 'esbuild-plugin-clean' - clean wrong
const clean = require('esbuild-plugin-clean')correctconst { clean } = require('esbuild-plugin-clean')
Quickstart
import { build } from 'esbuild';
import { clean } from 'esbuild-plugin-clean';
(async () => {
await build({
entryPoints: ['./src/index.ts'],
bundle: true,
outdir: './dist',
plugins: [
clean({
patterns: ['./dist/*'],
cleanOnStartPatterns: ['./prepare'],
cleanOnEndPatterns: ['./post'],
dryRun: false,
sync: true,
verbose: true,
}),
],
});
})();