penv.macro
TypeScript icon, indicating that this package has built-in type declarations

0.4.0 • Public • Published

penv.macro

version downloads Build Status MIT License Conventional Commits Babel 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

English | 中文

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.

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i penv.macro

Weekly Downloads

270

Version

0.4.0

License

MIT

Unpacked Size

21.3 kB

Total Files

11

Last publish

Collaborators

  • chengjianhua