Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A Babel plugin for replacing expressions with other expressions

License

NotificationsYou must be signed in to change notification settings

preactjs/babel-plugin-transform-replace-expressions

Repository files navigation

Replace JavaScript expressions with other expressions.

Installation

$ yarn add --dev babel-plugin-transform-replace-expressions

Example

Input 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";

Conflict resolution

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}]]}

Notes

  • Replacements are only applied to expressions. Therefore replacingDEBUG withfalse inconst DEBUG = true does nothing, but forif (DEBUG) {} the result isif (false) {}.

  • Onlyfull expressions count. You can't replaceenv inprocess.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 replacinga with2 in the following code:

    a=1;b=a;

    yields

    a=1;b=2;

License

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

Stars

Watchers

Forks

Sponsor this project

  •  

[8]ページ先頭

©2009-2025 Movatter.jp