1. UglifyJS
UglifyJS is a JavaScript compression tool written in JavaScript.
Official website:/uglifyjs/
1. Install UglifyJS through NPM
(1) Installation
From the official website/en/Download the installation program for the corresponding platform. The current latest version 11.4.0 and the recommended version 10.14.2.
I downloaded 10.14.2, and it is a node-v10.14. installation package, and install it according to the default next step.
After the installation is successful, enter node -v at the cmd command prompt to display the version number:
C:\Users\lc>node -v
v10.14.2
To exit the environment, press Ctrl+C twice.
(2) About NPM
NPM is the default package management tool installed with NodeJS. Through NPM, code can be installed, shared, distributed, and managed project dependencies.
Enter npm -v at the cmd command prompt to display the version number:
C:\Users\lc>node -v
6.4.1
(3) Install UglifyJS
Enter at the cmd command prompt:
npm install uglify-js
2. Compress JS files
(1) At the cmd command prompt, use the cd command to locate the directory where the JS file is located;
(2) Enter the compression command:
uglifyjs -c -m -o
And is the file to be compressed, and is the file after compression.
Common parameters description:
-o,--output Specifies the output file, which is the command line by default
-b,--beautify Parameters for beautifying code format
-m,--mangle Change variable name
-r,--reserved The reserved variable name does not need to be changed by the -m parameter
-c,--compress code compression
--comments The code used to control comments
2. Google Closure Compiler
The Closure Compiler application is a Java command line tool used to compress, optimize and troubleshoot JavaScript code.
Official website:/closure/compiler/
1. Download:
The official website download address is/closure-compiler/
After decompression, there are 3 files:
COPYING
Open the help document and you can see a line that explains: Closure Compiler requires Java 8 or above.
2. Compress js files from the command line:
(1) Create a new test file in the decompressed ClosureCompiler directory
(2) Open the cmd command line and locate cd into the ClosureCompiler directory;
(3) Enter the compression command:
java -jar --js --js_output_file
It is a file to be compressed, and multiple files can be spaced apart, which is a compressed file.
The above is the detailed content of the basic usage of the JavaScript code compression tools UglifyJS and Google Closure Compiler. For more information on the usage of UglifyJS and Google Closure Compiler, please follow my other related articles!