当前位置: 首页 > news >正文

lodash-webpack-plugin插件

进行webpack打包优化时,为了缩小lodash的打包体积使用了lodash-webpack-plugin插件,但是运行项目后页面报错Uncaught TypeError: predicate is not a function

解决方法:使用插件时开启对应的配置:(开启后包体积会相应增加)

shorthandsIteratee 是_.property,_.matches, &的简写形式_.matchesProperty
cloning支持“克隆”方法和克隆源对象。
currying支持“咖喱”方法。
caching缓存诸如_.cloneDeep,_.isEqual, & 之类的方法_.uniq
collections支持“Collection”方法中的对象。
exotics支持缓冲区、映射、集合、符号、类型化数组等对象。
guards主机对象、稀疏数组和其他边缘情况的防护。
metadata用于减少绑定、柯里化和部分应用函数包装的元数据。
(需要currying
deburring支持字母去毛刺。
unicode支持 Unicode 符号。
chaining支持链序列的组件。
memoizing支持_.memoize和记忆。
coercions支持将值强制转换为整数、数字和字符串。
flattening支持“展平”方法和展平其余参数。
paths_.get,_.has, & 等方法的深度属性路径支持_.set
placeholders

参数占位符支持“bind”、“curry”和“partial”方法。
(需要currying

1. Uncaught TypeError: predicate is not a function :

'shorthands': true, 'collections': true,

2. Uncaught TypeError: Cannot read properties of undefined (reading 'length')

'flattening': true,​​​​​​​

记录一下使用lodash-webpack-plugin插件

使⽤ babel-plugin-lodash 和 lodash-webpack-plugin:

babel-plugin-lodash: 对代码中的 import _ from 'lodash'或者import { add } from 'lodash/fp'进⾏编译优化。确保引⼊最⼩的包。
lodash-webpack-plugin:⽤于精简lodash,去除了⼀些特性,如果需要⽤到,则需⼿动开启。

配置方式如下:
安装babel-plugin-lodash后配置:

extraBabelPlugins: [ ['lodash'], ],

lodash-webpack-plugin配置为:

import lodashModuleReplacementPlugin from 'lodash-webpack-plugin'; chainWebpack(config, { webpack }) { //优化lodash config.plugin("loadshReplace").use(new LodashModuleReplacementPlugin({ // 'caching': true, // 'chaining': true, // 'currying': true, // 'cloning': true, // 'coercions': true, // 'collections': true, // 'deburring': true, // 'exotics': true, // 'flattening': true, // 'guards': true, // 'memoizing': true, // 'metadata': true, // 'paths': true, // 'placeholders': true, // 'shorthands': true, // 'unicode': true, })); },

注:lodash优化效果有限:随着系统的增⻓,引⼊的第三⽅库越来越多。⽽第三⽅库中,有可能使⽤require('lodash')引⼊lodash,导致我们项⽬中对lodash的优化⽆效。所以,可以不⽤太过于纠结lodash的优化。

http://www.jsqmd.com/news/826146/

相关文章:

  • 别再死记硬背了!用Python实战搞定贾俊平《统计学》第四章核心考点(附代码与数据)
  • 开源法律知识库:结构化数据驱动法律科技应用
  • Python-ADB安全认证详解:从RSA密钥到设备授权的最佳实践
  • WebStorm模板通配符
  • 别再只跑回归了!用Stata做异方差检验与修正的完整工作流(含稳健标准误)
  • 拆解MC1496乘法器:如何在没有现成库的Multisim里,手动封装一个调幅核心模块
  • AI编码助手:从架构设计到工程实践,打造你的智能开发副驾驶
  • AI智能体技能库:构建可复用、标准化与安全的应用能力模块
  • Web前端之指定元素优先列布局的实现原理、使用数据驱动实现Grid布局、Grid首列锚定算法
  • AI提示词工程化:从GitHub项目到团队协作的工程实践
  • Arm SystemReady ACS测试指南与硬件兼容性认证
  • sagents框架实战:从零构建具备记忆与协作能力的AI智能体
  • 儒卓力CITE首秀:技术分销如何赋能嵌入式、汽车电子与物联网创新
  • Adv_Fin_ML_Exercises特征重要性分析:5种方法对比
  • GEE python:影像的一元线性趋势性分析linearfit函数
  • Blender FLIP Fluids渲染与合成:打造电影级液体效果的10个关键技术要点
  • Kubernetes监控与可观测性最佳实践
  • Simplefolio最佳实践案例:10个成功的开发者作品集展示
  • 构建AI智能体调度平台:从微服务架构到工程实践
  • VTK开发精要:数据与管线机制
  • Cursor AI代码优化工具:自动检测与重构冗余API调用
  • Coding Agent 正在偷走你的控制权?慢下来,守住开发者的核心地位!
  • Augustus核心功能深度解析:路障、劳动力池与仓库管理
  • Jdbc手动实现事务管理
  • 深入PEX8796:从Serdes到Virtual Switch,图解PCIe交换芯片的三种工作模式
  • FPGA开发板GT远端环回测试:原理、配置与调试实战指南
  • RAG是什么?为什么Agent必须用RAG?
  • pgwatch2在Kubernetes中的部署:Helm Chart完全解析
  • Cursor AI编程助手规则文件(.cursorrules)配置指南与最佳实践
  • AI+Web3开发实战:Helius Core-AI如何赋能Solana智能体应用