当前位置:网站首页>压缩 js 代码就用 terser
压缩 js 代码就用 terser
2022-07-06 18:40:00 【一颗冰淇淋】
webapck中提供了压缩 js 代码的方式,可以移除无用代码、替换变量名等,减少编译后文件体积,提升加载速度。
不同mode
在 webpack配置文件 webpack.config.js中通过将 mode设置为 development或者 production,会对代码进行不同的处理。

可以发现,production模式下编译的文件,文件及变量名被修改、空格换行被去除,即使自己没有进行配置,webpack 也会在我们设置 production的模式时默认添加一些属性,比如这里js代码压缩用到的就是 TerserPlugin 。

terser
TerserPlugin处理代码依赖的是 terser这个工具, terser 是可以直接安装并独立使用的,使用的时候有非常多的配置可以自行定义,具体可参考 官方文档

其中属于 compress options
- arrows — 对象里的箭头函数函数体只有一句
- arguments — arguments 参数进行转换
- dead_code — 删除不可达的代码 (remove unreachable code)
以下属于 mangle options
- toplevel — 顶层作用域要不要丑化
- keep_classnames — 类名保留
- keep_fnames — 保留函数名
通过 npm install terser安装依赖后,直接执行 terser 命令语句 npx terser ./src/index.js -o ./terser/default.js,这里没有进行配置,所以使用的是默认处理方式,只移除了换行。

自定义js代码的编译方式,npx terser ./src/index.js -o terser/index.min.js -c arguments,arrows=true -m toplevel,keep_classnames,keep_fnames
以上配置表示
- 函数中使用到 arguments 时,转成形参
- 箭头函数体只有一句时,去除 return
- 丑化顶层作用域的变量,比如将变量名 message 变为 o
- 保留类名
- 保留函数名
可以看到,编译后的代码去除了空格和换行,以及一些其它指定的处理

为了更方便阅读,将编译后的代码格式化

TerserPlugin
在项目中,有很多 js 文件需要进行压缩处理,自己一个个命令去指定编译规则的方式会过于麻烦,通过 TerserPlugin 统一配置能够解决这个问题。
通过 npm install terser-webpack-plugin --save-dev安装依赖后,在 webpack.config.js文件中定义对应的配置,更多配置可参考 官方文档
const TerserPlugin = require('terser-webpack-plugin');
module.exports = {
// 其它配置省略
mode: 'production',
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
terserOptions: {
compress: {
arguments: true,
dead_code: true,
},
toplevel: true,
keep_classnames: true,
keep_fnames: true,
},
}),
],
},
};
编译后文件的js代码被压缩到了一行,格式化之后可以看到对应的处理

总结
terser是一个工具,有着压缩、转换处理 js 代码等功能,通过命令行可以直接对 js 文件进行编译。
但在项目中,直接使用 terser过于繁琐,所以借助 terser-webpack-plugin统一编译,当 mode为 production时,有默认的配置,也可以自行定义处理规则。
以上就是 terser 和 TerserPlugin 的介绍, 更多有关 webpack的内容可以参考我其它的博文,持续更新中~
边栏推荐
- Processing image files uploaded by streamlit Library
- 【Unity】升级版·Excel数据解析,自动创建对应C#类,自动创建ScriptableObject生成类,自动序列化Asset文件
- Redis tool class redisutil (tool class III)
- Halcon实例转OpenCvSharp(C# OpenCV)实现--瓶口缺陷检测(附源码)
- ROS learning (XIX) robot slam function package cartographer
- Schedulx v1.4.0 and SaaS versions are released, and you can experience the advanced functions of cost reduction and efficiency increase for free!
- 6 seconds to understand the book to the Kindle
- Metaforce force meta universe development and construction - fossage 2.0 system development
- 使用Ceres进行slam必须要弄清楚的几个类和函数
- FLIR blackfly s industrial camera: configure multiple cameras for synchronous shooting
猜你喜欢

企业中台建设新路径——低代码平台

#夏日挑战赛#数据库学霸笔记(下)~

The last line of defense of cloud primary mixing department: node waterline design

Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem

Flir Blackfly S USB3 工业相机:计数器和定时器的使用方法

Correct use of BigDecimal

The GPG keys listed for the "MySQL 8.0 community server" repository are already ins

String or binary data will be truncated

Flir Blackfly S 工业相机:通过外部触发实现多摄像头同步拍摄

SchedulX V1.4.0及SaaS版发布,免费体验降本增效高级功能!
随机推荐
Flir Blackfly S 工业相机:配置多个摄像头进行同步拍摄
如何从0到1构建32Core树莓派集群
红外相机:巨哥红外MAG32产品介绍
1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
大咖云集|NextArch基金会云开发Meetup来啦!
猫猫回收站
FLIR blackfly s industrial camera: synchronous shooting of multiple cameras through external trigger
6 seconds to understand the book to the Kindle
Integrated navigation: product description and interface description of zhonghaida inav2
Flir Blackfly S 工业相机:自动曝光配置及代码
Centros 8 installation MySQL Error: The gpg Keys listed for the "MySQL 8.0 Community Server" repository are already ins
阿里云易立:云原生如何破解企业降本提效难题?
Lombok同时使⽤@Data和@Builder 的坑
遇到慢SQL该怎么办?(下)
@Before, @after, @around, @afterreturning execution sequence
centos8 用yum 安装MySQL 8.0.x
[unity] upgraded version · Excel data analysis, automatically create corresponding C classes, automatically create scriptableobject generation classes, and automatically serialize asset files
投资的再思考
豆瓣平均 9.x,分布式领域的 5 本神书!
Processing image files uploaded by streamlit Library