Webpack 最终包大小太大

Pra*_*war 5 node.js npm reactjs webpack

使用 webpack 4.9.1 进行构建npm run build

Package.json 文件命令

"build": "webpack --mode production --config webpack.config.prod.js",

构建后我的包大小为 1010 KiB,这太大了。从那天起就想弄清楚但没有成功所以最后放在这里

图像

webpack.config.prod.js

var path = require('path');
var webpack = require('webpack');
var CopyWebpackPlugin = require('copy-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;

module.exports = {
  mode: 'production',
  devtool: 'none',
  entry: {
    index: './src/index.js',
  },
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'bundle.js',
    publicPath: '/'
  },
  optimization: {
    minimize: true,
  },
  plugins: [
    new webpack.LoaderOptionsPlugin({ options: {} }),
    new webpack.optimize.AggressiveMergingPlugin(),
    new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
    new CopyWebpackPlugin([
      {
        from: 'style.css',
      },
      {
        from: 'bootstrap.min.css',
      },
      {
        from: 'index.html',
      }
    ]),
    new BundleAnalyzerPlugin(),
  ],
  module: {
    noParse:[ /node_modules\/localforage\/dist\/localforage.js/],
    rules: [
      {
        test: /\.js$/,
        enforce: "pre",
        loaders: ['eslint-loader'],
        include: path.join(__dirname, 'src')
      },
      {
        test: /\.js$/,
        loaders: ['babel-loader'],
        include: [path.join(__dirname, 'src'), path.join(__dirname, 'translations')]
      },
      {
        // do not exclude node_modules, since map.json is in node_modules
        test: /\.json$/,
        loader: 'json'
      },
      {
        test: /\.css$/,
        loaders: [ 'style-loader', 'css-loader' ]
      },
    ]
  }
};
Run Code Online (Sandbox Code Playgroud)

我的 .babelrc 如下所示

{
  "presets": ["react", "es2015", "stage-2"],
  "env": {
    "development": {
      "plugins": [
        ["react-transform", {
          "transforms": [{
            "transform": "react-transform-hmr",
            "imports": ["react"],
            "locals": ["module"],
            "preventFullImport": true
          }, {
            "transform": "react-transform-catch-errors",
            "imports": ["react", "redbox-react"],
            "preventFullImport": true
          }]
        }]
      ]
    }
  },
  "plugins": [
    ["transform-object-rest-spread"],
    ["transform-react-display-name"],
    ["module-resolver", {
      "root": ["./src"]
    }]
  ]
}
Run Code Online (Sandbox Code Playgroud)

我知道我在这里错过了一些东西。

应该接近或超过 webpack 建议,但 1010 KB 太多了

来自构建日志

入口点大小限制中的警告:以下入口点组合资产大小超过建议的限制 (244 KiB)。这可能会影响网络性能。入口点:索引(1010 KiB)bundle.js

其他相关信息:

  1. webpack版本:webpack 4.9.1
  2. Node.js 版本:v6.10.0 运行
  3. 系统: mac OS 附加
  4. 工具:npm -v = 4.1.2

小智 4

您可能需要考虑拆分您的块。就像您可以为自己的代码和node_modules.

您可能希望在生产模式下缩小代码以减少包的大小。您可以使用 webpack 配置文件执行以下操作:

const UglifyJSPlugin = require('uglifyjs-webpack-plugin');

optimization: splitChunks: {
    cacheGroups: {
      vendor: {
        chunks: "initial",
        test: path.resolve(process.cwd(), "node_modules"),
        name: "vendor",
        enforce: true
      }
    }
  },
  minimizer: [
    new UglifyJSPlugin({
      uglifyOptions: {
        sourceMap: true,
        compress: {
          drop_console: true,
          conditionals: true,
          unused: true,
          comparisons: true,
          dead_code: true,
          if_return: true,
          join_vars: true,
          warnings: false
        },
        output: {
          comments: false
        }
      }
    })
  ]
Run Code Online (Sandbox Code Playgroud)

这将为您创建单独的捆绑包node_module,您可以将其包含在主文件中。