Redux: Webpack compiles too huge bundle.js (in examples)

Created on 26 Sep 2015  ·  3Comments  ·  Source: reduxjs/redux

I'm new to webpack. Can't figure why is it compiles such a heavy bundle.js. Even for the smalest of your examples, todos-with-undo it's 1.9 MB! For other more complex examples it's about 2.5 MB. There's definitely something wrong in the configuration.
Great job by the way :)

examples infrastructure

Most helpful comment

There's nothing wrong per se, the examples are just not configured for production. We aren't likely to do this here to avoid the maintenance burden—please consult fuller Webpack boilerplates for examples of production configs.

At the very least you'll want to do this in production:

  • change devtool from 'eval' to 'source-map'
  • add webpack.DefinePlugin to set process.env.NODE_ENV to 'production'
  • add webpack.optimize.UglifyJsPlugin for minification
  • make sure you call Babel with NODE_ENV=production so .babelrc doesn't enable react-transform

A typical Webpack production config might look like this:

var path = require('path');
var webpack = require('webpack');

module.exports = {
  devtool: 'source-map',
  entry: [
    './src/index'
  ],
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'bundle.js',
    publicPath: '/static/'
  },
  plugins: [
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.DefinePlugin({
      'process.env': {
        'NODE_ENV': JSON.stringify('production')
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      compressor: {
        warnings: false
      }
    })
  ],
  module: {
    loaders: [{
      test: /\.js$/,
      loaders: ['babel'],
      include: path.join(__dirname, 'src')
    }]
  }
};

Here is a boilerplate including a production mode if you're interested.

All 3 comments

There's nothing wrong per se, the examples are just not configured for production. We aren't likely to do this here to avoid the maintenance burden—please consult fuller Webpack boilerplates for examples of production configs.

At the very least you'll want to do this in production:

  • change devtool from 'eval' to 'source-map'
  • add webpack.DefinePlugin to set process.env.NODE_ENV to 'production'
  • add webpack.optimize.UglifyJsPlugin for minification
  • make sure you call Babel with NODE_ENV=production so .babelrc doesn't enable react-transform

A typical Webpack production config might look like this:

var path = require('path');
var webpack = require('webpack');

module.exports = {
  devtool: 'source-map',
  entry: [
    './src/index'
  ],
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'bundle.js',
    publicPath: '/static/'
  },
  plugins: [
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.DefinePlugin({
      'process.env': {
        'NODE_ENV': JSON.stringify('production')
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      compressor: {
        warnings: false
      }
    })
  ],
  module: {
    loaders: [{
      test: /\.js$/,
      loaders: ['babel'],
      include: path.join(__dirname, 'src')
    }]
  }
};

Here is a boilerplate including a production mode if you're interested.

The best way should be AsyncRoute. We should bundle the template/css/js of a component together to avoid multiple requests, but load it on demand.

Having devtool as inline-source-map instead of just source-map will also bulk the output file by putting the source map (surprise) inline. Obvious, but I overlooked this and it took me an hour to figure out so I'm putting it here for general future reference

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wmertens picture wmertens  ·  4Comments

cloudfroster picture cloudfroster  ·  3Comments

dmitry-zaets picture dmitry-zaets  ·  3Comments

benoneal picture benoneal  ·  3Comments

vraa picture vraa  ·  3Comments