Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to Compress into gzip in webpack

I want to compress my react app into gzip,actually it is 2.2 mb so I used compression-webpack-plugin but i'm unable to compress
my webpack.config.js

const webpack = require('webpack');
const path = require('path');
const fs = require("fs")
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const CompressionPlugin = require('compression-webpack-plugin');

const VENDOR_LIBS =[
    'antd','axios','moment','rc-time-picker','react',
    'react-dom','react-ga','react-google-maps','react-loadable',
    'react-redux','react-router','react-router-dom','recompose','redux','redux-thunk'
]
module.exports = (env) => {
    const isProduction = env === 'production';
    const CSSExtract = new ExtractTextPlugin('styles.css');

    return {
        node: {
            fs: 'empty',
            child_process: 'empty',
          },
        entry: {
            bundle:'./src/app.js',
            vendor:VENDOR_LIBS
        },
        output: {
            path: path.join(__dirname, 'public'),
            filename: '[name].js'
        },
        module: {
            rules: [{
                loader: 'babel-loader',
                test: /\.js$/,
                exclude: /node_modules/
            }, {
                test: /\.s?css$/,
                use: CSSExtract.extract({
                    use: [
                        {
                            loader: 'css-loader',
                            options: {
                                sourceMap: true
                            }
                        },
                        {
                            loader: 'sass-loader',
                            options: {
                                sourceMap: true
                            }
                        }
                    ]
                })
            },{
                test: /\.(gif|svg|jpg|png|ttf|eot|woff(2)?)(\?[a-z0-9=&.]+)?$/,
                loader: "file-loader",
            }],
        },
        
        plugins: [
            CSSExtract,
            new webpack.optimize.CommonsChunkPlugin({
                name:'vendor'
            }),
            new HtmlWebpackPlugin({
                template:'src/index.html'
            }),
            new CompressionPlugin(),
            new BundleAnalyzerPlugin()
            
        ],
        devtool: isProduction ? 'source-map' : 'inline-source-map',
        devServer: {
            contentBase: path.join(__dirname, 'public'),
            historyApiFallback: true,
            host:'0.0.0.0',
            disableHostCheck: true,
        }
    };
};

it showing me error

TypeError: Cannot read property 'emit' of undefined
at CompressionPlugin.apply (/media/.........
if i add addition options to the CompressionPlugin like

new CompressionPlugin({
  asset: '[path].gz[query]',
  algorithm: 'gzip',
  test: /\.js$|\.css$|\.html$|\.eot?.+$|\.ttf?.+$|\.woff?.+$|\.svg?.+$/,
  threshold: 10240,
  minRatio: 0.8
  })

it showing me error

throw new ValidationError(ajv.errors, name);
ValidationError: Compression Plugin Invalid Options
options should NOT have additional properties

how can I solve this issue or is there any other way to gzip my app.

like image 999
zulqarnain Avatar asked Sep 11 '18 11:09

zulqarnain


3 Answers

Change "asset" to "filename".

new CompressionPlugin({
  filename: '[path].gz[query]',
  algorithm: 'gzip',
  test: /\.js$|\.css$|\.html$|\.eot?.+$|\.ttf?.+$|\.woff?.+$|\.svg?.+$/,
  threshold: 10240,
  minRatio: 0.8
})
like image 159
Cyro Dubeux Avatar answered Oct 28 '22 19:10

Cyro Dubeux


Your problem is that you need to be running with webpack 4 and node has to be > 6.9.0 as stated in the docs:

"This module requires a minimum of Node v6.9.0 and Webpack v4.0.0."

Your configuration is also wrong:

new CompressionPlugin({
  filename: '[path].gz[query]',
  algorithm: 'gzip',
  test: /\.js$|\.css$|\.html$|\.eot?.+$|\.ttf?.+$|\.woff?.+$|\.svg?.+$/,
  threshold: 10240,
  minRatio: 0.8
})
like image 33
PlayMa256 Avatar answered Oct 28 '22 21:10

PlayMa256


[email protected] has breaking changes.

You either have to upgrade to webpack 4 or use [email protected].

And also the asset option is deprecated, use filename instead.

like image 40
ADIL Avatar answered Oct 28 '22 19:10

ADIL