or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-babel-plugin-transform-node-env-inline

Babel plugin that inlines NODE_ENV environment variable and statically evaluates related expressions

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/babel-plugin-transform-node-env-inline@0.4.x

To install, run

npx @tessl/cli install tessl/npm-babel-plugin-transform-node-env-inline@0.4.0

index.mddocs/

babel-plugin-transform-node-env-inline

A Babel plugin that inlines process.env.NODE_ENV environment variable references with their literal values during compilation, enabling dead code elimination and build-time optimizations.

Package Information

  • Package Name: babel-plugin-transform-node-env-inline
  • Package Type: npm
  • Language: JavaScript (Babel plugin)
  • Installation: npm install babel-plugin-transform-node-env-inline --save-dev

Core Imports

This package exports a single function that serves as a Babel plugin factory:

const plugin = require("babel-plugin-transform-node-env-inline");

For ES modules:

import plugin from "babel-plugin-transform-node-env-inline";

Basic Usage

Via .babelrc (Recommended)

{
  "plugins": ["transform-node-env-inline"]
}

Via CLI

babel --plugins transform-node-env-inline script.js

Via Node API

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

Transformation Example

Input:

if (process.env.NODE_ENV === "development") {
  console.log("Debug mode enabled");
}

const isProd = process.env.NODE_ENV === "production";

Output (when NODE_ENV=development):

if (true) {
  console.log("Debug mode enabled");
}

const isProd = false;

Capabilities

Plugin Factory Function

The main export is a Babel plugin factory function that creates a Babel plugin for transforming process.env.NODE_ENV references.

/**
 * Creates a Babel plugin that inlines NODE_ENV and evaluates expressions
 * @param {Object} babel - Babel object containing types utility
 * @param {Object} babel.types - Babel types utility for AST manipulation
 * @returns {BabelPlugin} Babel plugin object with visitor pattern
 */
function plugin({ types }) {
  return {
    name: "transform-node-env-inline",
    visitor: {
      MemberExpression(path) { /* implementation */ }
    }
  };
}

Plugin Object

The plugin factory returns a Babel plugin object with the following structure:

interface BabelPlugin {
  /** Plugin identifier name */
  name: "transform-node-env-inline";
  /** Visitor object for AST traversal */
  visitor: {
    /** Processes MemberExpression nodes to transform process.env.NODE_ENV */
    MemberExpression(path: NodePath): void;
  };
}

Types

/**
 * Babel NodePath object provided by Babel's traversal system
 */
interface NodePath {
  /** Check if the node matches a specific member access pattern */
  matchesPattern(pattern: string): boolean;
  /** Replace the current node with a new node */
  replaceWith(node: any): void;
  /** Access to the parent node path */
  parentPath: NodePath;
}

/**
 * Babel types utility object for creating AST nodes
 */
interface BabelTypes {
  /** Convert a JavaScript value to an AST node */
  valueToNode(value: any): any;
}

Transformation Details

Pattern Matching

The plugin identifies process.env.NODE_ENV member expressions using Babel's matchesPattern method with the exact pattern "process.env.NODE_ENV".

Static Evaluation

When process.env.NODE_ENV is part of a binary expression (like ===, !==, ==, !=), the plugin:

  1. Replaces process.env.NODE_ENV with its literal string value
  2. Evaluates the entire binary expression using Babel's evaluate() method
  3. Replaces the expression with the resulting boolean value if evaluation is confident

Supported Patterns

  • Direct access: process.env.NODE_ENV"development"
  • Equality checks: process.env.NODE_ENV === "development"true (when NODE_ENV=development)
  • Inequality checks: process.env.NODE_ENV !== "production"true (when NODE_ENV=development)
  • Loose equality: process.env.NODE_ENV == "development"true (when NODE_ENV=development)

Build-time Benefits

This transformation enables:

  • Dead code elimination: Bundlers can remove unreachable branches
  • Bundle size reduction: Eliminates development-only code from production builds
  • Runtime performance: Removes conditional checks that would otherwise run at runtime
  • Static analysis: Makes code behavior more predictable for further optimizations