At the risk of being scolded, I sent it to the homepage. If I am on Mars. Ignore me ......
When jquery 1.5 was released, John resig said that the code optimization program was switched from Google closure to uglifyjs. The compression effect of the new tool was very satisfactory. Uglifyjs is a server node. js compression program. It is said to be very pornographic and violent ......
I tested that the compression ratio is indeed relatively high. Therefore, it is recommended to write an article.
Bytes --------------------------------------------------------------------------------------------
You can also try the online version of uglifyjs: https://ganquan.info/webkit/
Bytes --------------------------------------------------------------------------------------------
If you are interested in uglifyjs, you can install it as follows.
1. Install the node. js environment (I don't need to teach you this. There are a lot of online tutorials .)
2. Go to the upper right corner of the https://github.com/mishoo/UglifyJS and download the entire package.
3. decompress and open uglifyjs/bin/uglifyjs.
4. Find
Global. sys = require (/^ V0 \. [012]/. Test (process. Version )? "Sys": "util"); var FS = require ("FS"); var uglify = require ("uglify-js"), // symlink ~ /. Node_libraries/uglify-js.js to ../uglify-js.js JSP = uglify. parser,
Pro = uglify. uglify;
Replace
Global. sys = require (/^ V0 \. [012]/. Test (process. Version )? "Sys": "util ");
VaR FS = require ("FS"); require. paths. unshift (_ dirname + "/.. /"); var uglify = require (" Index "), JSP = uglify. parser, Pro = uglify. uglify;
5. Run the CD command to the uglifyjs/bin/directory. Execute./uglifyjs XX. js. If the installation is successful, Your compressed code will be directly displayed in the command line.
You can use the following command format./uglifyjs 1.js 2.js to save the compressed 1.js code to 2.js.
So far, the uglifyjs installation is complete, and I have also written a PHP class. You can call uglifyjs for compression.
<? PHP
/*
------------------------------------
# Code by https://fengyin.name
# Demo http://sweet.fengyin.name/
# Dual licensed under the MIT
------------------------------------
*/
$ Uglifyjs = new uglifyjs (Array (
'Node _ home' => '/usr/local/bin/node ',
'Uglifyjs _ path' => '/usr/uglifyjs/bin/uglifyjs', // path of the uglifyjs compression tool.
// -------- Additional parameter ---------//
'Prefix' => '-B', // The input parameter-B is formatted without being compressed. For more information, see command usage.
'Append' => ''// save the file. If it is null, the result is output directly in the command line.
));
$ Results = $ uglifyjs-> compress ($ _ FILES ['file'] ['tmp _ name']);
//////////////////////////
Class uglifyjs {
Function _ construct ($ Options = array ()){
$ This-> Options = $ options;
}
Function ARGs ($ option ){
Return $ option ['node _ home']. ''. $ option ['uglifyjs _ path']. ''. $ option ['prefix']. ''. $ option ['file']. ''. $ option ['append'];
}
Function exec ($ cmd ){
Exec ($ cmd. '2> & 1', $ out, $ status );
Return json_encode (Array (
'Shell' => $ cmd,
'Output' => implode ("\ n", $ out ),
'Status' => $ status
));
}
Function compress ($ file ){
$ This-> options ['file'] = $ file;
Return $ this-> exec ($ this-> ARGs ($ this-> options ));
}
}
?>
The following is a Chinese description of the command line of uglifyjs.
(
Limited to your poor English skills, please take a look.
If there are some translation errors that I don't understand, there will be no translation. Please forgive me. The original English version prevails.
The following is for reference only ~~ Not responsible for translation accuracy.
)
Use: This is a script Assistant (Tool)-bin/uglifyjs-this library can be used to compress a script to a minimum. Introduction: uglifyjs [Options...] [files] File parameters should be placed behind the options. uglifyjs will read the JavaScript code in the file for processing. If you do not specify the output file name, the processed content is output to the command line. For example, uglifyjs 1.js 2.js will create a 2.js file. for example, uglifyjs 1.js reads 1. JS content and output the result to the command line. Supported options:-B or -- beautify-parameter-B or -- beautify is used to beautify (Format) the code. Code will be compressed before beautification (not verified .) -I n or -- indent N-indentation level space number example: uglifyjs-B 1.js 2.js will put 1. JS content format is output to 2. JS-Q or -- quote-keys-quote keys in literal objects (by default, only keys that cannot be identifier names will be quotes ). -nm or -- no-mangle-variable name is not shortened, meaning that the variable name in the Code is not shortened to abcdefg-ns or -- no-squeeze-no use of ast_squeeze () (The optimized code can be smaller and more readable .) -Mt or -- mangle-toplevel-mangle names in the toplevel scope too (by default we don't do this ). -- no-seqs-When ast_squeeze () is called (it will not be called unless you have added the -- no-squeeze parameter), it will reduce the rows that are repeatedly declared in a code block. For example, "A = 10; B = 20; Foo ();" will be rewritten as "A = 10, B = 20, Foo ();". In various cases, this will allow us to discard the parentheses in the code block (after some statement blocks become an independent declaration line ). This is the default one, because it seems safer and can save several hundred bytes, but it will be disabled after -- no-seqs is added. -- No-dead-code-by default, uglifyjs will delete obviously inaccessible statements (such as return, throw, break, or continue statements) in the code, and some declarations that are not functions or variables ). This option can be used to disable this optimization function. -NC or -- no-copyright-by default, uglifyjs retains some initial tags (such as copyright information) in the generated code ). If you set this parameter, it will be disabled. -O filename or -- output filename-output the result to a file with the specified file name. Without this parameter, the result will be output to the command line -- overwrite-if the code is read from a file (not directly standard input) and the -- overwrite parameter is used, the results will also be written into the same file. -- Ast-pass this if you want to get the abstract syntax tree instead of JavaScript as output. useful for debugging or learning more about the internals. -- Ast-Pass: If you want to obtain an abstract syntax tree that replaces JavaScript output, these parameters are useful when you debug or learn internal components. -V or -- verbose-output some notes on stderr (for now just how long each operation takes).-V or -- verbose-output some standard error messages (show how long each time is spent)
-- Extra-enable additional optimizations that have not yet been extensively tested. these might, or might not, break your code. if you find a bug using this option, please report a test case. when you discover a bug, you can add a breakpoint to optimize the code that has not been thoroughly tested. You can use this option to generate a test case. -- Extra-enable -- unsafe-enable other additional optimizations that are known to be unsafe in some contrived situations, but cocould still be generally useful. for now only this: Foo. tostring () => Foo + ""
Want Foo. tostring () is also feasible, but there are some insecure situations. You also need to add some optimizations, such: foo + "-- Max-line-len (default 32 k characters)-Add a newline after around 32 k characters. I 've seen both ff and chrome croak when all the code was on a single line of around 670 K. pass-max-line-len 0 to disable this safety feature. when the size exceeds 32 KB, you need to add a line. I have found that when the line of Firefox and chorme Code exceeds kb, an alarm will be triggered. Use-max-line-len 0 to stop this Security Attribute -- reserved-names-some libraries rely on certain names to be used, as pointed out in Issue #92 and #81, so this option allow you to exclude such names from the Mangler. for example, to keep names require and $ super intact you 'd specify-Reserved-names "require, $ super ".
-- Reserved-names-some class libraries will be used based on specific names, such as: #92 and #81. Therefore, this option allows you to exclude some keywords.