当前位置:网站首页>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 ~
边栏推荐
- Chang'an chain learning notes - certificate model of certificate research
- TiFlash 源码阅读(四)TiFlash DDL 模块设计及实现分析
- Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
- Integerset of PostgreSQL
- Processing image files uploaded by streamlit Library
- postgresql 之 数据目录内部结构 简介
- Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
- [unity notes] screen coordinates to ugui coordinates
- Lidar: introduction and usage of ouster OS
- 机器人队伍学习方法,实现8.8倍的人力回报
猜你喜欢

C#/VB.NET 删除Word文档中的水印

3D激光SLAM:Livox激光雷达硬件时间同步

FLIR blackfly s usb3 industrial camera: white balance setting method

postgresql之integerset

This week's hot open source project!

张平安:加快云上数字创新,共建产业智慧生态

1个月增长900w+播放!总结B站顶流恰饭的2个新趋势

FLIR blackfly s industrial camera: explanation and configuration of color correction and code setting method

Integerset of PostgreSQL

处理streamlit库上传的图片文件
随机推荐
Integrated navigation: product description and interface description of zhonghaida inav2
Use of pgpool II and pgpooladmin
fiddler的使用
Lidar: introduction and usage of ouster OS
云原生混部最后一道防线:节点水位线设计
Halcon实例转OpenCvSharp(C# OpenCV)实现--瓶口缺陷检测(附源码)
C语言练习题_1
FLIR blackfly s usb3 industrial camera: how to use counters and timers
leetcode:736. LISP syntax parsing [flowery + stack + status enumaotu + slots]
【LeetCode】Day97-移除链表元素
企业中台建设新路径——低代码平台
Overall query process of PostgreSQL
Blackfly s usb3 industrial camera: buffer processing
Robot team learning method to achieve 8.8 times human return
Data connection mode in low code platform (Part 1)
MetaForce原力元宇宙开发搭建丨佛萨奇2.0系统开发
B站6月榜单丨飞瓜数据UP主成长排行榜(哔哩哔哩平台)发布!
The boss is quarantined
Stm32f4 --- general timer update interrupt
激光雷达:Ouster OS产品介绍及使用方法