0.0.1 • Published 3 years ago
babel-plugin-wrapper-condition v0.0.1
babel-plugin-wrapper-dev
A babel plug-in that wraps the code with conditional statements, which is usually used to kill the local debugging code in the online environment
Example
code:
console.log(456)
// wrapper
1 + 1 === 1will be converted to
if (process.env.NODE_ENV !== "production") {
console.log(456)
}
// wrapper
if (process.env.NODE_ENV !== "production") {
1 + 1 === 1
}Parameters
wrapperlogWhether the code should be judged in the external package condition ofconsole.log. (Defaults totrue.)nodeEnvAs shown in the above example, it is a placeholder forprocess.env.${nodeEnv} !== "${deployState}"(Defaults toNODE_ENV.)deployStateAs shown in the above example, it is a placeholder forprocess.env.${nodeEnv} !== "${deployState}"(Defaults toproduction.)commentWhether to enable comments.// warpperwill take effect only when it is true, it works withcommentField, the code block below is wrapped by the condition code when it is true, Note that It is only effective for expression (Defaults tofalse.)expression : [1,2,3] a = 1 1 + 2; -1; function(){}; () => {}; class{}; a; this; super; a::b;commentFieldThis field determines whether the code block below is wrapped by the condition code(Defaults towrapper.)
Usage
you should transmit nodeEnv and production to wrapper your log
if (process.env.${nodeEnv} !== "${deployState}") {
your log
}Via .babelrc (Recommended)
.babelrc.json
{
"plugins": [
[
'babel-plugin-wrapper-condition',
{
wrapperlog: true,
nodeEnv: "NODE_ENV",
deployState: "production",
comment: true,
commentField: 'ONLE_IN_PROD'
}
]
],
}example code
cd example && node code.js
or
export NODE_ENV='production' && cd example && node index.js
export NODE_ENV='dev' && cd example && node index.js0.0.1
3 years ago