簡體   English   中英

如何進一步減少 React 應用程序的包大小

[英]How to reduce bundle size of react app even further

我正在嘗試將構建文件( main.js )的大小減小到 500kb 或更少。

最初它是 1.2MB,經過一些代碼拆分和 webpack,我設法將其減少到 600kb 左右,但我需要再減少 100kb。 我對 webpack 相當陌生,如果有任何反饋,我將不勝感激。

您可以在下面找到我的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
    })
  ]
}

編輯:這是我的構建腳本:

"build": "NODE_ENV=production webpack --config webpack.production.config",

您正在使用url-loader內聯最多 0.1MB(100000 字節)的圖像和其他資產。 顯着增加您的捆綁包大小不需要很多具有此上限大小的資產。 事實上,降低此限制並取消內聯一個 100KB 的資產意味着您滿足了目標捆綁包大小。

我建議只內聯小於 10KB(10000 字節)的資產。 在需要時,可以通過 HTTP 請求獲取大於此值的所有內容。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM