头图

The benefits of minifying JS

(1) Reduce the size of the file;
(2) Reduce network transmission volume and bandwidth occupation;
(3) Reduce the processing pressure of the server;
(4) Improve the speed of page rendering and display.

Install the uglify plugin

 # 执行命令: 
npm install uglify-js -g

single file compression

 # 使用方法:uglifyjs + 要压缩的js文件名称 +  -o +  压缩后js文件名称
uglifyjs vendor.js -o vendor.min.js

The file size is significantly smaller after compression! ! !

Bulk compression method

(1) Create a new txt file with the following content

 @echo off
:: 设置压缩JS文件的根目录,脚本会自动按树层次查找和压缩所有的JS(注意路劲中不能有空格)
SET JSFOLDER=D:\uglifyDestination
echo 正在查找JS文件
chdir /d %JSFOLDER%
for /r . %%a in (*.js) do (
    @echo 正在压缩 %%~a ...
    uglifyjs %%~fa  -m -o %%~fa
)
echo 完成!
pause & exit

(2) Modify the file to a .bat file (3) Place the js file to be compressed in the specified directory (for example: D:\uglifyDestination)
(4) Double-click the bat file to start compression


前端小菜
266 声望8 粉丝

努力可以改变能力