iphonic
iphonic

Reputation: 12719

ReactJS - What makes bundle.js size big

I have recently completed a project on ReactJS, the only problem with the project is its production bundle.js size its around 30MB, that's insanely huge. I have used BundleAnalyzerPlugin plugin to analyse it, it looks like this

enter image description here

The bigger the block, bigger is the size.

node_modules=10 MB each visible small chunks are almost = 1MB

My webpack config looks like this

webpack.base.config.js

var webpack = require('webpack');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
module.exports = {
  entry: [
    './src/index.js'
  ],
  output: {
    path: __dirname,
    publicPath: '/',
    filename: 'bundle.js'
  },
  noParse:[
    './~/xlsx/jszip.js',
  ],
  externals:[
    {'./jszip': 'jszip'}
  ],
  module: {
    loaders: [{
      test: /\.js$/,
      exclude: /node_modules/,
      loader: 'babel',
      query: {
        presets: ['react', 'es2015', 'stage-1']
      }
    },
    {
      test: /\.json$/,
      loader: "json-loader"
    },
    {
      test: /\.(html)$/,
      loader: "html-loader",
      options:{
        attrs:[':data-src!./src/components/preview/preview_html',':data-src!./src/components/landing/loader/index.html']
      }
    },
    {
      test: /\.(ttf|gif|eot|svg|woff|woff2|png)(\?.+)?$/,
      loader: 'file-loader?name=[hash:12].[ext]'
    },
    {
      test: /\.css?$/,
      loader: 'style-loader!css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]'
    },
    {
      test: /\.scss?$/,
      loader: 'style-loader!css-loader!sass-loader'
    }]
  },
  resolve: {
    root: __dirname,
    alias:{
      //Have set of aliases
    },
    extensions: ['','.json', '.js', '.jsx','.ejs']
  },
  plugins: [
        new webpack.ProvidePlugin({
           $: "jquery",
           jQuery: "jquery"
       }),
       new BundleAnalyzerPlugin()
  ],
  devServer: {
    historyApiFallback: true,
    contentBase: './',
    watchContentBase: true,
  },
  devtool:'cheap-module-eval-source-map'
};

webpack.prod.config.js

const merge = require('webpack-merge');
const baseConfig = require('./webpack.base.config.js');
var webpack = require('webpack');
var CompressionPlugin = require('compression-webpack-plugin');

var HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = merge(baseConfig,{
  plugins: [
      new webpack.DefinePlugin({
        'process.env': {
          'NODE_ENV': JSON.stringify('production')
        }
      }),
       new HtmlWebpackPlugin({
          template:'index.ejs',
          hash: false,
          baseHref: 'xxxx',
          scripts: [
            {
              src: 'bundle.js',
              type: 'text/javascript'
            }
          ]
      })
  ]
});

I have imported few css directly from node_modules into index.ejs file which looks like this

<link rel="stylesheet" href="/node_modules/react-bootstrap-switch/dist/css/bootstrap3/react-bootstrap-switch.css">
    <link rel="stylesheet" href="/node_modules/draft-js/dist/Draft.css">
    <link rel="stylesheet" href="/node_modules/react-select-plus/dist/react-select-plus.css">
    <link rel="stylesheet" href="/node_modules/react-day-picker/lib/style.css">
    <link rel="stylesheet" href="/node_modules/react-draft-wysiwyg/dist/react-draft-wysiwyg.css">
    <script src="node_modules/xlsx/dist/shim.min.js"></script>
    <script src="node_modules/xlsx/dist/xlsx.full.min.js"></script>

Need help to reduce the size, and find mistakes in config or implementation..

Thanks.

Upvotes: 1

Views: 2751

Answers (1)

Jackyef
Jackyef

Reputation: 5002

I assume you are using webpack 4. I noticed that you haven't set mode: 'production' in your production webpack config. That option will do optimisations including using terser to minify the bundle output. Also, set NODE_ENV to production, some libraries relies on the value of that variable directly.

I suggest starting with that, and also read this: https://webpack.js.org/guides/production/

After that is done, you can start identifying which 3rd party modules that are taking the most size in your vendor bundle. Then, you can consider code-splitting them so that they will be in their own separate chunks, and you can load them only when needed. You can read about code-splitting here: https://webpack.js.org/guides/code-splitting/

Upvotes: 3

Related Questions