如何调试这个缓慢的webpack构建?

时间:2018-05-15 12:58:09

标签: angular typescript webpack

我有一个使用webpack的项目,运行webpack大约需要12分钟。

我的webpack配置文件是使用角度应用程序的JavascriptServices模板生成的,而非供应商的文件是在此问题的末尾粘贴的。

我知道我需要做些什么来改进它,比如将更多的包移到供应商构建中,并收紧加载器的内容,但这不是我的问题。

我正在使用simple-progress-webpack-plugin生成webpack构建的日志,但无法弄清楚为什么某些行重复这么多次,以及为什么它们需要这么长时间。

如何调试/诊断此处发生的事情?

E.g。打字稿文件attachment.field.ts包含70多次,而后者包含的内容每次只占用这个单一日志条目超过10秒:

[18:39:26] Webpack (13%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\awesome-typescript-loader\dist\entry.js?silent=true!C:\Repos\MyProj\Code\MyProj.Web\node_modules\angular2-template-loader\index.js!C:\Repos\MyProj\Code\MyProj.Web\ClientApp\app\components\fields\attachment\attachment.field.ts)
(Snipped 7 identical lines)
[18:39:26] Webpack (41%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\awesome-typescript-loader\dist\entry.js?silent=true!C:\Repos\MyProj\Code\MyProj.Web\node_modules\angular2-template-loader\index.js!C:\Repos\MyProj\Code\MyProj.Web\ClientApp\app\components\fields\attachment\attachment.field.ts)
[18:39:36] Webpack (41%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\awesome-typescript-loader\dist\entry.js?silent=true!C:\Repos\MyProj\Code\MyProj.Web\node_modules\angular2-template-loader\index.js!C:\Repos\MyProj\Code\MyProj.Web\ClientApp\app\components\fields\attachment\attachment.field.ts)
(Snipped 25 identical lines - except for percentage creeping up)
[18:39:36] Webpack (45%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\awesome-typescript-loader\dist\entry.js?silent=true!C:\Repos\MyProj\Code\MyProj.Web\node_modules\angular2-template-loader\index.js!C:\Repos\MyProj\Code\MyProj.Web\ClientApp\app\components\fields\attachment\attachment.field.ts)
[18:39:46] Webpack (45%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\awesome-typescript-loader\dist\entry.js?silent=true!C:\Repos\MyProj\Code\MyProj.Web\node_modules\angular2-template-loader\index.js!C:\Repos\MyProj\Code\MyProj.Web\ClientApp\app\components\fields\attachment\attachment.field.ts)
[18:39:46] Webpack (13%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\platform-server\node_modules\parse5\lib\index.js)
[18:39:46] Webpack (13%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\platform-server\node_modules\xhr2\lib\xhr2.js)
[18:39:55] Webpack (45%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\awesome-typescript-loader\dist\entry.js?silent=true!C:\Repos\MyProj\Code\MyProj.Web\node_modules\angular2-template-loader\index.js!C:\Repos\MyProj\Code\MyProj.Web\ClientApp\app\components\fields\attachment\attachment.field.ts)
[18:39:55] Webpack (45%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\awesome-typescript-loader\dist\entry.js?silent=true!C:\Repos\MyProj\Code\MyProj.Web\node_modules\angular2-template-loader\index.js!C:\Repos\MyProj\Code\MyProj.Web\ClientApp\app\components\fields\attachment\attachment.field.ts)
[18:40:05] Webpack (45%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\awesome-typescript-loader\dist\entry.js?silent=true!C:\Repos\MyProj\Code\MyProj.Web\node_modules\angular2-template-loader\index.js!C:\Repos\MyProj\Code\MyProj.Web\ClientApp\app\components\fields\attachment\attachment.field.ts)
[18:40:05] Webpack (45%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\awesome-typescript-loader\dist\entry.js?silent=true!C:\Repos\MyProj\Code\MyProj.Web\node_modules\angular2-template-loader\index.js!C:\Repos\MyProj\Code\MyProj.Web\ClientApp\app\components\fields\attachment\attachment.field.ts)
[18:40:15] Webpack (45%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\awesome-typescript-loader\dist\entry.js?silent=true!C:\Repos\MyProj\Code\MyProj.Web\node_modules\angular2-template-loader\index.js!C:\Repos\MyProj\Code\MyProj.Web\ClientApp\app\components\fields\attachment\attachment.field.ts)
[18:40:25] Webpack (45%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\awesome-typescript-loader\dist\entry.js?silent=true!C:\Repos\MyProj\Code\MyProj.Web\node_modules\angular2-template-loader\index.js!C:\Repos\MyProj\Code\MyProj.Web\ClientApp\app\components\fields\attachment\attachment.field.ts)
[18:40:25] Webpack (46%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\awesome-typescript-loader\dist\entry.js?silent=true!C:\Repos\MyProj\Code\MyProj.Web\node_modules\angular2-template-loader\index.js!C:\Repos\MyProj\Code\MyProj.Web\ClientApp\app\components\fields\attachment\attachment.field.ts)
(Snipped 30 more lines)
[18:44:58] Webpack (49%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\awesome-typescript-loader\dist\entry.js?silent=true!C:\Repos\MyProj\Code\MyProj.Web\node_modules\angular2-template-loader\index.js!C:\Repos\MyProj\Code\MyProj.Web\ClientApp\app\components\fields\attachment\attachment.field.ts)
[18:45:09] Webpack (49%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\awesome-typescript-loader\dist\entry.js?silent=true!C:\Repos\MyProj\Code\MyProj.Web\node_modules\angular2-template-loader\index.js!C:\Repos\MyProj\Code\MyProj.Web\ClientApp\app\components\fields\attachment\attachment.field.ts)

