Webpack极端缓慢构建

时间:2016-01-04 12:07:25

标签: javascript reactjs typescript webpack babeljs

我使用 webpack + 打字稿 + 做出反应

webpack.config.js是:

var webpack = require('webpack');
var path = require('path');
var node_modules_dir = path.join(__dirname, 'node_modules');

var deps = [
  'react/react.js',
  'react-dom/react-dom.js',
];

var config = {
    devtool: 'source-map',
    context: __dirname + '/Scripts/ts',
    entry: {
        server: "./server.js",
        client: "./client.ts"
    },
    output: {
        path: path.resolve(__dirname, "Scripts/public/"),
        filename: '[name].bundle.js'
    },
    resolve: {
        alias: {},
        modulesDirectories: ["node_modules"],
        extensions: ['', '.webpack.js', '.web.js', '.ts', '.tsx', '.js']
    },
    module: {
        noParse: [],

        // Use the expose loader to expose the minified React JS
        // distribution. For example react-router requires this
        loaders: [ {
            test: /\.ts(x?)$/,
            exclude: /(node_modules|bower_components)/,
            loader: 'babel-loader?presets[]=es2015&presets[]=react!ts-loader'
        },
        {
            test: path.resolve(node_modules_dir, deps[0]),
            loader: "expose?React"
        },
        ]
    },
    watch: true
};

deps.forEach(function (dep) {
    var depPath = path.resolve(node_modules_dir, dep);
    config.resolve.alias[dep.split(path.sep)[0]] = depPath;
    config.module.noParse.push(depPath);
});

module.exports = config;

我的问题是构建速度。初始过程大约需要25秒,增量需要5-6秒。结果:

webpack --profile --display-modules

是:

ts-loader: Using typescript@1.8.0-dev.20160104 and C:\Users\rylkov.i\Documents\Visual Studio 2013\Projects\react_test_app\react_test_app\tsconfig.json
Hash: d6d85b30dfc16f19f4a6
Version: webpack 1.12.9
Time: 25547ms
               Asset     Size  Chunks             Chunk Names
    client.bundle.js  1.14 MB       0  [emitted]  client
    server.bundle.js  1.14 MB       1  [emitted]  server
