Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack gzip compressed bundle not being served, the uncompressed bundle is

I am trying out Webpack for the first time. I've been using Gulp with Browserify for some time and am pretty comfortable with it. At this point, I'm just testing out a couple of Webpack plugins. Namely the compression-webpack-plugin. I've never used compression before, so bear with me if I'm making any noob mistake.

Below is my webpack.config.js. The result is I get a main.js, main.js.gz, main.css and index.html. The main.js is injected into the index.html, but if I open index.html in my browser, it serves the uncompressed main.js, not the compressed main.js.gz. I had read that I wouldn't need to include the .gz extension in my script tag, and the html-webpack-plugin doesn't include it, so I figured things are working correctly, yet the uncompressed main.js is served, rather than the compressed one.

var path = require('path');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var CompressionPlugin = require('compression-webpack-plugin');

module.exports = {
  entry: './app/scripts/main.js',
  output: {
    path: path.join(__dirname, 'public'),
    filename: '[name].js',
    chunkFilename: '[id].js'
  },
  module: {
    loaders: [
      {test: /\.scss$/, exclude: /node_modules/, loader: ExtractTextPlugin.extract('style-loader', 'css-loader!sass-loader')},
      {test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader'}
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      hash: true,
      template: 'app/index.html',
      inject: 'body'
    }),
    new ExtractTextPlugin('[name].css'),
    new CompressionPlugin()
  ]
};
like image 385
Patrick Grimard Avatar asked Jan 16 '16 14:01

Patrick Grimard


People also ask

Does Webpack use gzip?

Since static compression involves compressing files ahead of time, webpack settings can be modified to compress assets as part of the build step. CompressionPlugin can be used for this. By default, the plugin compresses the build files using gzip .

How do I compress a Webpack?

webpack.config.jsconst CompressionPlugin = require("compression-webpack-plugin"); module. exports = { plugins: [new CompressionPlugin()], }; And run webpack via your preferred method.


2 Answers

To load main.js.gz instead of main.js in case of included main.js in script-tag, you need to configure your web-server (apache, nginx, etc.)

Remember that configuration should load .js.gz or .js depend on if browser accepts gzip. Web-server can check it in HTTP request header Accept-Encoding: gzip, deflate

In browser devtools you will see main.js in any cases.

like image 136
Dmitry Yaremenko Avatar answered Nov 15 '22 17:11

Dmitry Yaremenko


I'm a little late to this thread, but thought I'd add my 2c. I generated a gz file using webpack, but Apache kept serving the uncompressed one (or erroring if it wasn't present), despite Accept-Encoding being set correctly. Turns out I had to uncomment AddEncoding x-gzip .gz .tgz and reload httpd. For the record, AddType application/x-gzip .gz .tgz was already set, and I'm using Apache 2.4.6 with Chrome 62. Thanks to Dmitry above for nudging me to look at my conf/httpd.conf file.

like image 41
Nate Avatar answered Nov 15 '22 17:11

Nate