Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack 4, exclude 3rd party lib from minification/compression

I have the following in my config:

const viewerConfigProdWeb = merge(common.commonWebConfig, {
output: {
    path: outputPath,
    filename: common.bundleNameWeb
},
devtool: 'source-map',
mode: 'production',
optimization: {
    minimizer: [
        new UglifyJsPlugin({
            cache: true,
            parallel: true,
            sourceMap: true,
            uglifyOptions: {
                compress: false,    //<--- if enabled, causes errors
                ecma: 6,
                mangle: true,
                exclude: path.resolve(__dirname, '../js/vendor/tomtom.min.js'),    // <--- it is already minified, want to exclude it somehow. But this approach doesn't work =(
            }
        })
    ]
}
});

I'm getting runtime errors when I change 'compress' to true in uglifyOptions. These errors appear when webpack tries to optimize a third-party lib, which is already compressed and minified. How can I exclude it from optimization?

Update: according to Sin's answer and readme, changed the optimization section in the config to following:

    optimization: {
    minimizer: [
        new UglifyJsPlugin({
            cache: true,
            parallel: true,
            sourceMap: true,
            exclude: /\.min\.js$/,     //<---- moved up and used regex
            uglifyOptions: {
                compress: true,        //<---- still causes errors when enabled
                ecma: 6,
                mangle: true
            }
        })
    ]
}

This doesn't work either =( Any other ideas?

like image 676
Anton Pilyak Avatar asked Apr 13 '26 22:04

Anton Pilyak


1 Answers

After working for a while, finally found that the exclude option only inspects the output filename instead of source filename. There is a github issue addressing this. You may try the solutions provided by @hulkish there.


Original Answer (not working):

Try add exclude to the top level of UglifyJsPlugin options. And use a RegExp or Array of RegExp instead of the full path. See uglifyjs-webpack-plugin README

like image 52
Sin Avatar answered Apr 16 '26 15:04

Sin