为啥webpack4使用happyPack开启多进程优化之后更慢了呢?
不开happyPack只用七八秒开发环境运行起来,开了反而会用三四十秒,以下为加了happyPack之后的webpack.config.js:
const path = require('path');
const webpack = require('webpack');
const HappyPack = require('happypack');
const happyThreadPool = HappyPack.ThreadPool({ size: 4 });
module.exports = {
devtool: 'cheap-module-eval-source-map',
entry: [
'./index.js',
],
mode: 'development',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
publicPath: '/',
},
resolve: {
modules: [path.resolve(__dirname, 'node_modules')],
alias: {
// lib
react: path.resolve(__dirname, 'node_modules/react/cjs/react.production.min.js'),
// dir
resources: path.resolve(__dirname, 'resources'),
app: path.resolve(__dirname, 'app'),
utils: path.resolve(__dirname, 'app/utils'),
components: path.resolve(__dirname, 'app/components'),
router: path.resolve(__dirname, 'app/router'),
},
},
module: {
rules: [
{
test: /\.m?js|\.jsx$/,
exclude: /(node_modules|bower_components)/,
use: ['happypack/loader?id=babel']
},
{
test: /\.css|\.less$/,
use: ['happypack/loader?id=style'],
},
{
test: /\.html$/,
use: {
loader: 'html-loader',
},
},
{
test: /\.(png|jpg|gif|svg|ico|woff|eot|ttf|woff2|icns)$/,
use: ['happypack/loader?id=file'],
},
],
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('development'),
},
}),
new HappyPack({
id: 'babel',
threadPool: happyThreadPool,
loaders: [{
loader: 'babel-loader',
options: {
cacheDirectory: true,
presets: [
'@babel/preset-env',
'@babel/preset-react',
],
plugins: [
["@babel/plugin-proposal-decorators", { "legacy": true }],
["@babel/plugin-proposal-class-properties", {"loose": true}],
"@babel/plugin-proposal-function-sent",
"@babel/plugin-proposal-export-namespace-from",
"@babel/plugin-proposal-numeric-separator",
"@babel/plugin-proposal-throw-expressions",
]
}
}],
}),
new HappyPack({
id: 'style',
threadPool: happyThreadPool,
loaders: ['style-loader', 'css-loader', 'less-loader'],
}),
new HappyPack({
id: 'file',
threadPool: happyThreadPool,
loaders: [
{
loader: 'file-loader',
options: {
name: '[path][name].[ext]',
},
},
],
}),
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin(),
new webpack.NoEmitOnErrorsPlugin(),
],
devServer: {
host: 'localhost',
port: 3000,
compress: true,
contentBase: '.',
historyApiFallback: true,
hot: true,
inline: true,
liveReload: false
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
开多线程并不意味着一定会提高速度,线程也是需要开销的
其实是这里less和css的解析应该分为两个loader,不然把css文件放入less-loader处理后就会很慢,这里没注意,没用happypack之前我是分开的,下面是正确的配置: