esbuild-plugin-vitest-cleaner
raw JSON → 0.5.1 verified Fri May 01 auth: no javascript
An esbuild plugin that removes import.meta.vitest references and associated code blocks before compilation, preventing test code from leaking into production builds. Current stable version is 0.5.1, with no fixed release cadence. Key differentiator: lightweight alternative to esbuild's built-in drop feature or conditional compilation, with support for file filtering via regex. Ships TypeScript type definitions. Requires Node >= 18.0.0. Known issue: JSDoc comments around import.meta.vitest are not stripped.
Common errors
error ERR_REQUIRE_ESM: require() of ES Module ↓
cause Package is ESM-only, but imported via CommonJS require()
fix
Use import { vitestCleaner } from 'esbuild-plugin-vitest-cleaner' instead of require().
error 'vitestCleaner' is not a function ↓
cause Using default import instead of named import
fix
Use import { vitestCleaner } from 'esbuild-plugin-vitest-cleaner'.
error Cannot find name 'vitestCleaner'. Did you mean 'vitestCleaner'? ↓
cause Missing or incorrect import in TypeScript
fix
Ensure the import is correct: import { vitestCleaner } from 'esbuild-plugin-vitest-cleaner'.
Warnings
gotcha JSDoc comments that annotate import.meta.vitest evaluations are not removed; leftover comments may contain test references. ↓
fix Manually review output or await a future version with AST-based parsing.
gotcha Plugin uses regex-based removal; complex code blocks with nested import.meta.vitest may not be fully stripped. ↓
fix Ensure test blocks are simple and straightforward; consider using esbuild's drop feature if available.
breaking Only supports ESM; Node CJS require() will throw a module import error. ↓
fix Use import syntax or configure project for ESM.
Install
npm install esbuild-plugin-vitest-cleaner yarn add esbuild-plugin-vitest-cleaner pnpm add esbuild-plugin-vitest-cleaner Imports
- vitestCleaner wrong
const vitestCleaner = require('esbuild-plugin-vitest-cleaner')correctimport { vitestCleaner } from 'esbuild-plugin-vitest-cleaner' - vitestCleaner wrong
import vitestCleaner from 'esbuild-plugin-vitest-cleaner'correctimport { vitestCleaner } from 'esbuild-plugin-vitest-cleaner' - FilterOptions
import type { FilterOptions } from 'esbuild-plugin-vitest-cleaner'
Quickstart
import { build } from 'esbuild';
import { vitestCleaner } from 'esbuild-plugin-vitest-cleaner';
await build({
entryPoints: ['src/index.ts'],
outfile: 'dist/index.js',
bundle: true,
plugins: [vitestCleaner()]
});