javascript - 为什么我的 webpack bundle.js 和 vendor.bund

我所有的 React 项目的文件都非常大(bundle.js 为 4.87 mb,vendor.bundle.js 为 2.87 mb)。我不知道为什么它这么大。我已经启用了 uglifyJS,但这似乎没有多大帮助(5.09 > 4.87mb 和 2.9 > 2.87mb)

var webpack = require('webpack');
var ExtractTextPlugin = require("extract-text-webpack-plugin");

require('es6-promise').polyfill();

var config = {
  entry: {
    app: [
      './src/entry.jsx'
    ],
    vendor: [
      'react',
      'lodash',
      'superagent'
    ]
  },
  output: {
    path: './build',
    filename: "bundle.js"
  },
  eslint: {
    configFile: './.eslintrc'
  },
  devtool: 'eval-source-map',
  module: {
    loaders: [
      { test: /\.(js|jsx)$/, loaders: ['react-hot', 'babel?experimental'], exclude: /node_modules/},
      { test: /\.(js|jsx)$/, loader: "eslint-loader", exclude: /node_modules/},
      { test: /\.json$/, loader: 'json' },
      { test: /\.yml$/, loader: 'json!yaml' },
      { test: /\.scss$/, loader: 'style!css!sass' },
      { test: /\.(png|woff|woff2|eot|ttf|svg)$/, loader: 'url-loader?limit=100000' }
    ]
  },
  plugins: [
    new webpack.DefinePlugin({'process.env': {'NODE_ENV': JSON.stringify('production')}}),
    new webpack.optimize.CommonsChunkPlugin("vendor", "vendor.bundle.js"),
    new webpack.optimize.UglifyJsPlugin({minimize: true}),
    new webpack.NoErrorsPlugin()
  ]
};

module.exports = config;

我的 package.json

{
  "license": "MIT",
  "engines": {
    "iojs": ">= 1.6.2"
  },
  "scripts": {
    "create:index": "mustang -t index.tmpl -i config.json -o build/index.html",
    "predev": "mkdir -p build/ && npm run create:index",
    "dev": "webpack-dev-server --host 0.0.0.0 --hot --progress --colors --content-base build",
    "backend": "NODE_ENV=production node server/server.js",
    "backend:dev": "DEBUG=tinderlicht node server/server.js",
    "predeploy": "mkdir -p build/ && npm run create:index",
    "deploy": "NODE_ENV=production webpack -p",
    "test": "node webpack-mocha.config.js"
  },
  "devDependencies": {
    "autoprefixer-loader": "^3.2.0",
    "axios": "^0.7.0",
    "babel": "^5.8.23",
    "babel-core": "^5.8.25",
    "babel-eslint": "^4.1.3",
    "babel-loader": "^5.3.2",
    "bluebird": "^2.10.2",
    "css-loader": "^0.19.0",
    "es6-collections": "^0.5.1",
    "es6-promise": "^3.0.2",
    "eslint": "^1.6.0",
    "eslint-loader": "^1.1.0",
    "eslint-plugin-react": "^3.5.1",
    "extract-text-webpack-plugin": "^0.8.2",
    "file-loader": "^0.8.1",
    "firebase": "^2.3.1",
    "fireproof": "^3.0.3",
    "jquery": "^2.2.0",
    "json-loader": "^0.5.1",
    "jsonld": "^0.4.2",
    "jsx-loader": "^0.13.2",
    "lodash": "^3.3.0",
    "mustang": "^0.1.3",
    "node-sass": "^3.3.3",
    "react": "^0.14.0",
    "react-dom": "^0.14.0",
    "react-hot-loader": "^1.1.5",
    "sass-loader": "3.0.0",
    "style-loader": "^0.12.4",
    "superagent": "^1.4.0",
    "url-loader": "^0.5.5",
    "webpack": "^1.5.3",
    "webpack-dev-server": "^1.7.0"
  },
  "dependencies": {
    "body-parser": "^1.15.0",
    "cors": "^2.7.1",
    "debug": "^2.2.0",
    "express": "^4.13.4",
    "mustache": "^2.2.1",
    "nodemailer": "^2.1.0",
    "nodemailer-sendmail-transport": "^1.0.0",
    "react-radio-group": "^2.2.0",
    "uglifyjs": "^2.4.10"
  }
}

有人知道如何解决这个问题吗?

最佳答案

我强烈推荐使用 Webpack Bundle Analyzer 来缩小你的 bundle (https://github.com/th0r/webpack-bundle-analyzer)。您可以看到是什么使您的 bundle 如此之大。您可能还使用了所有的 firebase、lodash 和 jquery。除了使用 webpack 生产插件之外,尝试使用忽略你不使用的任何内容并只导入你需要的内容,如下所示: 在 webpack 插件中:

    new webpack.IgnorePlugin(/^\.\/auth$/, /firebase$/),
    new webpack.IgnorePlugin(/^\.\/storage$/, /firebase$/),
    new webpack.IgnorePlugin(/^\.\/messaging$/, /firebase$/),

在您的导入中:

 const Firebase: any = require('firebase/app');  require('firebase/database');

对于 lodash,仅导入您需要的内容或进行自定义构建 (https://lodash.com/custom-builds):

import find from 'lodash/find' 

您也可以创建 jquery 自定义构建。

关于javascript - 为什么我的 webpack bundle.js 和 vendor.bundle.js 这么大?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35408898/

相关文章:

ios - 项目构建并运行,但不存档

android - 我可以从我的 Cordova config.xml 控制 AndroidMani

build - 没有缩小 JS 的 sencha 应用程序构建原生

maven - 构建依赖项时如何使 Jenkins 触发依赖项目的构建

json - 从位于内存中的 JSON 加载启动配置

build - 如何存储 CMake build设置

jenkins - 远程触发构建后检查 Jenkins 作业状态

variables - 如何使用 TFS 构建有条件地跳过或执行任务/步骤?

msbuild - 如何从外部文件中读取属性值?

java - 通过 Ant 编译 GWT