react-dom blowing out webpack bundle size MASSIVELY

10,330

Solution 1

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.

Solution 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.

Solution 3

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

Share:
10,330

Related videos on Youtube

Toby Flemming
Author by

Toby Flemming

Updated on June 04, 2022

Comments

  • Toby Flemming
    Toby Flemming almost 2 years

    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"]
    }
    
  • Toby Flemming
    Toby Flemming about 7 years
    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.
  • Alessander França
    Alessander França about 7 years
    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 splitting
  • Toby Flemming
    Toby Flemming about 7 years
    I'm using Webpack 2. It does OccurrenceOrderPlugin and UglifyJsPlugin by default when you run webpack -p
  • Alessander França
    Alessander França about 7 years
    I am using Wepack 2 either. When I change -p for theses plugins my bundle reduced from 8mb to 2mb
  • Toby Flemming
    Toby Flemming about 7 years
    The plugins don't do anything for me... webpack.js.org/guides/migrating/…
  • Toby Flemming
    Toby Flemming about 7 years
    I've got an old project that uses [email protected] and is 1KB... What has caused such a big jump?
  • Gajus
    Gajus about 7 years
    The [email protected] was just linking react package.
  • vsync
    vsync about 5 years
    Terser is superior to uglify. use terser-webpack-plugin
  • Colin D
    Colin D almost 4 years
    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 sizes