Babel plugin that transforms process.env references into inline string literals at build time.
npx @tessl/cli install tessl/npm-babel-plugin-transform-inline-environment-variables@0.4.0A 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.
npm install babel-plugin-transform-inline-environment-variables --save-devThis 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"]
});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";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;
}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"]
}]
]
}The plugin transforms the following patterns:
// Input
process.env.VARIABLE_NAME
// Output (if VARIABLE_NAME = "value")
"value"// Input
process.env["VARIABLE_NAME"]
// Output (if VARIABLE_NAME = "value")
"value"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";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.jsVia Node.js API:
const babel = require("@babel/core");
const result = babel.transform(sourceCode, {
plugins: [
["transform-inline-environment-variables", {
include: ["NODE_ENV"]
}]
]
});process.env.VAR or process.env["VAR"])process.env[varName])process.env.VAR = "value")