
Security News
NVD Concedes Inability to Keep Pace with Surging CVE Disclosures in 2025
Security experts warn that recent classification changes obscure the true scope of the NVD backlog as CVE volume hits all-time highs.
babel-plugin-transform-replace-expressions
Advanced tools
Replace JavaScript expressions with other expressions
Replace JavaScript expressions with other expressions.
$ yarn add --dev babel-plugin-transform-replace-expressions
Input file:
const env = process.env.NODE_ENV;
typeof Hello === "number";
.babelrc
:
{
"plugins": [
[
"babel-plugin-transform-replace-expressions",
{
"replace": {
"process.env.NODE_ENV": "\"production\"",
"typeof Hello": "42"
}
}
]
]
}
Output:
const env = "production";
42 === "number";
A conflict happens when two replacements have the same Babel abstract syntax tree representation. For example expressions typeof A
and typeof (A)
are formatted differently but have the same AST representation as far as the plugin is concerned. In those situations the default is to raise an error, and can be overwritten by setting the option allowConflictingReplacements
to true
.
You can also always give the replacements as an array of key-value pairs. When allowConflictingReplacements
is set to true
the last conflicting replacement gets selected.
{
"plugins": [
[
"babel-plugin-transform-replace-expressions",
{
"replace": [
["typeof A", "B"],
["typeof (A)", "C"]
],
"allowConflictingReplacements": true
}
]
]
}
Replacements are only applied to expressions. Therefore replacing DEBUG
with false
in const DEBUG = true
does nothing, but for if (DEBUG) {}
the result is if (false) {}
.
Only full expressions count. You can't replace env
in process.env.NODE_ENV
, you have to replace process.env
, which is a proper expression in Babel AST.
A replacement is only applied when the result is valid JavaScript. For example replacing a
with 2
in the following code:
a = 1;
b = a;
yields
a = 1;
b = 2;
This plugin is licensed under the MIT license. See LICENSE.
FAQs
Replace JavaScript expressions with other expressions
The npm package babel-plugin-transform-replace-expressions receives a total of 67,740 weekly downloads. As such, babel-plugin-transform-replace-expressions popularity was classified as popular.
We found that babel-plugin-transform-replace-expressions demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Security experts warn that recent classification changes obscure the true scope of the NVD backlog as CVE volume hits all-time highs.
Security Fundamentals
Attackers use obfuscation to hide malware in open source packages. Learn how to spot these techniques across npm, PyPI, Maven, and more.
Security News
Join Socket for exclusive networking events, rooftop gatherings, and one-on-one meetings during BSidesSF and RSA 2025 in San Francisco.