Webpack: How can we *conditionally* use a plugin?

后端 未结 7 2174
死守一世寂寞
死守一世寂寞 2021-02-07 00:31

In Webpack, I have the following plugins:

plugins: [
        new ExtractTextPlugin(\'styles.css\'),
        new webpack.optimize.UglifyJsPlugin({
            com         


        
相关标签:
7条回答
  • 2021-02-07 01:32

    You can use noop-webpack-plugin (noop means no operation):

    const isProd = process.env.NODE_ENV === 'production';
    const noop = require('noop-webpack-plugin');
    // ...
    plugins: [
        isProd ? new Plugin() : noop(),
    ]
    

    Or better/recommended solution with no additional module:

    const isProd = process.env.NODE_ENV === 'production';
    // ...
    plugins: [
        isProd ? new Plugin() : false,
    ].filter(Boolean)
    // filter(Boolean) removes items from plugins array which evaluate to
    // false (so you can use e.g. 0 instead of false: `new Plugin() : 0`)
    
    0 讨论(0)
提交回复
热议问题