Webpack:构建需要很长时间

Webpack: Taking a long time to build

我的 React 应用程序中的 webpack 构建时间有问题。一切都很好,但需要很长时间。

即使我只更改 JavaScript 文件 CSS 重建?

而且 CSS 编译花费的时间比我认为的要长(如果我错了请纠正我)?

我是 运行 具有 16gb Ram 的 Core i7,构建大约需要一分钟,这在开发过程中变得非常烦人,因为它是一行更改,您必须等待足够多的时间才能完成您可以在浏览器中看到您的更改吗?

这是错误的做法吗?

const CleanObsoleteChunks = require('webpack-clean-obsolete-chunks');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const DashboardPlugin = require('webpack-dashboard/plugin');
const path = require('path');
const webpack = require('webpack');

const BUILD_DIR = path.resolve(__dirname, '../dist');
const APP_DIR = path.resolve(__dirname, 'src/');

const config = {
    devtool: 'source-map',
    entry: {
        "ehcp-coordinator": [
            APP_DIR + '/index.js'
        ]
    },

    output: {
        path: `${BUILD_DIR}/js/`,
        filename: '[name].js',
        chunkFilename: '[name]-chunk.js',
    },

    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['es2015', 'es2017', 'react', 'stage-0'],
                        plugins: ['transform-runtime', 'transform-decorators-legacy', 'transform-class-properties', 'syntax-dynamic-import',
                          ["import", {"libraryName": "antd",  "style": false}]]
                    }
                }
            }, {
                test: /\.less$/,
                use: ExtractTextPlugin.extract({
                    use: [{
                        loader: "css-loader"
                    }, {
                        loader: "less-loader"
                    }]
                })
            }
        ]
    },

    plugins: [
        new webpack.DefinePlugin({
            'process.env.NODE_ENV': "'development'"
        }),
        new webpack.optimize.UglifyJsPlugin({
            'sourceMap': 'source-map'
        }),
        new webpack.optimize.CommonsChunkPlugin({
            name: 'vendor',
            filename: '[name].js',
            minChunks(module, count) {
                var context = module.context;
                return context && context.indexOf('node_modules') >= 0;
            }
        }),
        new ExtractTextPlugin("../css/[name].css")
    ],

    resolve: {
        modules: [
            path.resolve('./'),
            path.resolve('./node_modules'),
        ],
        extensions: ['.js', '.json']
    }

};

module.exports = config;

正如评论中所讨论的,以下是您可以为加快构建速度所做的最明显的更改:

  • UglifyJsPluginExtractTextPlugin 对编译时间的影响非常大,但实际上并没有在开发中带来很多切实的好处。检查配置脚本中的 process.env.NODE_ENV,并检查 enable/disable 它们,具体取决于您是否正在进行生产构建。
    • 代替ExtractTextPlugin,你可以在开发中使用style-loader将CSS注入到HTML页面的头部。这可能会导致页面加载时出现短暂的无样式内容 (FOUC),但构建速度要快得多。
  • 如果您还没有,请使用 webpack-dev-server 而不仅仅是 运行 监视模式下的 Webpack - 使用开发服务器编译内存中的文件而不是将它们保存到磁盘,这进一步减少开发中的编译时间。
    • 这确实意味着当您想要将文件写入磁盘时必须手动 运行 构建,但无论如何您都需要这样做才能切换到您的生产配置。
  • 不确定这是否对性能有任何影响,但您配置的 resolve 部分与默认设置没有明显不同,您应该能够删除它而不会导致任何问题。

在我的例子中,我将 devtool 属性 更新为 false

Medium 上的文章:https://medium.com/@gagan_goku/hot-reloading-a-react-app-with-ssr-eb216b5464f1

必须使用 SSR 解决我的 React 应用程序 (HELO) 的相同问题。 SSR 使事情变得复杂,但值得庆幸的是,如果您指定 --mode=development,webpack 会变​​得更快,因为它是在内存中完成的。

webpack-dev-server 对我不起作用,因为我需要 client.js 包才能使 SSR 客户端正常工作。这是我的设置:

webpack.config.js:

const path = require('path');

module.exports = {
    entry: {
        client: './src/client.js',      // client side companion for SSR
        // bundle: './src/bundle.js',      // Pure client side app
    },
    output: {
        path: path.resolve(__dirname, 'assets'),
        filename: "[name].js"
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                include: path.resolve(__dirname, 'src'),
                loader: "babel-loader",
                options: {
                    presets: [
                        '@babel/preset-env',
                        {'plugins': ['@babel/plugin-proposal-class-properties']},
                    ],
                }
            }
        ]
    },
    watchOptions: {
        aggregateTimeout: 1000,
        poll: 500,
        ignored: /node_modules/,
    }
};

package.json:

  "scripts": {
    // IMP: --mode=development
    "run-dev-ssr": "webpack --config webpack.config.js --watch --mode=development & babel src -d dist --watch & browser-refresh dist/server.js"
  }

.browser-refresh-ignore:

node_modules/
static/
.cache/
.*
*.marko.js
*.dust.js
*.coffee.js

.git/

# Add these files to ignore, since webpack is storing the compiled output to assets/ folder
src/
dist/

无模式构建时间:

Hash: 81eff31301e7cb74bffd
Version: webpack 4.29.5
Time: 4017ms
Built at: 05/10/2019 9:44:10 AM

Hash: 64e10f26caf6fe15068e
Version: webpack 4.29.5
Time: 2548ms


Time: 5680ms
Time: 11343ms

构建时间模式:

Hash: 27eb1aabe54a8b995d67
Version: webpack 4.29.5
Time: 4410ms

Time: 262ms

Time: 234ms

Time: 162ms