npm.io
1.0.0 • Published 7 years ago

js-conditional-loader

Licence
MIT
Version
1.0.0
Deps
0
Size
4 kB
Vulns
0
Weekly
0

js-conditional-loader

A javascript conditional compiling loader for webpack.

Usage

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');

Setup

    npm i -D js-conditional-loader
Config in webpack
module: {
    rules: [
        {
            test: /\.jsx$/,
            use: [
                //step-2
                'babel-loader',
                //step-1
                {
                    loader: 'js-conditional-loader',
                    options: {
                        env: process.env.NODE_ENV
                    }
                },
            ]
        }
    ]
}

Keywords