Webpack:需要很长时间才能构建

时间:2017-06-13 10:17:49

标签: javascript reactjs webpack babel

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

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

此外,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;

3 个答案:

答案 0 :(得分:8)

正如评论中所讨论的,以下是您可以加快构建的最明显的变化:

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

答案 1 :(得分:0)

就我而言,我将 <kendo-grid-column width="40"> <ng-template kendoGridCellTemplate let-dataItem="dataItem"> <a>{{dataItem.fileName}}</a> </ng-template> </kendo-grid-column> 属性更新为devtool

答案 2 :(得分:0)

有关媒介的文章: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"
  }

。浏览器刷新-忽略:

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