A Vite plugin that selectively removes console statements in production builds. Console statements can be kept using comment markers or by specifying which console methods to process.
# npm
npm install vite-plugin-keep-console --save-dev
# yarn
yarn add vite-plugin-keep-console -D
# pnpm
pnpm add vite-plugin-keep-console -DAdd the plugin to your vite.config.js or vite.config.ts:
import { defineConfig } from "vite";
import ConsoleKeeper from "vite-plugin-keep-console";
export default defineConfig({
plugins: [
ConsoleKeeper({
// options
}),
],
});| Option | Type | Default | Description |
|---|---|---|---|
includes |
string[] |
[] |
Array of console methods to process. If empty, all console methods are processed |
external |
Array<string|RegExp> |
[] |
Array of files to include for processing (string or RegExp) |
keepComments |
string[] |
["keep-console"] |
Array of comment markers that prevent console statement removal |
The plugin supports processing all standard console methods:
- Basic:
debug,error,info,log,warn - Advanced:
dir,dirxml,table,trace,group,groupCollapsed,groupEnd,clear - Performance:
count,countReset,time,timeLog,timeEnd,timeStamp,profile,profileEnd - Other:
assert,context,createTask,memory
import { defineConfig } from "vite";
import ConsoleKeeper from "vite-plugin-keep-console";
export default defineConfig({
plugins: [
ConsoleKeeper(), // Will remove all console statements except those marked with "keep-console" comment
],
});import { defineConfig } from "vite";
import ConsoleKeeper from "vite-plugin-keep-console";
export default defineConfig({
plugins: [
ConsoleKeeper({
includes: ["log", "error", "warn"], // Only process console.log, console.error, and console.warn
}),
],
});import { defineConfig } from "vite";
import ConsoleKeeper from "vite-plugin-keep-console";
export default defineConfig({
plugins: [
ConsoleKeeper({
external: ["src/**", /\.tsx?$/], // Only process files in src folder and TypeScript files
}),
],
});import { defineConfig } from "vite";
import ConsoleKeeper from "vite-plugin-keep-console";
export default defineConfig({
plugins: [
ConsoleKeeper({
keepComments: ["KEEP", "IMPORTANT", "DEBUG"],
}),
],
});You can prevent specific console statements from being removed by adding comment markers:
// keep-console
console.log("This will be kept in production");
/* keep-console */
console.error("This error will also be kept");
console.log("This will be removed in production");
// Using custom markers (if configured)
// KEEP
console.log("Kept with custom marker");
// IMPORTANT
console.warn("Important warning kept in production");Comment markers can be placed:
- Before the console statement (leading comments)
- After the console statement (trailing comments)
- Inside the console method call
- Before parameters
// keep-console - before statement
console.log("kept");
console.log("kept"); // keep-console - after statement
console.log(/* keep-console */ "kept"); // inside call
console.log(
// keep-console - before parameter
"kept"
);- File Processing: The plugin processes
.ts,.tsx,.js,.jsx,.vue, and.sveltefiles during the build phase - Console Detection: It identifies all
console.*method calls in your code - Comment Checking: For each console statement, it checks for comment markers in various positions
- Selective Removal: Console statements without keep markers are removed, while marked ones are preserved
- Smart Replacement: When console statements are used in expressions, they are replaced with
undefinedto maintain type compatibility
MIT © biubiukam