JavaScript code compression tool UglifyJS and basic usage of Google Closure Compiler

Online search, the current mainstream Js code compression tools are Uglify, YUI Compressor, Google Closure Compiler, try the simple and basic usage UglifyJS Google Closure Compiler two tools.

A, UglifyJS 
UglifyJS is written in JavaScript JavaScript compression tool.
Official website: HTTP: //lisperator.net/uglifyjs/
1, by NPM installation UglifyJS
(1) install Node.js
from Node.js official website https://nodejs.org/en/ download the corresponding platform installation program, the current latest version 11.4.0 recommended version 10.14.2.
10.14.2 it downloaded, download is a node-v10.14.2-x64.msi installation package, the next step in accordance with the default installation.
After a successful installation at the input node -v cmd command prompt, you can display the version number:

C:\Users\lc>node -v
v10.14.2

To exit the Node.js environment, double click Ctrl + C.

(2) on NPM
NPM is accompanied NodeJS default package management tools installed together, it can be installed by NPM, share, distribute the code, manage project dependencies.
Enter cmd npm -v at the command prompt, you can display the version number:

C:\Users\lc>node -v
6.4.1

(3) mounted UglifyJS 
the cmd command prompt, type:

npm install uglify-js

2, compressed JS file
(1) at the command prompt cmd, using the cd command to locate the JS file directory;
(2) input to the compressing command:

uglifyjs test.js test2.js -c -m -o test-outpout.js

test.js and test2.js is to be a compressed file, test-outpout.js is a compressed file.
Common parameters:

-o,--output      指定输出文件,默认情况下为命令行
-b,--beautify    美化代码格式的参数
-m,--mangle      改变变量名称
-r,--reserved    保留的变量名称,不需要被-m参数改变变量名的
-c,--compress    代码压缩
--comments       用来控制注释的代码的

Two, the Closure Compiler Google
the Closure Compiler Java application is a command-line tool, used to compress JavaScript code, optimization and troubleshooting.
Official website: HTTPS: //developers.google.com/closure/compiler/
1, download:
official website to download address https://dl.google.com/closure-compiler/compiler-latest.zip
have three files after decompression:

closure-compiler-v20181210.jar
COPYING
README.md

Open README.md help, you can see there is a line Description: Closure Compiler requires Java 8 or later.

2, the command line js file compression:
(1) Create a file text.js tested ClosureCompiler unpacked directory
(2) open cmd command line, and is positioned to ClosureCompiler cd directory;
(3) command input compression:

java -jar closure-compiler-v20181210.jar --js test.js --js_output_file test-outpout.js

test.js file is to be compressed, a space between the plurality of files are available, test-outpout.js file is compressed.

Guess you like

Origin blog.csdn.net/gdjlc/article/details/85060568