Code Monkey home page Code Monkey logo

penv.macro's Introduction

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

penv.macro's People

Contributors

bohdanbirdie avatar chengjianhua avatar dependabot[bot] avatar huangxizhou avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

penv.macro's Issues

throws if defaultValue is not defined

Given

const foo = pickEnv({
  production: 'foo',
  develop: 'foo_dev'
})

If current env is staging, foo will be null, which is not expected.

Expected

throws if variable is undefined as no env is matched

Suggestion

Maybe an option to control this behavior is just fine

Default value if no env is matched

Consider the following example...

const SERVER_URL = env({
  production: 'https://production.example.com'
}) || 'https://development.example.com'

It might be somewhat nicer if we could do...

const SERVER_URL = env({
  default: 'https://development.example.com',
  production: 'https://production.example.com'
})

It's useful when you know there is only a difference for one (or more) environment. Might be actually an alternative approach to #2. Thoughts?

I might try to provide PR if it sounds interesting.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.