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
Install
$ npm i env-to-code
Usage
# bash export FOO=barexport BAZ=1export DEBUG=false
=== 'bar' // true === '"bar"' // true === 1 // true === false // true === 'false' // true // ButJSON === '"false"' // true 'process.env.DEBUG':
js(s, config?)
- s
string
environment variable string - config
?Object
optional config- testJSON
?boolean=false
whether to test ifs
is a JSON.testJSON
takes effect ahead ofarrayDelimiter
. - arrayDelimiter
?string=','
by default, it will try to split the env variable into array witharrayDelimiter
. To disable this feature, set the option tofalse
or''
- testJSON
Parses the environment variable into JavaScript variable.
// ['English', 'Chinese'] // 'English' // '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:
// [// '["a"',// '"b"]'// ]
So, if environment variables contains JSON strings, it is better to set testJSON
to 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
or writing JavaScript code into files.
'process.env.NODE_ENV': 'process.env.DEBUG':
or
// write.jsfs
DEBUG=true node write.js
And in foo.js
moduleexports = debug:true
License
MIT