react-dom blowing out webpack bundle size MASSIVELY
Asked Answered
H

4

14

This has got to be one of the strangest issues with webpack i have ever come across...

Check out this bundle breakdown: enter image description here react 116.01KB - fair enough

react-dom 533.24KB - seriously WTF

I thought it may be a corruption in my dependencies but nuking node_modules and reinstalling doesn't have any effect. I guess it's something to do with the way webpack is bundling it but i'm lost for ideas. The way i'm handing .js imports is pretty stock standard.

// webpack.config.js
const path = require('path');
// const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const Dashboard = require('webpack-dashboard');
const DashboardPlugin = require('webpack-dashboard/plugin');

const dashboard = new Dashboard();

module.exports = {
  context: path.join(__dirname, 'src'),
  entry: {
    bundle: './index.js',
  },
  output: {
    filename: 'bundle.js',
    path: path.join(__dirname, 'build'),
  },
  module: {
    rules: [
      {
        test: /\.html$/,
        use: 'file-loader?name=[name].[ext]',
      },
      {
        test: /.scss$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: [
            'css-loader',
            'postcss-loader',
          ],
        }),
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: 'babel-loader',
      },
    ],
  },
  plugins: [
    // new BundleAnalyzerPlugin(),
    new ExtractTextPlugin('styles.css'),
    new DashboardPlugin(dashboard.setData),
  ],
  devServer: {
    quiet: true,
  },
};

// .babelrc
{
  "presets": [
    "react",
    "es2015"
  ],
  "plugins": ["transform-object-rest-spread"]
}
Henryk answered 10/3, 2017 at 10:7 Comment(0)
H
9

http://elijahmanor.com/react-file-size/

In v15.4.0 the file size of react-dom grew from 1.17kB to 619.05kB. Which means my webpack setup isn't doing anything wrong bundling files. The reason why this module grew so large is because code was transferred from the react module.

Henryk answered 10/3, 2017 at 12:15 Comment(1)
The values being listed are development bundle sizes. Generally a production or minimized build is shipped, probably gzip too, and so react.min.js.gz + react-dom.min.js.gz <50kb. Using "mode": "production" in the webpack build (now the default in webpack) will produce these expected smaller bundle sizesGotthelf
Z
6

I had to change my webpack.config.js, from

devtool: 'inline-source-map'

enter image description here

to

devtool: 'source-map'

enter image description here

Now it generates a much smaller .js + a separate .js.map file, for each of the chunks.

Notice the JS size is even less than react-dom.production.min.js in node_modules: enter image description here

Zomba answered 4/8, 2021 at 14:6 Comment(0)
D
2

If you look into the corresponding folders under the node_modules folder, and note the file sizes, you'll see that there's nothing to be surprised about:

react

react-dom

That is, the size of the bundle grows noticeably because the size of react-dom.js is large.

Dowry answered 10/3, 2017 at 11:29 Comment(2)
I've got an old project that uses [email protected] and is 1KB... What has caused such a big jump?Henryk
The [email protected] was just linking react package.Retrieval
C
-1

Add this following commands at plugins to minify your imports:

new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.DefinePlugin(GLOBALS),
new webpack.optimize.UglifyJsPlugin(),

You should create a file or option to production bundle to use this plugins

Coimbra answered 10/3, 2017 at 10:20 Comment(6)
I'm aware that my bundle can be compressed down. My concern is the size of react-dom relative to react... I get a smaller bundle in production mode but react-dom is still not right.Henryk
I had a similar problem and solved with this plugins, in production react-dom becames 30k. To solve the 2mb compressed at my production bundle, I used route code splittingCosta
I'm using Webpack 2. It does OccurrenceOrderPlugin and UglifyJsPlugin by default when you run webpack -pHenryk
I am using Wepack 2 either. When I change -p for theses plugins my bundle reduced from 8mb to 2mbCosta
The plugins don't do anything for me... webpack.js.org/guides/migrating/…Henryk
Terser is superior to uglify. use terser-webpack-pluginDisenchant

© 2022 - 2024 — McMap. All rights reserved.