lodash
作為一個比較常用的前端開發(fā)工具集仅父,在使用webpack進(jìn)行vendor
分離的實踐中叛薯,會遇到將整個lodash
文件分離到vendor.js
的問題。這樣會使vendor.js
文件變得特別大笙纤。
webpack.config.js
文件代碼如下:
var path = require('path');
module.exports = mode => {
return {
entry: {
A: './moduleA.js',
B: './moduleB.js',
C: './moduleC.js',
},
mode: mode,
output: {
path: path.resolve(__dirname, 'dist/'),
filename: '[name].js'
},
optimization: {
usedExports: true,
splitChunks: {
cacheGroups: {
commons: {
chunks: 'all',
minChunks: 2,
maxInitialRequests: 5,
minSize: 0
},
vendor: {
test: /node_modules/,
chunks: "initial",
name: "vendor",
priority: 10,
enforce: true
}
}
}
},
module: { },
plugins: [ ]
}
}
運行npm run test
腳本命令耗溜,結(jié)果如下:
Hash: 5d86af7ed04c57cca071
Version: webpack 4.28.4
Time: 5707ms
Built at: 2019-01-11 19:25:04
Asset Size Chunks Chunk Names
A.js 1.46 KiB 3 [emitted] A
B.js 1.53 KiB 4 [emitted] B
C.js 1.54 KiB 5 [emitted] C
commons~A~B~C.js 132 bytes 0 [emitted] commons~A~B~C
commons~A~C.js 238 bytes 1 [emitted] commons~A~C
vendor.js 69.7 KiB 2 [emitted] vendor
Entrypoint A = vendor.js commons~A~B~C.js commons~A~C.js A.js
Entrypoint B = commons~A~B~C.js B.js
Entrypoint C = vendor.js commons~A~B~C.js commons~A~C.js C.js
如上面的情況,vendor.js
文件為69.7kb
省容,如果再引用了其他第三方庫抖拴,文件會更大。
那么腥椒,如何在開發(fā)的過程中阿宅,壓縮lodash
呢?
babel-loader & babel-plugin-lodash
可以使用babel-loader
在對*.js
文件進(jìn)行解析笼蛛,然后借助于babel-plugin-lodash
插件對引用的lodash
進(jìn)行類似tree shaking
的操作洒放,這樣就可以去除未使用的lodash
代碼片段。
安裝所需依賴:
yarn add babel-loader @babel/core @babel/preset-env babel-plugin-lodash --dev
像下面這樣修改webpack.config.js
配置文件:
...
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
plugins: ['lodash']
}
}
}
]
}
...
運行npm run test
滨砍,腳本命令結(jié)果如下:
Hash: 30def5521978552cc93d
Version: webpack 4.28.4
Time: 3249ms
Built at: 2019-01-11 21:25:23
Asset Size Chunks Chunk Names
A.js 1.46 KiB 3 [emitted] A
B.js 1.53 KiB 4 [emitted] B
C.js 1.54 KiB 5 [emitted] C
commons~A~B~C.js 132 bytes 0 [emitted] commons~A~B~C
commons~A~C.js 226 bytes 1 [emitted] commons~A~C
vendor.js 502 bytes 2 [emitted] vendor
Entrypoint A = vendor.js commons~A~B~C.js commons~A~C.js A.js
Entrypoint B = commons~A~B~C.js B.js
Entrypoint C = vendor.js commons~A~B~C.js commons~A~C.js C.js
vendor.js
文件從69.7kb
降至502bytes
往湿。
根據(jù)babel-plugin-lodash
參考文檔介紹榨为,使用lodash-webpack-plugin
可以進(jìn)一步壓縮lodash
。
lodash-webpack-plugin
安裝lodash-webpack-plugin
依賴:
yarn add lodash-webpack-plugin --dev
修改webpack.config.js
配置文件如下:
var LodashModuleReplacementPlugin = require('lodash-webpack-plugin');
...
plugins: [
new LodashModuleReplacementPlugin,
]
...
運行npm run test
腳本命令煌茴,結(jié)果如下所示:
Hash: 30def5521978552cc93d
Version: webpack 4.28.4
Time: 2481ms
Built at: 2019-01-11 21:07:23
Asset Size Chunks Chunk Names
A.js 1.46 KiB 3 [emitted] A
B.js 1.53 KiB 4 [emitted] B
C.js 1.54 KiB 5 [emitted] C
commons~A~B~C.js 132 bytes 0 [emitted] commons~A~B~C
commons~A~C.js 226 bytes 1 [emitted] commons~A~C
vendor.js 502 bytes 2 [emitted] vendor
Entrypoint A = vendor.js commons~A~B~C.js commons~A~C.js A.js
Entrypoint B = commons~A~B~C.js B.js
Entrypoint C = vendor.js commons~A~B~C.js commons~A~C.js C.js
vendor.js
依然是502 bytes
随闺,問題不在loadsh-webpack-plugin
,它雖然會進(jìn)一步優(yōu)化lodash
蔓腐,但是在無法進(jìn)一步優(yōu)化的情況下矩乐,它也沒辦法。
一般情況下回论,不使用lodash-webpack-plugin
就可以滿足開發(fā)的需要散罕,但是文件特別大的情況下,建議還是使用它傀蓉。