Skip to content

Conditionally applies a set of babel plugins based on the result of an expression evaluated at runtime.

Notifications You must be signed in to change notification settings

scbrady/babel-plugin-conditional

 
 

Repository files navigation

babel-plugin-conditional

Build Status

Conditionally applies a set of babel plugins based on the result of an expression evaluated at runtime.

What?

Yes, an explanation would be nice. See gajus/flow-runtime#64 for some background for what and why.

Installation

yarn add --dev babel-plugin-conditional

Add the following to your .babelrc or babel configuration:

{
  "plugins": [
    ["conditional", {
      "test": "process.env.NODE_ENV === 'development'",
      "consequent": [["some-plugin-you-want-to-run-only-in-dev"]],
      "alternate": [["some-plugin-you-want-to-run-only-in-prod"]]
    }]
  ]
}

Now given an input like this:

export const add = (a, b) => a + b;

it will produce output like this:

export let add;
if (process.env.NODE_ENV === 'development') {
  const _add = (a, b) => {
    return a + b;
  };
  add = _add;
}
else {
  const _add = (a, b) => a + b;
  add = _add;
}

The condition can then be stripped by tools such as webpack or rollup, so you only get one of the branches in production, but library authors can ship a one set of file that covers both debug and production use cases.

Licence

MIT.

Licence

About

Conditionally applies a set of babel plugins based on the result of an expression evaluated at runtime.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%