3

I am trying to reduce the size of my build file ( main.js ) to 500kb or less.

Initially it was 1.2MB and with some code splitting and webpack I managed to reduce it to around 600kb but I need to reduce it by another 100kb. I am fairly new to webpack and I would appreciate any feedback.

Bellow you can find my webpack.prodduction.config.js

const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const webpack = require('webpack')
const UglifyJSPlugin = require('uglifyjs-webpack-plugin')
const LodashModuleReplacementPlugin = require('lodash-webpack-plugin')

console.log(`Building for: ${process.env.NODE_ENV}`)
module.exports = {
  module: {
    rules: [
      {
        test: /.jsx?$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      }, {
        test: /\.css$/,
        loader: 'style-loader!css-loader'
      }, {
        test: /\.(jpe?g|png|gif|woff|woff2|eot|ttf|svg)(\?[a-z0-9=.]+)?$/,
        loader: 'url-loader?limit=100000' },
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          options: {
            presets: [
              ['@babel/preset-env', {
                useBuiltIns: false,
                modules: false
              }]
            ],
            plugins: [
              '@babel/plugin-transform-runtime',
              '@babel/plugin-proposal-class-properties',
              'inline-react-svg'
            ]
          },
          loader: 'babel-loader'
        }
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
      },
      {
        test: /\.module\.s(a|c)ss$/,
        loader: [
          {
            loader: 'style-loader',
            options: {
              attrs: { class: 'BasebotTag' }
            }
          },
          {
            loader: 'css-loader',
            options: {
              modules: true,
              localIdentName: '[name]__[local]___[hash:base64:5]',
              camelCase: true
            }
          },
          {
            loader: 'sass-loader'
          }
        ]
      },
      {
        test: /\.(woff(2)?|ttf|eot|svg|otf)(\?v=\d+\.\d+\.\d+)?$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              name: '[name].[ext]',
              outputPath: 'fonts/'
            }
          }
        ]
      },
      {
        test: /\.s(a|c)ss$/,
        exclude: /\.module.(s(a|c)ss)$/,
        loader: [
          {
            loader: 'style-loader',
            options: {
              attrs: { class: 'BasebotTag' }
            }
          },
          'css-loader',
          {
            loader: 'sass-loader'
          }
        ]
      }
    ]
  },
  resolve: {
    extensions: ['.js', '.jsx', '.scss']
  },
  plugins: [
    new UglifyJSPlugin(),
    new LodashModuleReplacementPlugin(),
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: JSON.stringify('production')
      }
    }),
    new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /en/),
    new webpack.optimize.ModuleConcatenationPlugin(),
    new MiniCssExtractPlugin({
      filename: '[name].[hash].css',
      chunkFilename: '[id].[hash].css'
    }),
    new LodashModuleReplacementPlugin({
      caching: true,
      cloning: true
    })
  ]
}

EDIT: This is my build script:

"build": "NODE_ENV=production webpack --config webpack.production.config",
2
  • medium.com/@rajaraodv/… Commented Nov 21, 2019 at 9:10
  • @DanielLizik Not only this is a 3 year old article, but some of the plugins have been removed. Thanks anyway. Commented Nov 21, 2019 at 9:13

1 Answer 1

2

You are inlining images and other assets up to 0.1MB (100000 Bytes) with url-loader. It would not take many assets with a size of this upper limit to increase your bundle size significantly. In fact, reducing this limit and un-inlining just one asset of 100KB would mean you meet your target bundle size.

I would suggest only inlining assets that are less than 10KB (10000 Bytes). Everything larger than this can be fetched with an HTTP request when it is needed.

{
  test: /\.(jpe?g|png|gif|woff|woff2|eot|ttf|svg)(\?[a-z0-9=.]+)?$/,
  loader: 'url-loader?limit=10000' }, // <= lower the limit here to 10000
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi and thanks for the reply! I have added the change but it is still the same, I am not sure what else to do. I have included my build command as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.