or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-babel-plugin-transform-remove-console

A Babel plugin that removes all console method calls during JavaScript code transformation.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/babel-plugin-transform-remove-console@6.9.x

To install, run

npx @tessl/cli install tessl/npm-babel-plugin-transform-remove-console@6.9.0

index.mddocs/

babel-plugin-transform-remove-console

A 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.

Package Information

  • Package Name: babel-plugin-transform-remove-console
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install babel-plugin-transform-remove-console --save-dev

Core Imports

Since 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]
});

Basic Usage

Configuration File Usage

.babelrc (without options):

{
  "plugins": ["transform-remove-console"]
}

.babelrc (with exclude options):

{
  "plugins": [
    ["transform-remove-console", { "exclude": ["error", "warn"] }]
  ]
}

CLI Usage

babel --plugins transform-remove-console script.js

Programmatic Usage

require("@babel/core").transform("code", {
  plugins: ["transform-remove-console"]
});

Transformation Examples

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";
}

Capabilities

Plugin Factory Function

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;
}

Plugin Configuration

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[];
}

Visitor Object

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;
  };
}

Transformation Behavior

The plugin handles various console usage patterns:

Console Method Calls

  • Expression statements: console.log("foo"); → completely removed
  • Non-expression contexts: var x = console.log("foo"); → replaced with var x = void 0;

Console Member Expressions

  • Direct reference: console.log → replaced with empty function function() {}
  • Assignment: var logger = console.log; → replaced with var logger = function() {};

Console Bind Calls

  • Bound methods: console.log.bind(console) → replaced with empty function

Excluded Methods

Methods 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");    // removed

Error Handling

The plugin operates at the AST level during Babel transformation. It does not throw runtime errors but may encounter compilation errors if:

  • Invalid AST structures are encountered
  • Babel transformation pipeline errors occur
  • Plugin configuration is malformed

Advanced Usage

Multiple Plugin Instances

{
  "plugins": [
    ["transform-remove-console", { "exclude": ["error"] }],
    // Other plugins...
  ]
}

Environment-Specific Configuration

{
  "env": {
    "production": {
      "plugins": ["transform-remove-console"]
    },
    "development": {
      "plugins": [
        ["transform-remove-console", { "exclude": ["log", "warn", "error"] }]
      ]
    }
  }
}