client.bundle.js.map  1.31 MB       0  [emitted]  client
server.bundle.js.map  1.31 MB       1  [emitted]  server
   [0] ./client.ts 80 bytes {0} [built]
       factory:38ms building:21905ms dependencies:1ms = 21944ms
   [0] ./server.js 70 bytes {1} [built]
       factory:14ms building:19ms = 33ms
   [1] C:/Users/rylkov.i/Documents/Visual Studio 2013/Projects/react_test_app/react_test_app/~/expose-loader?Components!./components/index.js 179 bytes {0} {1} [built]
       [0] 33ms -> factory:2078ms building:8ms = 2119ms
   [2] C:/Users/rylkov.i/Documents/Visual Studio 2013/Projects/react_test_app/react_test_app/Scripts/ts/components/index.js 210 bytes {0} {1} [built]
       [0] 33ms -> [1] 2086ms -> factory:19832ms building:4ms = 21955ms
   [3] C:/Users/rylkov.i/Documents/Visual Studio 2013/Projects/react_test_app/react_test_app/Scripts/ts/components/main.tsx 4.78 kB {0} {1} [built]
       [0] 33ms -> [1] 2086ms -> [2] 19836ms -> factory:26ms building:331ms dependencies:1ms = 22313ms
   [4] C:/Users/rylkov.i/Documents/Visual Studio 2013/Projects/react_test_app/react_test_app/~/react/react.js 172 bytes {0} {1} [built]
       [0] 33ms -> [1] 2086ms -> [2] 19836ms -> [3] 357ms -> factory:315ms building:0ms = 22627ms
   [5] C:/Users/rylkov.i/Documents/Visual Studio 2013/Projects/react_test_app/react_test_app/~/react/react.js 641 kB {0} {1} [built]
       [0] 33ms -> [1] 2086ms -> [2] 19836ms -> [3] 357ms -> [4] 315ms -> factory:1ms building:86ms = 22714ms
   [6] C:/Users/rylkov.i/Documents/Visual Studio 2013/Projects/react_test_app/react_test_app/Scripts/ts/components/todoItem.tsx 2.81 kB {0} {1} [built]
       [0] 33ms -> [1] 2086ms -> [2] 19836ms -> factory:28ms building:576ms dependencies:70ms = 22629ms
   [7] C:/Users/rylkov.i/Documents/Visual Studio 2013/Projects/react_test_app/react_test_app/Scripts/ts/components/selectControl.tsx 3.44 kB {0} {1} [built]
       [0] 33ms -> [1] 2086ms -> [2] 19836ms -> factory:27ms building:428ms dependencies:218ms = 22628ms
   [8] C:/Users/rylkov.i/Documents/Visual Studio 2013/Projects/react_test_app/react_test_app/~/react-dom/react-dom.js 1.17 kB {0} {1} [built]
       [0] 33ms -> [1] 2086ms -> [2] 19836ms -> [7] 455ms -> factory:215ms building:4ms = 22629ms
   [9] C:/Users/rylkov.i/Documents/Visual Studio 2013/Projects/react_test_app/react_test_app/Scripts/ts/components/selectItem.tsx 2.63 kB {0} {1} [built]
       [0] 33ms -> [1] 2086ms -> [2] 19836ms -> factory:28ms building:502ms dependencies:144ms = 22629ms
  [10] C:/Users/rylkov.i/Documents/Visual Studio 2013/Projects/react_test_app/react_test_app/~/imports-loader?$=jquery!C:/Users/rylkov.i/Documents/Visual Studio 2013/Projects/react_test_app/react_test_app/Scripts/ui-select.js 99.6 kB {0} {1} [built]
       [0] 33ms -> [1] 2086ms -> [2] 19836ms -> [7] 455ms -> factory:150ms building:145ms = 22705ms
  [11] C:/Users/rylkov.i/Documents/Visual Studio 2013/Projects/react_test_app/react_test_app/~/jquery/dist/jquery.js 348 kB {0} {1} [built]
       [0] 33ms -> [1] 2086ms -> [2] 19836ms -> [7] 455ms -> [10] 295ms -> factory:10ms building:211ms = 22926ms

我认为这非常慢。 react.js和react-dom.js已经编译好js文件,没有额外的要求。我的组件只是示例。 webpack的另一个问题是:

watch:true
配置的

属性。我无法理解为什么总是如此。但也许这是因为构建过程缓慢。谢谢!

3 个答案:

答案 0 :(得分:1)

一件事,特别是在开发中,请确保设置mode

module.exports = {
  mode: "development"

  // Other options...
}

如果根本没有设置(看起来不在共享的webpack配置中),它将default to "production"。这是理想的选择,因为您希望代码在生产模式下运行以进行部署,因为它可以减少代码,消除死代码,某些程序包(例如:React)根据环境提供不同的内部版本。

但是,如果您正在开发中,则在生产模式下运行会增加一些构建时间成本,因为这样做的精简,消除无效代码等会增加总体构建时间。对于实际构建的产品来说这是不可避免的,因此这不会帮助您节省构建时间,但是假设您要在开发中进行大量构建,应该可以节省一些时间。

另外,请考虑将devtool选项更改为较便宜的选项,例如cheap-module-eval-source-map或文档中的其他选项之一:https://webpack.js.org/configuration/devtool/。该文档解释了差异和构建时间成本。当前选项source-map是最慢的选项之一。

module.exports = {
  devtool: "cheap-module-eval-source-map"

  // Other options...
}

答案 1 :(得分:0)

你可以尝试:

 devtool: 'eval',

它会生成一个相当大的文件,但只有一半的时间。不建议生产。

答案 2 :(得分:0)

删除dev-tool: source-maps应该会加快编译时间。同样重要的是要注意,由于输出超过1mb,因此文件位于较大的一侧。

您还可以将cacheDirectory: true标志添加到babel-loader。我发现这大大加快了我公司的建设速度。参考-https://webpack.js.org/loaders/babel-loader/#options

当我希望Webpack在监视模式下运行时,我还亲自使用--watch标志。这使我可以更好地控制何时实际运行。