Skip to content

A macro used with babel-plugin-macros to write configurations for multiple environments, and remove configurations are irrelevant with the specified environment from your codes finally.

License

Notifications You must be signed in to change notification settings

chengjianhua/penv.macro

Repository files navigation

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

About

A macro used with babel-plugin-macros to write configurations for multiple environments, and remove configurations are irrelevant with the specified environment from your codes finally.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published