# @oliver-schoendorn/babel-plugin-transform-define

> This babel plugin will take a set of config values (or load them from a file path) and replace all occurrences in the parsed scripts.

Latest version **2.0.0** (published 2019-11-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install @oliver-schoendorn/babel-plugin-transform-define
pnpm add @oliver-schoendorn/babel-plugin-transform-define
yarn add @oliver-schoendorn/babel-plugin-transform-define
bun add @oliver-schoendorn/babel-plugin-transform-define
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2019-11-28 |
| First published | 2019-02-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 13.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Oliver Schöndorn |
| Maintainers | cyanide235 |
| Keywords | babel, plugin, define |

## Links

- npm: https://www.npmjs.com/package/@oliver-schoendorn/babel-plugin-transform-define
- Repository: https://github.com/oliver-schoendorn/babel-plugin-transform-define
- Homepage: https://github.com/oliver-schoendorn/babel-plugin-transform-define#readme
- Issues: https://github.com/oliver-schoendorn/babel-plugin-transform-define/issues
- npm.io page: https://npm.io/package/@oliver-schoendorn/babel-plugin-transform-define

## 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

- 2.0.0 (latest) — 2019-11-28
- 1.0.1 — 2019-02-25
- 1.0.0 — 2019-02-25

## README

# babel-plugin-transform-define
[![Build Status](https://travis-ci.com/oliver-schoendorn/babel-plugin-transform-define.svg?branch=master)](https://travis-ci.com/oliver-schoendorn/babel-plugin-transform-define)
[![Coverage Status](https://coveralls.io/repos/github/oliver-schoendorn/babel-plugin-transform-define/badge.svg?branch=master)](https://coveralls.io/github/oliver-schoendorn/babel-plugin-transform-define?branch=master)

This [Babel](https://babeljs.io/) plugin will take a set of config values (or load them from a file path) and replace all occurrences in the parsed scripts.
This plugin is compatible with [Babel](https://babeljs.io/) 7. Other [Babel](https://babeljs.io/) versions have not been tested.

`npm i -D @oliver-schoendorn/babel-plugin-transform-define`


### Configuration example (.babelrc, static config)

```json
{
    "plugins": [
        [ "@oliver-schoendorn/babel-plugin-transform-define", {
            "values": { "your": "values" } 
        } ]
    ]
}
```

### Configuration example (.babelrc, config from file)

```json
{
    "plugins": [
        [ "@oliver-schoendorn/babel-plugin-transform-define", {
            "file": "./src/config" 
        } ]
    ]
}
```

## Examples

All of the following example will use these plugin config values:
```json
{
  "values": {
    "process.env.NODE_ENV": "production",
    "isProduction": true,
    "CONFIG": {
      "API_HOST": "https://your.domain/"
    },
    "VAR": null,
    "TEST_STUFF": "/^foo$/igm",
    "didRTFM": true
  }
}
```

### Variable declarations

Input
```ecmascript 6
const initState = { initialized: true }
const appState = { global: { ...CONFIG, isProduction }, ...initialized }
const thisWorks = asWell = isProduction
const VAR = { CONFIG }
```

Output
```ecmascript 6
const initState = { initialized: true }
const appState = { global: { ...{ "API_HOST": "https://your.domain" } }, ...initialized }
const thisWorks = asWell = true
const VAR = { CONFIG: { "API_HOST": "https://your.domain" } }
```

### Assignments and calls

Input
```ecmascript 6
const myObject = {}, isProduction = false
myObject.foo = VAR
myObject.isProduction = isProduction

new RegExp(TEST_STUFF)

Object.keys(CONFIG).map(key => console.log(key))
```

Output
```ecmascript 6
const myObject = {}, isProduction = false
myObject.foo = null
myObject.isProduction = isProduction

new RegExp("/^foo$/igm")

Object.keys({ "API_HOST": "https://your.domain" }).map(key => console.log(key))
```

### Exports

Input
```ecmascript 6
export { CONFIG }
export default isProduction
```

Output
```ecmascript 6
const CONFIG = { "API_HOST": "https://your.domain" }
export { CONFIG }

const isProduction = true
export default isProduction
```

### Conditionals evaluation

Input
```ecmascript 6
if (isProduction) { doThings() }
else { doSomthingElse() }

if (! isProduction) { whatEver() }
else { sorcery() }

const youAre = didRTFM ? 'awesome' : 'lazy'
const complicated = didRTFM && ! isProduction

const canNotEvaluate = didRTFM && require('./aScript')
```

Output
```ecmascript 6
{ doThings() }
{ sorcery() }

const youAre = 'awesome'
const complicated = false
const canNotEvaluate = true && require('./aScript')
```


## Options

| Option key                | Description                                                                                                                                                                                                                                                 | Type      | Default value |
|---------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------|----------------|
| **values**                | Object with keys and values, that should be replaced.<br> **Please note that only the top level keys will be used for replacement.**<br> <br> If you want to define `process.env.NODE_ENV` for instance, <br>use `{ "process.env.NODE_ENV": "production" }` | `object`  | `{}`           |
| **file**                  | Instead of passing an object, you can also reference a file. <br>The given path has to be relative to your babel config and <br>should use `module.exports = { API_HOST: "https://..." }`                                                                   | `string`  | `undefined`    |
| **memberExpressions**     | You can disable the processing of member expressions (`node.env.NODE_ENV`).                                                                                                                                                                                 | `boolean` | `true`         |
| **exportExpressions**     | Toggles the processing of export expressions (`export { MY_CONFIG }` or `export default MY_CONFIG`). **Note:** If you enable this option, you will have to set the babel parser option `allowUndeclaredExports` to `true`.                                  | `boolean` | `true`         |
| **evaluateConditionals**  | Toggles the evaluation and simplification of If Statements, Unary Statements and so on.                                                                                                                                                                     | `boolean` | `true`         |
| **verbose**               | If set to true, some information about the replaced nodes and their types will be printed to the console.                                                                                                                                                   | `boolean` | `false`        |

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