babel-plugin-inline-replace-variables

raw JSON →
1.3.1 verified Sat Apr 25 auth: no javascript maintenance

A Babel plugin that replaces identifier references with literal values during compilation. Version 1.3.1 is the latest stable release. It supports simple literal replacements (strings, booleans) as well as complex expressions via AST nodes. The plugin is commonly used for compile-time constants like __DEV__ or __VERSION__. Compared to alternatives like babel-plugin-replace-identifiers, this one replaces identifiers with literal values, not other identifiers. Low maintenance and simple API, suitable for projects using Babel 6/7.

error Cannot find module 'babel-plugin-inline-replace-varibles'
cause Misspelled package name: 'varibles' instead of 'variables'.
fix
Install the correct package: npm install babel-plugin-inline-replace-variables --save-dev
error ReferenceError: __SERVER__ is not defined
cause Variable used in source code but not replaced because plugin not configured or not running.
fix
Add the plugin to .babelrc with the replacement value for __SERVER__.
error Error: Plugin configuration: [object Object] is not a valid replacement
cause Trying to use an object replacement without proper 'type' property.
fix
Use format: { '__KEY__': { type: 'node', replacement: someASTNode } }
deprecated Package name changed: 'babel-plugin-inline-replace-varibles' (misspelled) is deprecated; use 'babel-plugin-inline-replace-variables'.
fix npm uninstall babel-plugin-inline-replace-varibles && npm install babel-plugin-inline-replace-variables
gotcha Plugin only replaces identifiers with literal values, not other identifiers. If you need identifier-to-identifier replacement, use babel-plugin-replace-identifiers instead.
fix Use babel-plugin-replace-identifiers for identifier replacement.
gotcha When using AST node replacement via 'replacement' property, the node must be a valid Babel AST node; passing a string will be interpreted as a literal value, not a node.
fix Use { type: 'node', replacement: /* AST node */ } for expression replacements.
breaking Babel 6 vs Babel 7 compatibility: plugin may not work with Babel 7 without adjustment due to peer dependency on @babel/core.
fix Ensure @babel/core is installed as a dev dependency and that the plugin is compatible (tested with Babel 7+).
npm install babel-plugin-inline-replace-variables
yarn add babel-plugin-inline-replace-variables
pnpm add babel-plugin-inline-replace-variables

Replace __SERVER__ with true and __VERSION__ with a string literal using Babel plugin configuration.

// .babelrc or babel.config.js
{
  "plugins": [
    ["inline-replace-variables", {
      "__SERVER__": true,
      "__VERSION__": "1.0.0"
    }]
  ]
}

// input.js
if (__SERVER__) {
  console.log('Server version:', __VERSION__);
}

// output.js
if (true) {
  console.log('Server version:', "1.0.0");
}