A Babel plugin that removes all console method calls during JavaScript code transformation.
npx @tessl/cli install tessl/npm-babel-plugin-transform-remove-console@6.9.0A Babel plugin that removes all console.* calls from JavaScript code during the compilation process. This plugin provides a clean way to eliminate debugging statements from production code without manually removing each console statement.
npm install babel-plugin-transform-remove-console --save-devSince this is a Babel plugin, it's typically imported through Babel configuration rather than direct import statements.
Babel configuration (.babelrc or babel.config.js):
{
"plugins": ["transform-remove-console"]
}For programmatic use:
const babel = require("@babel/core");
const removeConsolePlugin = require("babel-plugin-transform-remove-console");
const result = babel.transform(code, {
plugins: [removeConsolePlugin]
});.babelrc (without options):
{
"plugins": ["transform-remove-console"]
}.babelrc (with exclude options):
{
"plugins": [
["transform-remove-console", { "exclude": ["error", "warn"] }]
]
}babel --plugins transform-remove-console script.jsrequire("@babel/core").transform("code", {
plugins: ["transform-remove-console"]
});Input:
function foo() {
console.log("debug message");
console.error("error message");
console.warn("warning message");
return "result";
}Output (default):
function foo() {
return "result";
}Output (with exclude: ["error", "warn"]):
function foo() {
console.error("error message");
console.warn("warning message");
return "result";
}The main export that creates the Babel plugin configuration.
/**
* Main plugin factory function
* @param {Object} param - Babel plugin context
* @param {Object} param.types - Babel types (t) for AST manipulation
* @returns {Object} Babel plugin configuration object
*/
function plugin({ types: t }): BabelPlugin;
interface BabelPlugin {
name: string;
visitor: VisitorObject;
}The plugin accepts configuration options to customize its behavior.
interface PluginOptions {
/**
* Array of console method names to exclude from removal
* @example ["error", "warn", "info"]
*/
exclude?: string[];
}The AST visitor configuration that defines how the plugin transforms code.
interface VisitorObject {
/**
* Handles console method call expressions
* @param {NodePath} path - AST node path for CallExpression
* @param {Object} state - Plugin state containing options
*/
CallExpression(path: NodePath, state: { opts: PluginOptions }): void;
/**
* Handles console member expressions on exit
* @param {NodePath} path - AST node path for MemberExpression
* @param {Object} state - Plugin state containing options
*/
MemberExpression: {
exit(path: NodePath, state: { opts: PluginOptions }): void;
};
}The plugin handles various console usage patterns:
console.log("foo"); → completely removedvar x = console.log("foo"); → replaced with var x = void 0;console.log → replaced with empty function function() {}var logger = console.log; → replaced with var logger = function() {};console.log.bind(console) → replaced with empty functionMethods specified in the exclude option are preserved:
// With exclude: ["error", "warn"]
console.log("debug"); // removed
console.error("error"); // preserved
console.warn("warning"); // preserved
console.info("info"); // removedThe plugin operates at the AST level during Babel transformation. It does not throw runtime errors but may encounter compilation errors if:
{
"plugins": [
["transform-remove-console", { "exclude": ["error"] }],
// Other plugins...
]
}{
"env": {
"production": {
"plugins": ["transform-remove-console"]
},
"development": {
"plugins": [
["transform-remove-console", { "exclude": ["log", "warn", "error"] }]
]
}
}
}