当前位置:网站首页>Compress JS code with terser
Compress JS code with terser
2022-07-07 02:27:00 【An ice cream】
webapck
Compression is provided in js The way the code works , You can remove useless code 、 Replace variable name, etc , Reduce the volume of compiled files , Increase loading speed .
Different mode
stay webpack
The configuration file webpack.config.js
Through will mode
Set to development
perhaps production
, The code will be processed differently .
You can find ,production
Files compiled in mode , File and variable names have been modified 、 Blank line breaks are removed , Even if you don't configure it yourself ,webpack We will also set production
By default, some attributes are added in the mode of , Here, for example. js Code compression uses TerserPlugin
.
terser
TerserPlugin
Processing code depends on terser
This tool , terser
It can be installed directly and used independently , When using, there are many configurations that can be defined by yourself , For details, please refer to Official documents
One of them belongs to compress options
- arrows — The arrow function body in the object has only one sentence
- arguments — arguments Parameter conversion
- dead_code — Remove unreachable code (remove unreachable code)
The following belong to mangle options
- toplevel — Should the top-level scope be defaced
- keep_classnames — The class name is reserved
- keep_fnames — Keep the function name
adopt npm install terser
After installing dependency , Direct execution terser Command statement npx terser ./src/index.js -o ./terser/default.js
, There is no configuration here , So the default processing method is used , Only remove line breaks .
Customize js How the code is compiled ,npx terser ./src/index.js -o terser/index.min.js -c arguments,arrows=true -m toplevel,keep_classnames,keep_fnames
The above configuration indicates
- Function to use arguments when , Convert to formal parameter
- When the arrow function body has only one sentence , Remove return
- Vilify variables in the top-level scope , For example, the variable name message Turn into o
- Keep the class name
- Keep the function name
You can see , The compiled code removes spaces and newlines , And some other specified processing
To make it easier to read , Format the compiled code
TerserPlugin
In the project , There's a lot of js Files need to be compressed , It will be too troublesome to specify the Compilation Rules one by one , adopt TerserPlugin Unified configuration can solve this problem .
adopt npm install terser-webpack-plugin --save-dev
After installing dependency , stay webpack.config.js
The corresponding configuration is defined in the file , More configurations can be found in Official documents
const TerserPlugin = require('terser-webpack-plugin');
module.exports = {
// Other configurations are omitted
mode: 'production',
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
terserOptions: {
compress: {
arguments: true,
dead_code: true,
},
toplevel: true,
keep_classnames: true,
keep_fnames: true,
},
}),
],
},
};
Of the compiled file js The code is compressed into one line , After formatting, you can see the corresponding processing
summary
terser
It's a tool , With compression 、 Conversion processing js Code and other functions , Through the command line, you can directly js File for compilation .
But in the project , Use it directly terser
Too complicated , So with terser-webpack-plugin
Unified compilation , When mode
by production
when , There is a default configuration , You can also define processing rules by yourself .
That's all terser and TerserPlugin Introduction to , More information about webpack
You can refer to my other blog posts , Ongoing update ~
边栏推荐
- How can reinforcement learning be used in medical imaging? A review of Emory University's latest "reinforcement learning medical image analysis", which expounds the latest RL medical image analysis co
- Stm32f4 --- general timer update interrupt
- Infrared camera: juge infrared mag32 product introduction
- 处理streamlit库上传的图片文件
- Time synchronization of livox lidar hardware -- PPS method
- Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
- 【论文阅读|深读】ANRL: Attributed Network Representation Learning via Deep Neural Networks
- [unity] upgraded version · Excel data analysis, automatically create corresponding C classes, automatically create scriptableobject generation classes, and automatically serialize asset files
- Introduction to RC oscillator and crystal oscillator
- Decryption function calculates "task state and lifecycle management" of asynchronous task capability
猜你喜欢
Introduction to FLIR blackfly s industrial camera
传感器:土壤湿度传感器(XH-M214)介绍及stm32驱动代码
Big guys gather | nextarch foundation cloud development meetup is coming!
Web3的先锋兵:虚拟人
一片葉子兩三萬?植物消費爆火背後的“陽謀”
Schedulx v1.4.0 and SaaS versions are released, and you can experience the advanced functions of cost reduction and efficiency increase for free!
Increase 900w+ playback in 1 month! Summarize 2 new trends of top flow qiafan in station B
UC伯克利助理教授Jacob Steinhardt预测AI基准性能:AI在数学等领域的进展比预想要快,但鲁棒性基准性能进展较慢
Stm32f4 --- PWM output
新一代云原生消息队列(一)
随机推荐
Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
Jacob Steinhardt, assistant professor of UC Berkeley, predicts AI benchmark performance: AI has made faster progress in fields such as mathematics than expected, but the progress of robustness benchma
Alibaba cloud middleware open source past
压缩 js 代码就用 terser
如何从0到1构建32Core树莓派集群
[unity] upgraded version · Excel data analysis, automatically create corresponding C classes, automatically create scriptableobject generation classes, and automatically serialize asset files
ZABBIX 5.0: automatically monitor Alibaba cloud RDS through LLD
RC振荡器和晶体振荡器简介
【Node学习笔记】chokidar模块实现文件监听
Data connection mode in low code platform (Part 1)
C#/VB.NET 删除Word文檔中的水印
新一代云原生消息队列(一)
FLIR blackfly s industrial camera: explanation and configuration of color correction and code setting method
3--新唐nuc980 kernel支持jffs2, Jffs2文件系统制作, 内核挂载jffs2, uboot网口设置,uboot支持tftp
#yyds干货盘点# 解决名企真题:最大差值
1--新唐nuc980 NUC980移植 UBOOT,从外部mx25l启动
A new path for enterprise mid Platform Construction -- low code platform
Introduction to the internal structure of the data directory of PostgreSQL
猿桌派第三季开播在即,打开出海浪潮下的开发者新视野
The empirical asset pricing package (EAP) can be installed through pypi