Assets compression in Rails 6 with Webpack not working

Viewed 808

I changed my config/webpack/production.js to:

process.env.NODE_ENV = process.env.NODE_ENV || 'production'
const CompressionPlugin = require('compression-webpack-plugin');

const environment = require('./environment')

module.exports = environment.toWebpackConfig()

module.exports.plugins.push(
    new CompressionPlugin({
        asset: "[path].gz[query]",
        algorithm: "gzip",
        test: /\.(js|css)$/,
        threshold: 10240,
        minRatio: 0.8
    })
);

But this did not compress anything in my assets. Am I adding the plugin in the wrong way or missing something? Thanks.

1 Answers

Not sure exactly what part of webpacker minified my code, but the problem was that I was running webpack manually but not in production mode, this ignored anything related to compression. I added to my config/webpack/production.js:

module.exports = environment.toWebpackConfig()

module.exports.optimization = {
    minimize: true,
    minimizer: new UglifyJsPlugin({
        test: /\.js$/,
        exclude: /node_modules/,
        sourceMap: true,
        uglifyOptions: {
            compress: {},
            mangle: true,
        }
    })
}

That alone did not solve the issue, only after I added the production mode flag, the compression started happening:

bin/webpack  --mode production
Related