vite-plugin-strip-comments

raw JSON →
0.0.10 verified Mon Apr 27 auth: no javascript

Vite plugin to strip comments in production builds, with options to preserve legal comments or JSDoc. Current stable version is 0.0.10 (released 2025-03-17). Targets Node >=20, requires pnpm >=9.10.0 or Deno >=2.0.4. Compatible with Rollup, Rolldown, tsup, and tsdown. Differentiates from built-in minify by removing stubborn comments like istanbul ignore flags. Provides three stripping modes: all, keep-legal (default), and keep-jsdoc. Ships TypeScript types. Early stage (low version), use with caution. GitHub: https://github.com/thednp/vite-plugin-strip-comments.

error Error: Module not found: Error: Can't resolve 'vite-plugin-strip-comments'
cause Package not installed or version incompatible with Node <20.
fix
Install the package and ensure Node >=20.
error TypeError: strip is not a function
cause Named import used instead of default import.
fix
Use default import: import strip from 'vite-plugin-strip-comments'
error TypeError: Cannot read properties of undefined (reading 'type')
cause Options object passed incorrectly as empty or invalid.
fix
Call strip() without arguments or with a valid options object: strip({ type: 'keep-legal' })
error Unexpected token 'export'
cause Using require() in a CommonJS context; package is ESM-only.
fix
Use dynamic import(): const strip = await import('vite-plugin-strip-comments');
gotcha Plugin is experimental; may strip important comments or cause build issues. Test thoroughly.
fix Enable only for production builds, disable other comment-stripping plugins to avoid double processing.
gotcha Does not respect /*! legal comment marker correctly before v0.0.9; use v0.0.9+.
fix Update to at least version 0.0.9.
gotcha Enforce option only accepts 'pre' (default) or 'post'; misconfiguration may cause unintended order.
fix Set enforce to 'pre' to strip before other transforms, or 'post' for after.
gotcha Type option expects exact strings: 'none', 'keep-legal', or 'keep-jsdoc'; typos are silently ignored.
fix Use exact string values as documented.
npm install vite-plugin-strip-comments
yarn add vite-plugin-strip-comments
pnpm add vite-plugin-strip-comments

Minimal Vite config using strip-comments plugin with keep-legal mode to preserve copyright/legal headers.

// vite.config.ts
import { defineConfig } from 'vite'
import strip from 'vite-plugin-strip-comments'

export default defineConfig({
  plugins: [
    strip({ type: 'keep-legal', enforce: 'pre' })
  ]
})