A javascript conditional compiling loader for webpack.
This loader use <JS_IF condition={}></JS_IF>
XML tag in comment;
- Mode 1
/*<JS_IF condition={env == 'op'}>*/
console.log('op');
/* </JS_IF>*/
/*<JS_IF condition={env != 'op'}>*/
console.log('not op');
/* </JS_IF>*/
when loader.options.env == 'op', Output:
console.log('op');
when loader.options.env != 'op', Output:
console.log('not op');
- Mode 2 -- Nested condition use Namesapce
/*<JS_IF:a condition={env == 'op'}>*/
console.log('op');
/*<JS_IF:b condition={sum > 10}>*/
console.log('sum > 10');
/* </JS_IF:b>*/
/* </JS_IF:a>*/
when loader.options.env == 'op' && sum = 20, Output:
console.log('op');
console.log('sum > 10');
when loader.options.env == 'op' && sum = 10, Output:
console.log('op');
npm i -D js-conditional-loader
module: {
rules: [
{
test: /\.jsx$/,
use: [
//step-2
'babel-loader',
//step-1
{
loader: 'js-conditional-loader',
options: {
env: process.env.NODE_ENV
}
},
]
}
]
}