优化
- noParse
module: {
noParse: /jquery/,
rules: [
{
test: /\.js$/,
use:{
loader: 'babel-loader',
options: {
presets: [
'@babel/preset-env',
'@babel/preset-react'
]
}
}
}
]
},
IgnorePlugin
new webpack.IgnorePlugin(/.\/locale/,/moment/),//moment这个库中,如果引用了./locale/目录的内容,就忽略掉,不会打包进去 ,因为页面没有用。不需要的不打包进去
dllPlugin 动态链接库
module.exports = {
mode: 'development',
entry: {
react: ['react', 'react-dom']
},
output:{
filename: '_dll_[name].js',
path:path.resolve(__dirname, 'dist'),
library: '_dll_[name]', // 打包文件执行 返回的结果
// libraryTarget: 'commonjs' // umd commonjs var this var ab = 文件结果
},
plugins: [
new webpack.DllPlugin({ // 内置库, 提前生成
name: '_dll_[name]',
path: path.resolve(__dirname, 'dist', 'manifest.json')
}),
new NoConsole()
]
}
生成 _dll_react.js 引入到 index.html中
在正常打包的 config配置
new webpack.DllReferencePlugin({
// manifest.json中引入的包 都不会在打包
manifest: path.resolve(__dirname, 'dist', 'manifest.json')
}),
happypack 多线程打包
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
include: path.resolve('src'),
use: 'happypack/loader?id=js',//js要多线程打包
}
]
new Happypack({
id: 'css',
use: ['style-loader', 'css-loader']
}),
new Happypack({
id: 'js',
use: [{
loader: 'babel-loader',
options: {
presets: [
'@babel/preset-env',
'@babel/preset-react'
]
}
}]
}),
import 再生产环境下会自动 去除没用代码 tree-shaking
require 语法 内容会在 default 中,不会 tree-shaking scope hosting 没用的变量不声明
抽离公共代码
// 多入口
entry:{
index: 'index.js',
test: 'test.js'
},
output: {
filename: '[name].js'
},
optimization: {
splitChunks: { // 分割代码块 多入口
cacheGroups: { // 缓存组
common: { // 公共模块 引用的文件
minSize: 0, // 大小是多少 抽离
minChunks: 2, // 被引用1次以上 抽离
chunks: 'initial',
},
vendor: {
priority: 1,// 权重。优先抽离
// 第三方模块
test: /node_modules/, // 中的文件都抽离
chunks: 'initial', // 入口开始
minSize: 0, // 大小是多少 抽离
minChunks: 2,
}
}
}
},
赖加载
// 原理是jsonp
import('./source.js').then(res => {
console.log(res)
})