欢迎在下方发表您的优质见解
1)优化 Webpack 的构建速度
2)使用webpack4-优化原因
①noParse
module.exports = {
module: {
noParse: /jquery/,
rules:[]
}
}
②IgnorePlugin
③dillPlugin
④happypack -> thread-loader
⑤thread-loader
thread-loader 会将您的 loader 放置在一个 worker 池里面运行,以达到多线程构建。
把这个 loader 放置在其他 loader 之前(如下图 example 的位置), 放置在这个 loader 之后的 loader 就会在一个单独的 worker 池(worker pool)中运行。
// webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.js$/,
include: path.resolve("src"),
use: [
"thread-loader",
// 你的高开销的loader放置在此 (e.g babel-loader)
]
}
]
}
}
每个 worker 都是一个单独的有 600ms 限制的 node.js 进程。同时跨进程的数据交换也会被限制。请在高开销的loader中使用,否则效果不佳
⑥压缩加速——开启多线程压缩
module.exports = {
optimization: {
minimizer: [
new UglifyJsPlugin({
parallel: true,
}),
],
},};
module.exports = {
optimization: {
minimizer: [new TerserPlugin(
parallel: true // 多线程
)],
},
};
2)优化 Webpack 的打包体积
3)speed-measure-webpack-plugin
简称 SMP,分析出 Webpack 打包过程中 Loader 和 Plugin 的耗时,有助于找到构建过程中的性能瓶颈。
开发阶段
1)开启多核压缩
插件:* terser-webpack-plugin *
const TerserPlugin = require('terser-webpack-plugin')
module.exports = {
optimization: {
minimizer: [
new TerserPlugin({
parallel: true,
terserOptions: {
ecma: 6,
},
}),
]
}
}
2)监控面板
插件:speed-measure-webpack-plugin
在打包的时候显示出每一个loader,plugin所用的时间,来精准优化
// webpack.config.js文件
const SpeedMeasurePlugin = require('speed-measure-webpack-plugin');
const smp = new SpeedMeasurePlugin();
//............
// 用smp.warp()包裹一下合并的config
module.exports = smp.wrap(merge(_mergeConfig, webpackConfig));
3)开启一个通知面板
插件:webpack-build-notifier
// webpack.config.js文件
const WebpackBuildNotifierPlugin = require('webpack-build-notifier');
const webpackConfig= {
plugins: [
new WebpackBuildNotifierPlugin({
title: '我的webpack',
// logo: path.resolve('./img/favicon.png'),
suppressSuccess: true
})
]
}
4)开启打包进度
插件:progress-bar-webpack-plugin
// webpack.config.js文件
const ProgressBarPlugin = require('progress-bar-webpack-plugin');
const webpackConfig= {
plugins: [
new ProgressBarPlugin(),
]
}
5)开发面板更清晰
插件:webpack-dashboard
// webpack.config.js文件
const DashboardPlugin = require('webpack-dashboard/plugin');
const webpackConfig= {
plugins: [
new DashboardPlugin()
]
}
```js
// package.json文件
{
"scripts": {
"dev": "webpack-dashboard webpack --mode development",
},
}
6)开启窗口的标题
**node-bash-title**
这个包mac的item用有效果,windows暂时没看到效果
```js
// webpack.config.js文件
const setTitle = require('node-bash-title');
setTitle('server');
7)friendly-errors-webpack-plugin
插件:friendly-errors-webpack-plugin
new FriendlyErrorsWebpackPlugin({
compilationSuccessInfo: {
messages: ['You application is running here http://localhost:3000'],
notes: ['Some additionnal notes to be displayed unpon successful compilation']
},
onErrors: function (severity, errors) {
// You can listen to errors transformed and prioritized by the plugin
// severity can be 'error' or 'warning'
},
// should the console be cleared between each compilation?
// default is true
clearConsole: true,
// add formatters and transformers (see below)
additionalFormatters: [],
additionalTransformers: []
}),
很硬核的知识,大白前来点赞,占楼先。
有个问题, includes 的效率比 indexOf 高吗? 我怎么看 chromium bug list 里面很多 benchmark 的结果是相反的
太牛逼了 看不懂
Most helpful comment
1)优化 Webpack 的构建速度
注意:thread-loader 和 cache-loader 兩個要一起使用的話,請先放 cache-loader 接著是 thread-loader 最後才是 heavy-loader
2)使用webpack4-优化原因
①noParse
②IgnorePlugin
module.exports = {
plugins: [
new Webpack.IgnorePlugin(/.\/local/, /moment/),
]
}
③dillPlugin
④happypack -> thread-loader
⑤thread-loader
thread-loader 会将您的 loader 放置在一个 worker 池里面运行,以达到多线程构建。
把这个 loader 放置在其他 loader 之前(如下图 example 的位置), 放置在这个 loader 之后的 loader 就会在一个单独的 worker 池(worker pool)中运行。
每个 worker 都是一个单独的有 600ms 限制的 node.js 进程。同时跨进程的数据交换也会被限制。请在高开销的loader中使用,否则效果不佳
⑥压缩加速——开启多线程压缩
Webpack 4.0以前:uglifyjs-webpack-plugin,parallel参数
2)优化 Webpack 的打包体积
3)speed-measure-webpack-plugin
简称 SMP,分析出 Webpack 打包过程中 Loader 和 Plugin 的耗时,有助于找到构建过程中的性能瓶颈。