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

后端 未结 7 2196
死守一世寂寞
死守一世寂寞 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:10

    You can use mode argument in webpack to pass development/production value to webpack config and then conditionally load plugins.

    NPM Script:

    "start": "webpack --watch --mode=development",
    "build": "webpack --mode=production",
    

    webpack.config.js:

    module.exports = (env, argv) => {
      console.log("mode: ", argv.mode);
    
      const isDev = argv.mode === "development";
    
      const pluginsArr = [new CleanWebpackPlugin()];
    
      // load plugin only in development mode
      if (isDev) {
        pluginsArr.push(new ExtensionReloader({}));
      }
      return {
        entry: {},
        devtool: isDev ? "inline-source-map" : "", // generate source code only in development mode
    
        plugins: pluginsArr,
        output: {},
      };
    };
    

提交回复
热议问题