还有另一个地方需要花费大量时间,而@angular\animations\bundles\animations-browser.umd.js有40个条目(尽管@angular\animations实际上已经包含在我的供应商包中),并且每个条目都是关于每个10秒:

[18:45:49] Webpack (17%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:45:58] Webpack (17%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:45:58] Webpack (17%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:46:08] Webpack (17%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:46:08] Webpack (17%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:46:18] Webpack (18%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:46:18] Webpack (18%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:46:28] Webpack (18%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:46:28] Webpack (18%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:46:28] Webpack (18%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:46:28] Webpack (18%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:46:38] Webpack (18%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:46:48] Webpack (18%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:46:57] Webpack (19%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:47:07] Webpack (19%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:47:17] Webpack (19%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:47:27] Webpack (19%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:47:37] Webpack (19%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:47:47] Webpack (19%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:47:56] Webpack (19%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:48:06] Webpack (19%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:48:16] Webpack (19%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:48:26] Webpack (20%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:48:36] Webpack (20%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:48:45] Webpack (20%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:48:56] Webpack (20%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:49:05] Webpack (20%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:49:15] Webpack (20%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:49:25] Webpack (20%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:49:35] Webpack (20%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:49:45] Webpack (21%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:49:55] Webpack (21%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:50:05] Webpack (21%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:50:14] Webpack (21%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:50:24] Webpack (21%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:50:34] Webpack (21%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:50:44] Webpack (21%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:50:53] Webpack (21%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)
[18:51:03] Webpack (22%) - Build modules (C:\Repos\MyProj\Code\MyProj.Web\node_modules\@angular\animations\bundles\animations-browser.umd.js)

以下是我的webpack.config.jswebpack.config.vendor.jspackage.json。 由于空间限制,我无法发布完整的构建日志,但如果需要,我会尝试发布更多信息。

webpack.config.js:

const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin;

module.exports = (env) => {
    // Configuration in common to both client-side and server-side bundles
    const isDevBuild = !(env && env.prod);
    const sharedConfig = {
        stats: { modules: false },
        context: __dirname,
        resolve: { extensions: [ '.js', '.ts' ] },
        output: {
            filename: '[name].js',
            publicPath: '/dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
        },
        module: {
            rules: [
                { test: /\.ts$/, include: [ /ClientApp/, /node_modules/ ], use: ['awesome-typescript-loader?silent=true', 'angular2-template-loader'] },
                { test: /\.html$/, use: 'html-loader?minimize=false' },
                { test: /\.css$/, use: [ 'to-string-loader', isDevBuild ? 'css-loader' : 'css-loader?minimize' ] },
                { test: /\.(png|jpg|jpeg|gif|svg)$/, use: 'url-loader?limit=25000' }
            ]
        },
        plugins: [new CheckerPlugin()]
    };

    // Configuration for client-side bundle suitable for running in browsers
    const clientBundleOutputDir = './wwwroot/dist';
    const clientBundleConfig = merge(sharedConfig, {
        entry: { 'main-client': './ClientApp/boot-client.ts' },
        output: { path: path.join(__dirname, clientBundleOutputDir) },
        plugins: [
            new webpack.DllReferencePlugin({
                context: __dirname,
                manifest: require('./wwwroot/dist/vendor-manifest.json')
            })
        ].concat(isDevBuild ? [
            // Plugins that apply in development builds only
            new webpack.SourceMapDevToolPlugin({
                filename: '[file].map', // Remove this line if you prefer inline source maps
                moduleFilenameTemplate: path.relative(clientBundleOutputDir, '[resourcePath]') // Point sourcemap entries to the original file locations on disk
            })
        ] : [
            // Plugins that apply in production builds only
            new webpack.optimize.UglifyJsPlugin()
        ])
    });

    // Configuration for server-side (prerendering) bundle suitable for running in Node
    const serverBundleConfig = merge(sharedConfig, {
        resolve: { mainFields: ['main'] },
        entry: { 'main-server': './ClientApp/boot-server.ts' },
        plugins: [
            new webpack.DllReferencePlugin({
                context: __dirname,
                manifest: require('./ClientApp/dist/vendor-manifest.json'),
                sourceType: 'commonjs2',
                name: './vendor'
            })
        ],
        output: {
            libraryTarget: 'commonjs',
            path: path.join(__dirname, './ClientApp/dist')
        },
        target: 'node',
        devtool: 'inline-source-map'
    });

    return [clientBundleConfig, serverBundleConfig];
};

webpack.config.vendor.js:

const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const merge = require('webpack-merge');

module.exports = (env) => {
    const extractCSS = new ExtractTextPlugin('vendor.css');
    const isDevBuild = !(env && env.prod);
    const sharedConfig = {
        stats: { modules: false },
        resolve: { extensions: [ '.js' ] },
        module: {
            rules: [
                { test: /\.(png|woff|woff2|eot|ttf|svg)(\?|$)/, use: 'url-loader?limit=100000' }
            ]
        },
        entry: {
            vendor: [
                '@angular/animations',
                '@angular/common',
                '@angular/compiler',
                '@angular/core',
                '@angular/forms',
                '@angular/http',
                '@angular/platform-browser',
                '@angular/platform-browser-dynamic',
                '@angular/router',
                'bootstrap',
                'bootstrap/dist/css/bootstrap.css',
                'es6-shim',
                'es6-promise',
                'event-source-polyfill',
                'jquery',
                'zone.js',
            ]
        },
        output: {
            publicPath: '/dist/',
            filename: '[name].js',
            library: '[name]_[hash]'
        },
        plugins: [
            new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery' }), // Maps these identifiers to the jQuery package (because Bootstrap expects it to be a global variable)
            new webpack.ContextReplacementPlugin(/\@angular\b.*\b(bundles|linker)/, path.join(__dirname, './ClientApp')), // Workaround for https://github.com/angular/angular/issues/11580
            new webpack.ContextReplacementPlugin(/angular(\\|\/)core(\\|\/)@angular/, path.join(__dirname, './ClientApp')), // Workaround for https://github.com/angular/angular/issues/14898
            new webpack.IgnorePlugin(/^vertx$/) // Workaround for https://github.com/stefanpenner/es6-promise/issues/100
        ]
    };

    const clientBundleConfig = merge(sharedConfig, {
        output: { path: path.join(__dirname, 'wwwroot', 'dist') },
        module: {
            rules: [
                { test: /\.css(\?|$)/, use: extractCSS.extract({ use: isDevBuild ? 'css-loader' : 'css-loader?minimize' }) }
            ]
        },
        plugins: [
            extractCSS,
            new webpack.DllPlugin({
                path: path.join(__dirname, 'wwwroot', 'dist', '[name]-manifest.json'),
                name: '[name]_[hash]'
            })
        ].concat(isDevBuild ? [] : [
            new webpack.optimize.UglifyJsPlugin()
        ])
    });

    const serverBundleConfig = merge(sharedConfig, {
        target: 'node',
        resolve: { mainFields: ['main'] },
        output: {
            path: path.join(__dirname, 'ClientApp', 'dist'),
            libraryTarget: 'commonjs2',
        },
        module: {
            rules: [ { test: /\.css(\?|$)/, use: ['to-string-loader', isDevBuild ? 'css-loader' : 'css-loader?minimize' ] } ]
        },
        entry: { vendor: ['aspnet-prerendering'] },
        plugins: [
            new webpack.DllPlugin({
                path: path.join(__dirname, 'ClientApp', 'dist', '[name]-manifest.json'),
                name: '[name]_[hash]'
            })
        ]
    });

    return [clientBundleConfig, serverBundleConfig];
}

