Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/FormidableLabs/babel-plugin-transform-define

Compile time code replacement for babel similar to Webpack's DefinePlugin
https://github.com/FormidableLabs/babel-plugin-transform-define

Last synced: 3 months ago
JSON representation

Compile time code replacement for babel similar to Webpack's DefinePlugin

Awesome Lists containing this project

README

        


Babel Plugin Transform Define — Formidable, We build the modern web






npm version





Maintenance Status


Compile time code replacement for babel similar to Webpack's DefinePlugin

***

## Quick Start

```shell
$ npm install --save-dev babel-plugin-transform-define
```

**.babelrc**

```json
{
"plugins": [
["transform-define", {
"process.env.NODE_ENV": "production",
"typeof window": "object"
}]
]
}
```

**.babelrc.js**

```js
// E.g., any dynamic logic with JS, environment variables, etc.
const overrides = require("./another-path.js");

module.exports = {
plugins: [
["transform-define", {
"process.env.NODE_ENV": "production",
"typeof window": "object",
...overrides
}]
]
};
```

## Reference Documentation

`babel-plugin-transform-define` can transform certain types of code as a babel transformation.

##### `Identifiers`

*.babelrc*
```json
{
"plugins": [
["transform-define", {
"VERSION": "1.0.0",
}]
]
}
```

*Source Code*
```js
VERSION;

window.__MY_COMPANY__ = {
version: VERSION
};
```

*Output Code*
```js
"1.0.0";

window.__MY_COMPANY__ = {
version: "1.0.0"
};
```
***
##### `Member Expressions`

*.babelrc*
```json
{
"plugins": [
["transform-define", {
"process.env.NODE_ENV": "production"
}]
]
}
```

*Source Code*
```js
if (process.env.NODE_ENV === "production") {
console.log(true);
}
```

*Output Code*
```js
if (true) {
console.log(true);
}
```
***
##### `Unary Expressions`

*.babelrc*
```json
{
"plugins": [
["transform-define", {
"typeof window": "object"
}]
]
}
```

*Source Code*
```js
typeof window;
typeof window === "object";
```

*Output Code*
```js
'object';
true;
```

***

## License

[MIT License](http://opensource.org/licenses/MIT)

## Maintenance Status

**Stable:** Formidable is not planning to develop any new features for this project. We are still responding to bug reports and security concerns. We are still welcoming PRs for this project, but PRs that include new features should be small and easy to integrate and should not include breaking changes.