vite-plugin-remove-console

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

A Vite plugin that removes specified console statements in production builds. Current stable version is 2.2.0 (released 2023-12-04). Active development with regular releases. Key differentiators: supports custom removal statements via `custom` option, can retain specific console values via `externalValue`, and can exclude certain files via `external`. Works with .js, .ts, .jsx, .tsx, .vue, .svelte files. Comparison alternatives: unplugin-remove-console may be more framework-agnostic, but this plugin is Vite-specific and simple to configure.

error Cannot find module 'vite-plugin-remove-console' or its corresponding type declarations.
cause Missing installation or tsconfig not resolving types.
fix
Run npm install vite-plugin-remove-console -D and ensure tsconfig.json includes 'node_modules/@types' or use 'moduleResolution': 'node'.
error TypeError: removeConsole is not a function
cause Importing as named export instead of default export.
fix
Use: import removeConsole from 'vite-plugin-remove-console'
error The 'custom' option is not supported in version 1.x.
cause Using custom option with older version of the plugin.
fix
Upgrade to >=2.2.0: npm install vite-plugin-remove-console@2.2.0 -D
error Error: Plugin 'vite-plugin-remove-console' not recognized. Ensure it's in the plugins array and imported correctly.
cause Plugin not added to plugins array or imported incorrectly.
fix
Add removeConsole() to plugins array in vite.config.ts: plugins: [vue(), removeConsole()]
gotcha The plugin removes console statements only in production builds. Does not affect development.
fix Use process.env.NODE_ENV check if you need conditional removal in dev.
breaking v2.0.0 changed the default behavior: previously removed all console statements; now only removes console.log if no `includes` option provided.
fix Explicitly pass includes: ['log', 'warn', 'error', 'info', 'debug'] to restore old behavior.
gotcha Using `custom` option completely overrides `includes` option. They are not additive.
fix Do not combine `custom` with `includes`; choose one approach.
deprecated Option `external` can cause issues on Windows due to path separator backslashes.
fix Use forward slashes in paths, or upgrade to >=1.0.4 which fixes path translation.
gotcha The `externalValue` option matches against the string argument of the console call. Works only for simple string literals, not expressions.
fix Use simple string literals like 'keep this' inside console.log('keep this').
npm install vite-plugin-remove-console
yarn add vite-plugin-remove-console
pnpm add vite-plugin-remove-console

Typical Vite configuration using vite-plugin-remove-console to remove console.log, console.warn, console.error in production, while keeping statements with specific values and excluding a file.

// vite.config.ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import removeConsole from 'vite-plugin-remove-console'

export default defineConfig({
  plugins: [
    vue(),
    removeConsole({
      includes: ['log', 'warn', 'error'],
      externalValue: ['keep this'],
      external: ['src/no-remove.js']
    })
  ]
})