penv.macro
v0.4.0
Published
<!-- START doctoc generated TOC please keep comment here to allow auto update --> <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
Downloads
1,235
Readme
penv.macro
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.
Language
Installation
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.
Usage
Once you've configured babel-macros
you can import/require
penv.macro
.
Basic
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')()
With default value
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'
Advanced
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.
Issues
Click here to open a new issue.