Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork0
A Babel plugin for replacing expressions with other expressions
License
preactjs/babel-plugin-transform-replace-expressions
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Replace JavaScript expressions with other expressions.
$ yarn add --dev babel-plugin-transform-replace-expressionsInput file:
constenv=process.env.NODE_ENV;typeofHello==="number";
.babelrc:
{"plugins": [ ["babel-plugin-transform-replace-expressions", {"replace": {"process.env.NODE_ENV":"\"production\"","typeof Hello":"42" } } ] ]}Output:
constenv="production";42==="number";
A conflict happens when two replacements have the same Babelabstract syntax tree representation. For example expressionstypeof A andtypeof (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 optionallowConflictingReplacements totrue.
You can also always give the replacements as an array of key-value pairs. WhenallowConflictingReplacements is set totrue thelast 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
DEBUGwithfalseinconst DEBUG = truedoes nothing, but forif (DEBUG) {}the result isif (false) {}.Onlyfull expressions count. You can't replace
envinprocess.env.NODE_ENV, you have to replaceprocess.env, which is a proper expression in Babel AST.A replacement is only applied when the result is valid JavaScript. For example replacing
awith2in the following code:a=1;b=a;
yields
a=1;b=2;
This plugin is licensed under the MIT license. SeeLICENSE.
About
A Babel plugin for replacing expressions with other expressions
Topics
Resources
License
Code of conduct
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.