Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react antd large production bundle

Webpack outputs a very large bundle: 1.5MB minimized.

I import single components according to the docs, using imports 'antd/lib/...'
These are my imports:

import React from "react";
import ReactDOM from "react-dom";

import TreeSelect from 'antd/lib/tree-select';
const TreeNode = TreeSelect.TreeNode;
import 'antd/lib/tree-select/style/css';

import moment from 'moment';
import LocaleProvider from 'antd/lib/locale-provider';

import DatePicker from 'antd/lib/date-picker';
import 'antd/lib/date-picker/style/css'
const { RangePicker } = DatePicker;

import Menu from 'antd/lib/menu';
import 'antd/lib/menu/style/css'

import Dropdown from 'antd/lib/dropdown';
import 'antd/lib/dropdown/style/css';

import Modal from 'antd/lib/modal';
import 'antd/lib/modal/style/css';

import './styles.css';

I'm using just 5 components. Does it make sense the bundle size is that big? My own code is fairly small - around 15KB without minification.

UPDATE: After using IgnorePlugin() for moment, my bundle size got 300KB smaller. Still 1.5MB is very big.

Bellow is webpack config files.

webpack.config.js:

  const config = {
        entry: {
            main: path.resolve(SRC_DIR, "index.js"),
        },
        mode: 'development',
        devtool: 'cheap-eval-source-map',
        output: {
            path: DIST_DIR,
            filename: "bundle.js",
            publicPath: "/static/bundles/",
        },
        resolve: {
            extensions: [".js", ".json", ".css"]
        },
        module: {
            rules: [
                {
                    test: /\.js?/,
                    include: SRC_DIR,
                    loader: "babel-loader",
                    options: {
                        babelrc: true
                    }
                },
                {
                    test: /\.css$/,
                    use: [
                        "style-loader", "css-loader"
                    ]
                }
            ]
        },
      plugins: [
        new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NamedModulesPlugin(),
        //new webpack.LoaderOptionsPlugin({ debug: true}),
      ]
    };

    module.exports = config;

webpack.prod.js (used to make the bundle):

const common = require('./webpack.config.js');
   module.exports = Object.assign(common, {
        entry: {
            main: path.resolve(SRC_DIR, "index.js"),
        },
        mode: 'production',
        devtool: false,
        output: {
            publicPath: '/static/dist/',
            path: DIST_DIR,
            filename: 'bundle.js'
        },
        plugins: [
            new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
            new BundleAnalyzerPlugin()
        ]
    });
like image 557
user3599803 Avatar asked Mar 05 '26 23:03

user3599803


2 Answers

Some of the components of Antd date Time functionality like RangePicker also use moment.js lib, so it can become quite heavy.

UPD:

try to optimize it using plugins:

new webpack.optimize.UglifyJsPlugin({
  compress: {
    warnings: false,
    screw_ie8: true,
    conditionals: true,
    unused: true,
    comparisons: true,
    sequences: true,
    dead_code: true,
    evaluate: true,
    if_return: true,
    join_vars: true,
  },
  comments: false,
  sourceMap: true,
  minimize: true,
  exclude: [/\.min\.js$/gi],
}),
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.AggressiveMergingPlugin(),
new webpack.optimize.ModuleConcatenationPlugin(),
like image 188
Roman Haivaronski Avatar answered Mar 07 '26 11:03

Roman Haivaronski


UPDATE

Antd 4 is out! It solves the SVG icon problem and it's 228kb smaller than the 3.x version. Updating to version 4.0.X is gonna bring a massive reduction in our bundle size.

OLD ANSWER

At the moment, a huge part of antd dist is svg icons. 16.3% to be exact (current version).

I believe we don't have any officials ways to deal with icons already, but a workaround exists. You can find it here.

Thus, if you remove this and moment locale you can reduce the lib size about 20%.

They are working on reducing the lib size in the antd 4 for, which it's already on alpha (with already less 130kb minified).

like image 35
Arthur Costa Avatar answered Mar 07 '26 13:03

Arthur Costa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!