Babel plugin that inlines NODE_ENV environment variable and statically evaluates related expressions
npx @tessl/cli install tessl/npm-babel-plugin-transform-node-env-inline@0.4.0A 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.
npm install babel-plugin-transform-node-env-inline --save-devThis 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";{
"plugins": ["transform-node-env-inline"]
}babel --plugins transform-node-env-inline script.jsrequire("@babel/core").transform("code", {
plugins: ["transform-node-env-inline"]
});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;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 */ }
}
};
}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;
};
}/**
* 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;
}The plugin identifies process.env.NODE_ENV member expressions using Babel's matchesPattern method with the exact pattern "process.env.NODE_ENV".
When process.env.NODE_ENV is part of a binary expression (like ===, !==, ==, !=), the plugin:
process.env.NODE_ENV with its literal string valueevaluate() methodprocess.env.NODE_ENV → "development"process.env.NODE_ENV === "development" → true (when NODE_ENV=development)process.env.NODE_ENV !== "production" → true (when NODE_ENV=development)process.env.NODE_ENV == "development" → true (when NODE_ENV=development)This transformation enables: