lodash作为一个比较常用的前端开发工具集,在使用webpack进行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脚本命令,结果如下:


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,如果再引用了其他第三方库,文件会更大。

那么,如何在开发的过程中,压缩lodash呢?

babel-loader & babel-plugin-lodash

可以使用babel-loader在对*.js文件进行解析,然后借助于babel-plugin-lodash插件对引用的lodash进行类似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,脚本命令结果如下:


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

根据babel-plugin-lodash参考文档介绍,使用lodash-webpack-plugin可以进一步压缩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脚本命令,结果如下所示:


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,它虽然会进一步优化lodash,但是在无法进一步优化的情况下,它也没办法。

一般情况下,不使用lodash-webpack-plugin就可以满足开发的需要,但是文件特别大的情况下,建议还是使用它。

参考源代码

来源:https://segmentfault.com/a/1190000017862101

最新文章

  1. 基于thrift的微服务框架
  2. IBatis.Net使用总结(三)-- IBatis实现分页返回数据和总数
  3. Logstash学习-Hello World
  4. 在Nginx中部署基于IP的虚拟主机
  5. setattribute兼容
  6. PAT乙级真题1016.部分A+B(15)(2016-4-28)
  7. C语言到底怎么分配空间
  8. TortoiseGit - pull request
  9. spring的纯注解的IOC配置
  10. angular 定时函数
  11. openstack-----各种系统镜像制作
  12. LeetCode-450 二叉搜索树删除一个节点
  13. Atom与markdown
  14. keras后端设置【转载】
  15. MySQL中的视图详解
  16. mysql统计函数
  17. [Windows Azure] Adding Sign-On to Your Web Application Using Windows Azure AD
  18. Elon Musk
  19. C语言:结构体和联合体(共用体)
  20. Cmder的安装

热门文章

  1. JEECMS站群管理系统-- 首页的加载过程
  2. Qt 日志输出
  3. GitKraken使用教程-基础部分(1)
  4. 在Java中VO , PO , BO , QO, DAO ,POJO是什么意思
  5. Fluent API配置
  6. OC与JS交互之UIWebView
  7. python面试题——前端(23题)
  8. Mac 下显示隐藏文件或文件夹
  9. python模块详解 YAML和configparser
  10. Mac下配置apach服务