In Webpack, how do I enable the plugin according to the command line options?

The following is the plugin property for mine webpack.config.js:

plugins: [
    new webpack.ProvidePlugin({
        $: 'jquery',
        jQuery: 'jquery'
    }),
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.DefinePlugin({
        'process.env': {
            'NODE_ENV': JSON.stringify('production'),
        }
    }),
    new webpack.optimize.UglifyJsPlugin({
        compressor: {
            warnings: false
        }
    }),
    new CompressionPlugin({
        asset: '{file}',
        algorithm: 'gzip',
        regExp: /\.js$|\.html$/,
    })
],

Sometimes I want to turn it off CompressionPlugin, and sometimes I want to turn it on. However, it is inconvenient to create two webpack configuration files. I was wondering if there is a way to enable / disable the plugin dynamically using command line options?

For example, it will be great if I can use webpack --disable-compression-pluginto disconnect CompressionPlugin. Anyone have any ideas on this?

+4
source share
2 answers

Try the yargsnpm module:

npm install yargs --save-dev

In webpack.config.js:

var webpack = require('webpack');
var yargs  = require("yargs");
...

var argv = yargs
    .boolean("disable-compression-plugin")
    .argv;
...

// use argv.disableCompressionPlugin to check the option

module.exports = {
    ...
    plugins: (function(argv) { 
        var plugins = [
            new webpack.ProvidePlugin({
                $: 'jquery',
                jQuery: 'jquery'
            }),
            new webpack.optimize.OccurenceOrderPlugin(),
            new webpack.DefinePlugin({
                'process.env': {
                    'NODE_ENV': JSON.stringify('production'),
                }
            }),
            new webpack.optimize.UglifyJsPlugin({
                compressor: {
                    warnings: false
                }
            })
        ];


        // HERE IS OPTION CONDITION
        if (argv.disableCompressionPlugin) {
            plugins.push(new CompressionPlugin({
                asset: '{file}',
                algorithm: 'gzip',
                regExp: /\.js$|\.html$/,
            }));
        }

        return plugins;
    })(argv),
    ...
};
+4
source

A plugin named name:

module.exports = {
    /.../
    plugins: [

      // enable by default
      { name: 'order', plugin: new webpack.optimize.OccurenceOrderPlugin() },

      // enable by default
      { name: 'provide', plugin: new webpack.ProvidePlugin({ $: "jquery" }) },

      // enable always
      { plugin: new webpack.optimize.UglifyJsPlugin({ compressor: { warnings: false } }) }

    ]
};

module.exports.plugins.forEach( function(p,i) {
    if ( process.argv.indexOf( '--disable-' + p.name + '-plugin' ) === -1 ) {
        module.exports.plugins[i] = p.plugin;
    } else {
        module.exports.plugins[i] = function() {}
    }
});
+2
source

Source: https://habr.com/ru/post/1626138/


All Articles