unplugin-remove

raw JSON →
1.0.3 verified Sat Apr 25 auth: no javascript

A build tool plugin that automatically removes `console.*` calls (log, warn, error, info, debug) and `debugger` statements in production mode. Version 1.0.3 is the current stable release. It supports Vite, Webpack, Rollup, esbuild, Rspack (experimental), and Rolldown (experimental) with a unified API via the unplugin framework. Key differentiators: zero-config defaults, configurable console types, include/exclude patterns, and source map support. Built on babel for AST transformation. Releases are infrequent, focused on bug fixes and new bundler support.

error TypeError: RspackPlugin is not a constructor
cause Using `new RspackPlugin()` without `.default` in CommonJS require.
fix
Use require('unplugin-remove/rspack').default or import RspackPlugin from 'unplugin-remove/rspack' in ESM.
error Error: [unplugin-remove] No bundler specified. Did you forget to use a subpath import?
cause Importing from 'unplugin-remove' directly instead of 'unplugin-remove/vite', etc.
fix
Import from the specific bundler subpath: import viteRemove from 'unplugin-remove/vite'.
error WARNING: Removing 'console.log' but it might be used in a logical expression.
cause The plugin may remove console.log from inside conditional expressions like `a && console.log('test')` where removal could alter logic.
fix
Review code to ensure removal does not change behavior; wrap console calls in blocks if needed.
error Source map mismatch warnings from bundler
cause Older versions (<1.0.3) did not use magic-string for source maps, causing incorrect mappings.
fix
Upgrade to v1.0.3 or later which uses magic-string for proper source map generation.
breaking Version 1.0.0 switched from custom AST parsing to babel, altering transform behavior and potentially removing code differently.
fix Upgrade to 1.0.0+ and verify your codebase for any removed console statements that were previously kept.
gotcha The plugin does NOT check process.env.NODE_ENV by default; it runs on all builds unless you guard its inclusion.
fix Conditionally add the plugin only in production: e.g., `process.env.NODE_ENV === 'production' ? viteRemove() : undefined`.
gotcha Rspack and Rolldown support are marked as experimental and may have bugs or incomplete features.
fix Test thoroughly or avoid using these bundler integrations in production.
gotcha The rspack example uses `require('unplugin-remove/rspack').default` — omitting `.default` will result in a constructor error.
fix Use `.default` when using CommonJS require for the rspack plugin.
gotcha By default, only `console.log` is removed. Other console methods (warn, error, info, debug) are NOT removed unless explicitly added to `consoleType`.
fix Set `consoleType: ['log', 'warn', 'error', 'info', 'debug']` to remove all console calls.
deprecated The `external` option (an array of module names) is still supported but may be removed in a future major version.
fix Use `include`/`exclude` patterns instead to filter files.
npm install unplugin-remove
yarn add unplugin-remove
pnpm add unplugin-remove

Shows how to configure and use unplugin-remove with Vite, removing console.log and debugger in production.

// vite.config.ts
import { defineConfig } from 'vite'
import viteRemove from 'unplugin-remove/vite'

export default defineConfig({
  plugins: [
    viteRemove({
      // remove only console.log and debugger (default)
      external: [],
      consoleType: ['log'],
      include: [/\.[jt]sx?$/, /\.vue\??/],
      exclude: [/node_modules/, /\.git/]
    })
  ]
})