使用webPack捆绑文件大小问题

时间:2017-02-01 14:27:14

标签: npm webpack browserify

在我的反应应用程序中,我第一次使用“browserify”将es6文件编译为es5。但是当我检查捆绑文件的大小为1.3mb(这么多)时,我的所有脚本的文件夹都有200kb。我想,可能 browserify 包括相同脚本的倍数。那是因为我有(例如)“Config.js”并且我将它包含在我需要的任何文件中 与

import Config from .'/config.js';

如果我不这样做,应用程序会显示错误,说明请求该部分中的变量。然后我将我的管理从 browserify 更改为 webpack 。关于这一点,我认为一切都是新的,文档说 webpack读取我的依赖项并尝试包含一次任何文件。但我的捆绑文件不断调整大小为1.3mb

这是我的虚拟 webpack.config.js

var path = require('path');
var webpack = require('webpack');

module.exports = {
    entry: './js/src/index.js',
    output: { path: __dirname, filename: 'bundle.js' },
    module: {
        loaders: [
            {
                test: /.jsx?$/,
                loader: 'babel-loader',
                exclude: /node_modules|libs/, //npm and bower_components(libs)

                query: {
                    presets: ['es2015', 'react']
                }
            }
        ]
    },
};

这是我的package.json,你可以在尝试使用webpack(启动命令)之前看到我正在执行的browserify命令。

   {
  "name": "appname",
  "version": "1.0.0",
  "description": "app description",
  "main": "js/dist/index.js",
  "dependencies": {
    "babelify": "^7.3.0",
    "browserify": "^13.1.1",
    "fetch": "^1.1.0",
    "owl.carousel": "^2.2.0",
    "react": "^15.4.2",
    "react-dom": "^15.4.2",
    "react-image-holder": "^2.0.1",
    "react-owl-carousel": "^0.14.0",
    "react-toastr": "^2.8.2",
    "whatwg-fetch": "^2.0.1"
  },
  "devDependencies": {
    "babel": "^6.5.2",
    "babel-cli": "^6.18.0",
    "babel-core": "^6.22.1",
    "babel-loader": "^6.2.10",
    "babel-plugin-external-helpers": "^6.18.0",
    "babel-plugin-transform-react-jsx": "^6.8.0",
    "babel-preset-env": "^1.1.5",
    "babel-preset-es2015": "^6.22.0",
    "babel-preset-es2017": "^6.16.0",
    "babel-preset-latest": "^6.16.0",
    "babel-preset-react": "^6.22.0",
    "babelify": "^7.3.0",
    "browserify": "^13.3.0",
    "envify": "^4.0.0",
    "gulp": "^3.9.1",
    "gulp-concat": "^2.6.1",
    "gulp-concat-css": "^2.3.0",
    "gulp-notify": "^3.0.0",
    "gulp-uglify": "^2.0.1",
    "gulp-uglifycss": "^1.0.6",
    "jquery": "^3.1.1",
    "jquery-ui": "^1.12.1",
    "react": "^15.4.2",
    "react-dom": "^15.4.2",
    "react-redux": "^5.0.1",
    "react-router": "^3.0.0",
    "react-router-redux": "^4.0.7",
    "redux": "^3.6.0",
    "uglify": "^0.1.5",
    "watchify": "^3.8.0",
    "webpack": "^1.14.0",
    "whatwg-fetch": "^2.0.1"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "watch-js": "NODE_ENV=production watchify assets/js/main.js -t babelify -t bulkify -o dist/js/app.js -v --full-path=false",
    "start": "set NODE_ENV=production && watchify js/src/index.js -v -t [babelify --presets [env] ] -o js/dist/index.js",
    "build": "browserify js/src/index.js -g [envify --NODE_ENV 'production'] -t [ babelify --presets [env] ]  -t  > js/dist/app.js"
  },
  "keywords": [
    "app",
    "jidapp"
  ]

1 个答案:

答案 0 :(得分:0)

您可以尝试在webpack.config.js中指定devtool设置。 以下是有关不同设置的一些信息。 https://webpack.js.org/configuration/devtool/ 对于生产构建,我通常使用廉价模块源映射。在开发中,我使用eval。

var path = require('path');
var webpack = require('webpack');

module.exports = {
    devtool: 'cheap-module-source-map',
    entry: './js/src/index.js',
    output: { path: __dirname, filename: 'bundle.js' },
    module: {
      loaders: [
        {
         test: /.jsx?$/,
         loader: 'babel-loader',
         exclude: /node_modules|libs/, 
         query: {
           presets: ['es2015', 'react']
         }
       }
      ]
    },
};

您还可以在此处查看其他webpack优化插件。 https://github.com/webpack/docs/wiki/optimization