CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-babel-plugin-transform-inline-environment-variables

Babel plugin that transforms process.env references into inline string literals at build time.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

index.mddocs/

babel-plugin-transform-inline-environment-variables

A Babel plugin that transforms process.env references into inline string literals at build time. This enables compile-time environment variable inlining, which helps with dead code elimination and optimization by replacing environment variable references with their actual values during the build process.

Package Information

  • Package Name: babel-plugin-transform-inline-environment-variables
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install babel-plugin-transform-inline-environment-variables --save-dev

Core Imports

This plugin is configured through Babel configuration files rather than imported directly in code:

.babelrc:

{
  "plugins": ["transform-inline-environment-variables"]
}

Node API:

const babel = require("@babel/core");

babel.transform("code", {
  plugins: ["transform-inline-environment-variables"]
});

Basic Usage

The plugin automatically transforms process.env references during Babel compilation:

Input code:

// Assuming process.env.NODE_ENV is "production"
if (process.env.NODE_ENV === "development") {
  console.log("Debug mode");
}
const apiUrl = process.env.API_URL;

Output after transformation:

if ("production" === "development") {
  console.log("Debug mode");
}
const apiUrl = "https://api.production.com";

Capabilities

Plugin Factory Function

The main export is a Babel plugin factory function that creates a plugin with environment variable inlining behavior.

/**
 * Creates a Babel plugin that transforms process.env references into inline literals
 * @param {Object} babel - Babel core object with types
 * @param {Object} babel.types - Babel types utilities (t)
 * @returns {BabelPlugin} Babel plugin object with name and visitor
 */
function pluginFactory({ types: t }) {
  return {
    name: "transform-inline-environment-variables",
    visitor: {
      MemberExpression(path, { opts: { include, exclude } = {} }) {
        // Implementation: transforms process.env.X to string literals
      }
    }
  };
}

/**
 * Plugin object structure returned by the factory
 */
interface BabelPlugin {
  name: string;
  visitor: {
    MemberExpression(path: NodePath, state: PluginState): void;
  };
}

interface PluginState {
  opts: PluginOptions;
}

interface NodePath {
  get(key: string): NodePath;
  matchesPattern(pattern: string): boolean;
  toComputedKey(): Node;
  parent: Node;
  node: Node;
  replaceWith(node: Node): void;
}

interface Node {
  value?: any;
}

Plugin Options

The plugin accepts configuration options to control which environment variables are processed:

interface PluginOptions {
  /** Array of environment variable names to include (whitelist) */
  include?: string[];
  /** Array of environment variable names to exclude (blacklist) */
  exclude?: string[];
}

Usage with options:

{
  "plugins": [
    ["transform-inline-environment-variables", {
      "include": ["NODE_ENV", "API_URL"],
      "exclude": ["SECRET_KEY"]
    }]
  ]
}

Transformation Behavior

The plugin transforms the following patterns:

Dot Notation Access

// Input
process.env.VARIABLE_NAME

// Output (if VARIABLE_NAME = "value")
"value"

Bracket Notation Access

// Input  
process.env["VARIABLE_NAME"]

// Output (if VARIABLE_NAME = "value")
"value"

Assignment Protection

The plugin does not transform assignments to prevent unintended side effects:

// Input (remains unchanged)
process.env.NODE_ENV = "development";

// Output (no transformation)
process.env.NODE_ENV = "development";

Configuration Examples

Include specific variables only:

{
  "plugins": [
    ["transform-inline-environment-variables", {
      "include": ["NODE_ENV", "DEBUG", "API_VERSION"]
    }]
  ]
}

Exclude sensitive variables:

{
  "plugins": [
    ["transform-inline-environment-variables", {
      "exclude": ["SECRET_KEY", "DATABASE_PASSWORD", "JWT_SECRET"]
    }]
  ]
}

Via CLI:

babel --plugins transform-inline-environment-variables script.js

Via Node.js API:

const babel = require("@babel/core");

const result = babel.transform(sourceCode, {
  plugins: [
    ["transform-inline-environment-variables", {
      include: ["NODE_ENV"]
    }]
  ]
});

Limitations

  • Only processes string literal property access (process.env.VAR or process.env["VAR"])
  • Does not transform computed property access with variables (process.env[varName])
  • Does not transform assignment expressions (process.env.VAR = "value")
  • Requires environment variables to be available at build time
  • Transforms are based on the build environment, not runtime environment

docs

index.md

tile.json