优化babel-loader
module.exports = {
module:{
rules:[
{
test: /\.js$/,
loader: ['babel-loader?cacheDirectory'],
include: path.resolve(__dirname, 'src'),
}
]
}
}
IgnorePlugin
import moment from 'moment';
import 'moment/locale/zh-cn';
module.exports = {
plugins: [
new webpack.IgnorePlugin(/\.\/locale/, /moment/)
]
}
noPase
module.exports = {
module: {
noParse: [/vue\.min\.js$/]
}
}
IgnorePugin VS noParse
- IgnorePlugin直接不引入代码,代码中没有,需要什么手动引入
- IgnorePlugin减少产出的体积
- noParse引入代码,但不打包,也不进行编译,不进行模块化的分析
happyPack
- happyPack多进程打包
- JS单线程,开启多进程打包
- 提高构建速度(特别是多核CPU)
npm install --save-dev happypack
const HappyPack = require('happypack')
module.exports = {
module: {
rule: [
{
test: /\.js$/,
use: ['happypack/loader?id=babel'],
include: path.resolve(__dirname, 'src'),
},
]
},
plugins: [
new HappyPack({
id: 'babel',
loaders: ['babel-loader?cacheDirectory']
}),
]
}
ParallelUglifyPlugin
- 多进程压缩js
- webpack内置Uglify工具压缩JS
- JS单线程,开启多进程压缩更快
- 和HappyPack同理
module.exports = {
plugins: [
new ParallelUglifyPlugin({
uglifyJS: {
output: {
beautify: false,
comments: false,
},
compress: {
drop_console: true,
collapse_vars: true,
reduce_vars: true,
}
}
})
]
}
关于开启多进程
- 如果项目较大,打包较慢,开启多进程能提高速度
- 如果项目较小,打包很快,开启多进程会降低速度(进程开销)
自动刷新
- 指保存代码以后,浏览器会自动刷新,作用有别于热更新
- 一般用webpack-dev-server,知道有这么个东西就行了
module.exports = {
watch: true,
watchOptions: {
ignored: /node_modules/,
aggregateTimeout: 300,
poll: 1000
}
}
热更新
- 自动刷新:整个网页全部刷新,速度较慢
- 自动刷新: 整个网页全部刷新,状态会丢失,如:表单输入全部被清除,路由回到首页等缺点
- 热更新:新代码生效,网页不刷新,状态不丢失
- 热更新:有代价,需要在开发环境下去监听或注册哪些模块我们需要热更新,模块化之外的还是以自动刷新的方式去更新。
const path = require('path')
const srcPath = path.join(__dirname, '..', 'src')
const distPath = path.join(__dirname, '..', 'dist')
const HotModuleReplacementPlugin = require('webpack/lib/HotModuleReplacementPlugin');
module.exports = {
entry: {
index: [
'webpack-dev-server/client?http://localhost:8080/',
'webpack/hot/dev-server',
path.join(srcPath, 'index.js')
],
other: path.join(srcPath, 'other.js')
},
plugins: [
new HotModuleReplacementPlugin()
],
devServer: {
port: 8080,
progress: true,
contentBase: distPath,
open: true,
compress: true,
hot: true,
proxy: {
'/api': 'http://localhost:3000',
'/api2': {
target: 'http://localhost:3000',
pathRewrite: {
'/api2': ''
}
}
}
},
}
DllPlugin
- DllPlugin动态链接库插件
- 前端框架如Vue React,体积大,构建慢
- 但是框架较稳定,不常升级版本
- 同一个版本只构建一次即可,不用每次都重新构建
- webpack已内置DllPlugin支持
- DllPlugin - 打包出Dll文件
- DllReferencePlugin - 使用dll文件
// 栗子
- 第一步使用DllPlugin打包出react.dll.js
const path = require('path')
const DllPlugin = require('webpack/lib/DllPlugin')
const { srcPath, distPath } = require('./paths')
module.exports = {
mode: 'development',
entry: {
react: ['react', 'react-dom']
},
output: {
filename: '[name].dll.js',
path: distPath,
library: '_dll_[name]',
},
plugins: [
new DllPlugin({
name: '_dll_[name]',
path: path.join(distPath, '[name].manifest.json'),
}),
],
}
- 第二步在html模板文件中引入react.dll.js
// index.html
...
<html>
<body>
<script src="./react.dll.js"></script>
</body>
</html>
- 第三步在webpack.dev.js中使用DllReferencePlugin插件忽略对Dll文件的打包
module.exports = {
plugins: [
new DllReferencePlugin({
manifest: require(path.join(distPath, 'react.manifest.json')),
}),
],
}
可用于生产环境
- babel-loader
- IgnorePlugin
- noParse
- happPack
- ParallelUglifyPlugin
不可用于生产环境