[英]Webpack: Taking a long time to build
我的反應應用程序中的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;
正如評論中所討論的,以下是您可以為加速構建所做的最明顯的更改:
UglifyJsPlugin
和ExtractTextPlugin
在對編譯時間的影響方面非常沉重,而實際上並沒有在開發中提供許多切實的好處。 檢查配置腳本中的process.env.NODE_ENV
,並根據您是否正在進行生產構建來啟用/禁用它們。
ExtractTextPlugin
,您可以在開發中使用style-loader
將CSS注入HTML頁面的頭部。 這可能會在頁面加載時短暫閃現無格式內容(FOUC),但構建起來要快得多。 webpack-dev-server
而不是僅在監視模式下運行Webpack - 使用dev服務器編譯內存中的文件而不是將它們保存到磁盤,這進一步減少了開發中的編譯時間。
resolve
部分與默認值沒有明顯區別,您應該能夠刪除它而不會導致任何問題。 在我的情況下,我將devtool
屬性更新為false
。
關於媒介的文章: https : //medium.com/@gagan_goku/hot-reloading-a-react-app-with-ssr-eb216b5464f1
不得不用SSR為我的React app( 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刷新,忽略:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.