The penv.macro
aims to write configurations of multiple environments in one
file simultaneously and remove irrelevant configuration codes from the final bundle. If you want to know more about this plugin, please see here.
This module is distributed via npm which is bundled with node and should be installed as one of your project's devDependencies
:
yarn add penv.macro --dev
or
npm install penv.macro --save-dev
You'll also need to install and configure babel-macros if you haven't already.
Once you've configured babel-macros
you can import/require
penv.macro
.
import env from 'penv.macro'
const BASE_URL = env({
development: 'https://development.example.com',
staging: 'https://staging.example.com',
production: (() => 'https://production.example.com')(),
})
// Assume that the value of `process.env.NODE_ENV` is `production`
const BASE_URL = (() => 'https://production.example.com')()
import env from 'penv.macro'
const BASE_URL = env(
{
development: 'https://development.example.com',
staging: 'https://staging.example.com',
production: (() => 'https://production.example.com')(),
},
'defaultValue',
)
// Assume that the value of `process.env.NODE_ENV` didn't match anything
const BASE_URL = 'defaultValue'
The default node environment variable used to determine which property value would
be matched is NODE_ENV
, if you want to change the default setting, you can customize
it by configuring babel-plugin-macros
.
For example in <ProjectRoot>/package.json
:
{
"dependencies": {},
"babelMacros": {
"penv": {
"targetName": "NODE_ENV"
}
}
}
To know more details, please go to the above link.
Click here to open a new issue.