16

I'm new to Webpack. I try to use Webpack for two main reasons :

  • Component management : using require(...)
  • Performance : smallest size possible, less requests possible to the server.

But with the application I just started (there are currently something like four React components only ), the bundle.js file generated by Webpack is 3.87Mb!!!

I'm pretty sure Webpack bundles things I won't ever need. I'd like to know how to optimize the generated file... How do I "debug" Webpack's process?

My webpack.config.js :

var webpack = require("webpack");

module.exports = {
    entry: "./app/bootstrap.js",
    output: {
        path: __dirname,
        publicPath: "/public/",
        filename: "bundle.js"
    },
    module: {
        loaders: [
            {
                test: /\.css$/,
                loader: "style!css"
            },
            {
                test: /\.js$/,
                exclude: /(node_modules|bower_components)/,
                loader: 'babel-loader'
            },
            {
                test: /\.js$/,
                include: /vis/,
                loader: 'babel-loader'
            },
            {
                test: /\.(png|woff|woff2|eot|ttf|svg|gif|jpg|jpeg|bmp)(\?.*$|$)/,
                loader: 'url-loader?limit=100000'
            }
        ]
    },
    plugins: [
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery",
            "window.jQuery": "jquery"
        }),
        new webpack.optimize.UglifyJsPlugin({minimize: true})
    ]

};

and package.json :

{
  "name": "XXXXX",
  "version": "1.0.0",
  "main": "",
  "scripts": {
    "dev": "webpack --progress --colors --watch --devtool eval",
    "prod": "webpack --progress --colors"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "alt": "^0.16.10",
    "bootstrap": "^3.3.5",
    "es6-promise": "^2.3.0",
    "i18next-client": "^1.10.2",
    "jquery": "^1.10.2",
    "react": "^0.13.3",
    "react-router": "^0.13.3",
    "toastr": "^2.1.0",
    "vis": "^4.4.0"
  },
  "devDependencies": {
    "css-loader": "^0.15.1",
    "babel-core": "^5.6.18",
    "babel-loader": "^5.3.1",
    "es6-module-loader": "^0.17.3",
    "extract-text-webpack-plugin": "^0.8.2",
    "file-loader": "^0.8.4",
    "node-libs-browser": "^0.5.2",
    "webpack": "^1.9.13",
    "url-loader": "^0.5.6",
    "style-loader": "^0.12.3",
    "webpack-dev-server": "^1.9.0"
  }
}

Any help on how to optimize the generated bundle.js?

electrotype
  • 8,342
  • 11
  • 59
  • 96

2 Answers2

12

simply

webpack --production
or
webpack -p

take a look at http://www.jonathan-petitcolas.com/2015/05/15/howto-setup-webpack-on-es6-react-application-with-sass.html

Whisher
  • 31,320
  • 32
  • 120
  • 201
  • Down to 2.01Mb using this flag, thanks! It's still big though. I'm going to investigate why.... I guess I may be part of the problem! ;-) – electrotype Jul 16 '15 at 22:53
  • 9
    For the records : what really made a big difference was removing the "--devtool eval" flag so no source map files are generated. (Also : is there a missing "c" in your "--prodution" flag?) – electrotype Jul 17 '15 at 11:09
  • This "--production" flag does at help at all in my project. The size went from 6.47M to 6.47M. I have my question here http://stackoverflow.com/questions/40093936/react-redux-bundle-js-is-too-big – Dustin Sun Oct 17 '16 at 19:25
6

Add this in your production config file:

plugins: [
    new webpack.DefinePlugin({
      'process.env': {
        // This has effect on the react lib size
        'NODE_ENV': JSON.stringify('production'),
      }
    }),
    new ExtractTextPlugin("bundle.css", {allChunks: false}),
    new webpack.optimize.AggressiveMergingPlugin(),
    new webpack.optimize.OccurrenceOrderPlugin(),
    new webpack.optimize.DedupePlugin(),
    new webpack.optimize.UglifyJsPlugin({
      mangle: true,
      compress: {
        warnings: false, // Suppress uglification warnings
        pure_getters: true,
        unsafe: true,
        unsafe_comps: true,
        screw_ie8: true
      },
      output: {
        comments: false,
      },
      exclude: [/\.min\.js$/gi] // skip pre-minified libs
    }),
    new webpack.IgnorePlugin(/^\.\/locale$/, [/moment$/]), //https://stackoverflow.com/questions/25384360/how-to-prevent-moment-js-from-loading-locales-with-webpack
    new CompressionPlugin({
      asset: "[path].gz[query]",
      algorithm: "gzip",
      test: /\.js$|\.css$|\.html$/,
      threshold: 10240,
      minRatio: 0
    })
  ],
Nissa
  • 4,636
  • 8
  • 29
  • 37
Khalid Azam
  • 1,615
  • 19
  • 17
  • for Ignore plugin to work with Webpack 2 use `new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)` – Amio.io Apr 19 '17 at 13:11