All Projects → jviide → babel-plugin-transform-replace-expressions

jviide / babel-plugin-transform-replace-expressions

Licence: MIT License
A Babel plugin for replacing expressions with other expressions

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to babel-plugin-transform-replace-expressions

Babel Plugin Polished
Compile polished helper functions at build time
Stars: ✭ 133 (+478.26%)
Mutual labels:  babel, babel-plugin
babel-plugin-source-map-support
A Babel plugin which automatically makes stack traces source-map aware
Stars: ✭ 41 (+78.26%)
Mutual labels:  babel, babel-plugin
Babel Plugin Transform Typescript Metadata
Babel plugin to emit decorator metadata like typescript compiler
Stars: ✭ 142 (+517.39%)
Mutual labels:  babel, babel-plugin
Js Proposal Algebraic Effects
📐Let there be algebraic effects in JS
Stars: ✭ 110 (+378.26%)
Mutual labels:  babel, babel-plugin
Xwind
Tailwind CSS as a templating language in JS and CSS-in-JS
Stars: ✭ 249 (+982.61%)
Mutual labels:  babel, babel-plugin
Babel Plugin Prismjs
A babel plugin to use PrismJS with standard bundlers.
Stars: ✭ 114 (+395.65%)
Mutual labels:  babel, babel-plugin
Param.macro
Partial application syntax and lambda parameters for JavaScript, inspired by Scala's `_` & Kotlin's `it`
Stars: ✭ 170 (+639.13%)
Mutual labels:  babel, babel-plugin
Idx.macro
a 'babel-macros' version of 'babel-plugin-idx'
Stars: ✭ 90 (+291.3%)
Mutual labels:  babel, babel-plugin
Eslint Import Resolver Babel Module
Custom eslint resolve for babel-plugin-module-resolver
Stars: ✭ 236 (+926.09%)
Mutual labels:  babel, babel-plugin
Babel Plugin React Intl Auto
i18n for the component age. Auto management react-intl ID.
Stars: ✭ 203 (+782.61%)
Mutual labels:  babel, babel-plugin
Babel Plugin Jsx Adopt
Stars: ✭ 94 (+308.7%)
Mutual labels:  babel, babel-plugin
babel-plugin-proposal-pattern-matching
the minimal grammar, high performance JavaScript pattern matching implementation
Stars: ✭ 34 (+47.83%)
Mutual labels:  babel, babel-plugin
Babel Plugin React Persist
Automatically useCallback() & useMemo(); memoize inline functions
Stars: ✭ 91 (+295.65%)
Mutual labels:  babel, babel-plugin
Babel Plugin Runtyper
⚡️ Runtime type-checker for JavaScript
Stars: ✭ 117 (+408.7%)
Mutual labels:  babel, babel-plugin
Jsx Control Statements
Neater If and For for React JSX
Stars: ✭ 1,305 (+5573.91%)
Mutual labels:  babel, babel-plugin
Babel Plugin React Html Attrs
Babel plugin which transforms HTML and SVG attributes on JSX host elements into React-compatible attributes
Stars: ✭ 170 (+639.13%)
Mutual labels:  babel, babel-plugin
Modify Babel Preset
💫 Create a modified babel preset based on an an existing preset.
Stars: ✭ 85 (+269.57%)
Mutual labels:  babel, babel-plugin
Generator Babel Plugin
Babel Plugin generator for Yeoman
Stars: ✭ 88 (+282.61%)
Mutual labels:  babel, babel-plugin
Babel Plugin Macros
🎣 Allows you to build simple compile-time libraries
Stars: ✭ 2,366 (+10186.96%)
Mutual labels:  babel, babel-plugin
S2s
Coding time Compile. A tool to write code fastest.
Stars: ✭ 254 (+1004.35%)
Mutual labels:  babel, babel-plugin

babel-plugin-transform-replace-expressions CircleCI npm

Replace JavaScript expressions with other expressions.

Installation

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

Example

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

Conflict resolution

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

Notes

  • 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;

License

This plugin is licensed under the MIT license. See LICENSE.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].