# babel-plugin-transform-replace-expressions

> Replace JavaScript expressions with other expressions

Latest version **0.2.0** (published 2019-03-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install babel-plugin-transform-replace-expressions
pnpm add babel-plugin-transform-replace-expressions
yarn add babel-plugin-transform-replace-expressions
bun add babel-plugin-transform-replace-expressions
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2019-03-11 |
| First published | 2019-02-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 6.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 25 |
| Author | Joachim Viide |
| Maintainers | jviide |
| Keywords | babel-plugin |

## Links

- npm: https://www.npmjs.com/package/babel-plugin-transform-replace-expressions
- Repository: https://github.com/jviide/babel-plugin-transform-replace-expressions
- npm.io page: https://npm.io/package/babel-plugin-transform-replace-expressions

## Dependencies (1)

- [@babel/parser](https://npm.io/package/@babel/parser.md) ^7.3.3

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.2.0 (latest) — 2019-03-11
- 0.1.1 — 2019-02-24
- 0.1.0 — 2019-02-24
- 0.0.3 — 2019-02-24
- 0.0.2 — 2019-02-24
- 0.0.1 — 2019-02-24

## README

# babel-plugin-transform-replace-expressions [![CircleCI](https://circleci.com/gh/jviide/babel-plugin-transform-replace-expressions.svg?style=shield)](https://circleci.com/gh/jviide/babel-plugin-transform-replace-expressions) [![npm](https://img.shields.io/npm/v/babel-plugin-transform-replace-expressions.svg)](https://www.npmjs.com/package/babel-plugin-transform-replace-expressions)

Replace JavaScript expressions with other expressions.

## Installation

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

## Example

Input file:

```js
const env = process.env.NODE_ENV;

typeof Hello === "number";
```

`.babelrc`:

```json
{
  "plugins": [
    [
      "babel-plugin-transform-replace-expressions",
      {
        "replace": {
          "process.env.NODE_ENV": "\"production\"",
          "typeof Hello": "42"
        }
      }
    ]
  ]
}
```

Output:

```js
const env = "production";

42 === "number";
```

## Conflict resolution

A conflict happens when two replacements have the same Babel [abstract syntax tree](https://en.wikipedia.org/wiki/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.

```js
{
  "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:
 
   ```js
   a = 1;
   b = a;
   ```
   
   yields
   
   ```js
   a = 1;
   b = 2;
   ```

## License

This plugin is licensed under the MIT license. See [LICENSE](./LICENSE).

---
_Source: https://npm.io/package/babel-plugin-transform-replace-expressions · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
