当前位置:网站首页>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 ~
边栏推荐
猜你喜欢
AWS学习笔记(一)
Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
建议收藏!!Flutter状态管理插件哪家强?请看岛上码农的排行榜!
将截断字符串或二进制数据
张平安:加快云上数字创新,共建产业智慧生态
Station B's June ranking list - feigua data up main growth ranking list (BiliBili platform) is released!
【森城市】GIS数据漫谈(二)
FLIR blackfly s industrial camera: auto exposure configuration and code
Overall query process of PostgreSQL
Decryption function calculates "task state and lifecycle management" of asynchronous task capability
随机推荐
压缩 js 代码就用 terser
postgresql之整體查詢大致過程
建议收藏!!Flutter状态管理插件哪家强?请看岛上码农的排行榜!
处理streamlit库上传的图片文件
Tiflash source code reading (IV) design and implementation analysis of tiflash DDL module
Dall-E Mini的Mega版本模型发布,已开放下载
Web3对法律的需求
Halcon knowledge: segment_ contours_ XLD operator
Freeswitch dials extension number source code tracking
Gee upgrade can realize one piece of run tasks
postgresql 之 数据目录内部结构 简介
STM32项目 -- 选题分享(部分)
张平安:加快云上数字创新,共建产业智慧生态
Draco - glTF模型压缩利器
Vingt - trois mille feuilles? "Yang mou" derrière l'explosion de la consommation végétale
本周 火火火火 的开源项目!
The last line of defense of cloud primary mixing department: node waterline design
Metaforce force meta universe development and construction - fossage 2.0 system development
Introduction to RC oscillator and crystal oscillator
The mega version model of dall-e MINI has been released and is open for download