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

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

    You can have one webpack config, built upon another, and add a few plugins (and/or change output names, etc) in the latter one:

    This webpack.release.config.js makes use of a webpack.config (development version), but uses more plugins...

    process.env.NODE_ENV = 'release';
    
    const config = require('./webpack.config'),
        webpack = require('webpack');
    
    config.output.filename = 'app.min.js';
    // use another plugin, compare to the basic version ←←←
    config.plugins.push(new webpack.optimize.UglifyJsPlugin({
        minimize: true
    }));
    
    module.exports = config;
    

    Also see here for a full example.

提交回复
热议问题