Conditionally applies a set of babel plugins based on the result of an expression evaluated at runtime.
Yes, an explanation would be nice. See gajus/flow-runtime#64 for some background for what and why.
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.
MIT.