0.0.1 • Published 1 year 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 === 1
will be converted to
if (process.env.NODE_ENV !== "production") {
console.log(456)
}
// wrapper
if (process.env.NODE_ENV !== "production") {
1 + 1 === 1
}
Parameters
wrapperlog
Whether the code should be judged in the external package condition ofconsole.log
. (Defaults totrue
.)nodeEnv
As shown in the above example, it is a placeholder forprocess.env.${nodeEnv} !== "${deployState}"
(Defaults toNODE_ENV
.)deployState
As shown in the above example, it is a placeholder forprocess.env.${nodeEnv} !== "${deployState}"
(Defaults toproduction
.)comment
Whether to enable comments.// warpper
will 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;
commentField
This 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.js
0.0.1
1 year ago