webpack最小化lodash

- 日理万妓 2022-03-27 06:50 350阅读 0赞

lodash作为一个比较常用的前端开发工具集,在使用webpack进行vendor分离的实践中,会遇到将整个lodash文件分离到vendor.js的问题。这样会使vendor.js文件变得特别大。

webpack.config.js文件代码如下:

  1. var path = require('path');
  2. module.exports = mode => {
  3. return {
  4. entry: {
  5. A: './moduleA.js',
  6. B: './moduleB.js',
  7. C: './moduleC.js',
  8. },
  9. mode: mode,
  10. output: {
  11. path: path.resolve(__dirname, 'dist/'),
  12. filename: '[name].js'
  13. },
  14. optimization: {
  15. usedExports: true,
  16. splitChunks: {
  17. cacheGroups: {
  18. commons: {
  19. chunks: 'all',
  20. minChunks: 2,
  21. maxInitialRequests: 5,
  22. minSize: 0
  23. },
  24. vendor: {
  25. test: /node_modules/,
  26. chunks: "initial",
  27. name: "vendor",
  28. priority: 10,
  29. enforce: true
  30. }
  31. }
  32. }
  33. },
  34. module: { },
  35. plugins: [ ]
  36. }
  37. }

运行npm run test脚本命令,结果如下:

  1. Hash: 5d86af7ed04c57cca071
  2. Version: webpack 4.28.4
  3. Time: 5707ms
  4. Built at: 2019-01-11 19:25:04
  5. Asset Size Chunks Chunk Names
  6. A.js 1.46 KiB 3 [emitted] A
  7. B.js 1.53 KiB 4 [emitted] B
  8. C.js 1.54 KiB 5 [emitted] C
  9. commons~A~B~C.js 132 bytes 0 [emitted] commons~A~B~C
  10. commons~A~C.js 238 bytes 1 [emitted] commons~A~C
  11. vendor.js 69.7 KiB 2 [emitted] vendor
  12. Entrypoint A = vendor.js commons~A~B~C.js commons~A~C.js A.js
  13. Entrypoint B = commons~A~B~C.js B.js
  14. 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代码片段。

安装所需依赖:

  1. yarn add babel-loader @babel/core @babel/preset-env babel-plugin-lodash --dev

像下面这样修改webpack.config.js配置文件:

  1. ...
  2. module: {
  3. rules: [
  4. {
  5. test: /\.js$/,
  6. exclude: /node_modules/,
  7. use: {
  8. loader: 'babel-loader',
  9. options: {
  10. presets: ['@babel/preset-env'],
  11. plugins: ['lodash']
  12. }
  13. }
  14. }
  15. ]
  16. }
  17. ...

运行npm run test,脚本命令结果如下:

  1. Hash: 30def5521978552cc93d
  2. Version: webpack 4.28.4
  3. Time: 3249ms
  4. Built at: 2019-01-11 21:25:23
  5. Asset Size Chunks Chunk Names
  6. A.js 1.46 KiB 3 [emitted] A
  7. B.js 1.53 KiB 4 [emitted] B
  8. C.js 1.54 KiB 5 [emitted] C
  9. commons~A~B~C.js 132 bytes 0 [emitted] commons~A~B~C
  10. commons~A~C.js 226 bytes 1 [emitted] commons~A~C
  11. vendor.js 502 bytes 2 [emitted] vendor
  12. Entrypoint A = vendor.js commons~A~B~C.js commons~A~C.js A.js
  13. Entrypoint B = commons~A~B~C.js B.js
  14. 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依赖:

  1. yarn add lodash-webpack-plugin --dev

修改webpack.config.js配置文件如下:

  1. var LodashModuleReplacementPlugin = require('lodash-webpack-plugin');
  2. ...
  3. plugins: [
  4. new LodashModuleReplacementPlugin,
  5. ]
  6. ...

运行npm run test脚本命令,结果如下所示:

  1. Hash: 30def5521978552cc93d
  2. Version: webpack 4.28.4
  3. Time: 2481ms
  4. Built at: 2019-01-11 21:07:23
  5. Asset Size Chunks Chunk Names
  6. A.js 1.46 KiB 3 [emitted] A
  7. B.js 1.53 KiB 4 [emitted] B
  8. C.js 1.54 KiB 5 [emitted] C
  9. commons~A~B~C.js 132 bytes 0 [emitted] commons~A~B~C
  10. commons~A~C.js 226 bytes 1 [emitted] commons~A~C
  11. vendor.js 502 bytes 2 [emitted] vendor
  12. Entrypoint A = vendor.js commons~A~B~C.js commons~A~C.js A.js
  13. Entrypoint B = commons~A~B~C.js B.js
  14. 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

发表评论

表情:
评论列表 (有 0 条评论,350人围观)

还没有评论,来说两句吧...

相关阅读

    相关 费用和附加值

    费用最小化   1.提前进行成本预算     开发前要提前进行成本预算,对开发过程中的所有支出和成本提前进行预测,做好心理预期和财务准备。(例如:做好开发计划,列出

    相关 曼哈顿距离

    曼哈顿距离 曼哈顿距离和欧式距离一样是一种距离度量标准,不同的是它定义在L1范数下,也即用绝对值来衡量两点之间的距离。在一维空间下,曼哈顿距离定义如下: d ( x