Webpack最终捆绑包的大小太大了

时间:2018-05-26 15:17:04

标签: node.js reactjs npm webpack

使用npm run build

使用webpack 4.9.1进行构建

Package.json文件命令

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

构建后,我的捆绑大小为1010 KiB,这太大了。试图弄清楚,但是没有成功,所以最终放在这里

image

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' ]
      },
    ]
  }
};




我的.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"]
    }]
  ]
}




我知道我在这里遗漏了一些东西。

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

FROM BUILD LOGS

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

其他相关信息:

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

1 个答案:

答案 0 :(得分:1)

您可能需要考虑拆分您的块。就像您可以为自己的代码和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
        }
      }
    })
  ]

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