https://github.com/kaelzhang/env-to-code
The module to parse `process.env[SOME_KEY]` into JavaScript variable or JavaScript code, especially, which is especially useful for `webpack.EnvironmentPlugin`
https://github.com/kaelzhang/env-to-code
environment-variables stringify webpack
Last synced: about 2 months ago
JSON representation
The module to parse `process.env[SOME_KEY]` into JavaScript variable or JavaScript code, especially, which is especially useful for `webpack.EnvironmentPlugin`
- Host: GitHub
- URL: https://github.com/kaelzhang/env-to-code
- Owner: kaelzhang
- License: other
- Created: 2019-03-14T12:25:21.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-03-17T12:29:34.000Z (over 7 years ago)
- Last Synced: 2026-04-07T21:38:56.266Z (2 months ago)
- Topics: environment-variables, stringify, webpack
- Language: JavaScript
- Homepage:
- Size: 11.7 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: HISTORY.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/kaelzhang/env-to-code)
[](https://codecov.io/gh/kaelzhang/env-to-code)
# env-to-code
The module to parse `process.env[SOME_KEY]` into JavaScript variable or JavaScript code, especially, which is very useful for [`webpack.EnvironmentPlugin`](https://webpack.js.org/plugins/environment-plugin/)
## Install
```sh
$ npm i env-to-code
```
## Usage
```sh
# bash
export FOO=bar
export BAZ=1
export DEBUG=false
```
```js
import {
js,
code
} from 'env-to-code'
js(process.env.FOO) === 'bar' // true
code(process.env.FOO) === '"bar"' // true
js(process.env.BAZ) === 1 // true
js(process.env.DEBUG) === false // true
code(process.env.DEBUG) === 'false' // true
// But
JSON.stringify(process.env.DEBUG) === '"false"' // true
new webpack.DefinePlugin({
'process.env.DEBUG': code(process.env.DEBUG)
})
```
## js(s, config?)
- **s** `string` environment variable string
- **config** `?Object` optional config
- **testJSON** `?boolean=false` whether to test if `s` is a JSON. `testJSON` takes effect ahead of `arrayDelimiter`.
- **arrayDelimiter** `?string=','` by default, it will try to split the env variable into array with `arrayDelimiter`. To disable this feature, set the option to `false` or `''`
Parses the environment variable into JavaScript variable.
```js
js('English, Chinese') // ['English', 'Chinese']
js('English') // 'English'
js('English, Chinese', {
arrayDelimiter: false
})
// 'English, Chinese'
```
**PAY ATTENTION THAT** with `testJSON=false` and `arrayDelimiter=','` which are the default options, method `js()` will split JSON array into an unexpected result, for example:
```js
js('["a","b"]')
// [
// '["a"',
// '"b"]'
// ]
```
So, if environment variables contains JSON strings, it is better to set `testJSON` to `true`:
```js
js('["a","b"]', {testJSON: true})
// ['a', 'b']
```
## code(s, config?)
This method has the same arguments as `js()`, and parses `s` into JavaScript code string.
So it is useful for [`webpack.EnvironmentPlugin`](https://webpack.js.org/plugins/environment-plugin/) or writing JavaScript code into files.
```js
new webpack.DefinePlugin({
'process.env.NODE_ENV': code(process.env.NODE_ENV),
'process.env.DEBUG': code(process.env.DEBUG)
})
```
or
```js
// write.js
fs.writeFileSync('foo.js', `module.exports = {debug:${code(process.env.DEBUG)}}`)
```
```sh
DEBUG=true node write.js
```
And in foo.js
```js
module.exports = {debug:true}
```
## License
MIT