pacakge.json:

{
  "name": "my-proj",
  "version": "0.0.0",
  "scripts": {
    "test": "karma start ClientApp/test/karma.conf.js"
  },
  "dependencies": {
    "@angular/animations": "4.1.2",
    "@angular/common": "4.1.2",
    "@angular/compiler": "4.1.2",
    "@angular/core": "4.1.2",
    "@angular/forms": "4.1.2",
    "@angular/http": "4.1.2",
    "@angular/platform-browser": "4.1.2",
    "@angular/platform-browser-dynamic": "4.1.2",
    "@angular/platform-server": "4.1.2",
    "@angular/router": "4.1.2",
    "@types/node": "7.0.18",
    "angular2-logger": "^0.6.0",
    "angular2-template-loader": "0.6.2",
    "angular2-uuid": "^1.1.1",
    "aspnet-prerendering": "^2.0.5",
    "aspnet-webpack": "^1.0.29",
    "awesome-typescript-loader": "3.1.3",
    "bootstrap": "3.3.7",
    "css": "2.2.1",
    "css-loader": "0.28.1",
    "es6-promise": "^4.2.4",
    "es6-shim": "0.35.3",
    "event-source-polyfill": "0.0.9",
    "expose-loader": "0.7.3",
    "extract-text-webpack-plugin": "2.1.0",
    "file-loader": "0.11.1",
    "html-loader": "0.4.5",
    "install": "^0.10.1",
    "isomorphic-fetch": "2.2.1",
    "jquery": "3.2.1",
    "json-loader": "0.5.4",
    "linq-es5": "^1.11.11",
    "ng-formly": "^1.0.0-rc.10",
    "ng2-bootstrap-modal": "^1.0.1",
    "ng2-validation": "^4.2.0",
    "npm": "^5.4.2",
    "preboot": "4.5.2",
    "raw-loader": "0.5.1",
    "reflect-metadata": "0.1.10",
    "rxjs": "^5.4.3",
    "style-loader": "0.17.0",
    "to-string-loader": "1.1.5",
    "typescript": "^2.5.3",
    "typescript-collections": "^1.2.5",
    "url-loader": "0.5.8",
    "web-animations-js": "^2.3.1",
    "webpack": "2.5.1",
    "webpack-hot-middleware": "2.18.0",
    "webpack-merge": "4.1.0",
    "zone.js": "0.8.10"
  },
  "devDependencies": {
    "@types/chai": "3.5.2",
    "@types/jasmine": "2.5.47",
    "chai": "3.5.0",
    "jasmine-core": "2.6.1",
    "karma": "1.7.0",
    "karma-chai": "0.1.0",
    "karma-chrome-launcher": "2.1.1",
    "karma-cli": "1.0.1",
    "karma-jasmine": "1.1.0",
    "karma-webpack": "2.0.3",
    "webpack-cyclic-dependency-checker": "0.0.1"
  }
}

0 个答案:

没有答案