JavaScript code optimization new tool UGLIFYJS

Source: Internet
Author: User
Tags abstract array command line php class version variable tostring install node

When JQuery 1.5 was released, John Resig said that the code optimizer used to switch from Google closure to Uglifyjs, the new tool has a very satisfying compression effect. UGLIFYJS is a server-side node.js compression program. I tested the compression rate is really high. So it is worth writing an article to recommend under.

You can also try the online version of uglifyjs:http://sweet.fengyin.name/

If you are interested in UGLIFYJS can be installed according to the following installation methods.

1. Install Node.js Environment

2. Enter the Https://github.com/mishoo/UglifyJS right corner "Download" Zip download the entire package.

3. Unpack and open Uglifyjs/bin/uglifyjs

4. Find

 
  
  
  1. Global.sys = require (/^v0\.[ 012]/.test (process.version)? "SYS": "Util");
  2. var fs = require ("FS");
  3. var uglify = require ("Uglify-js"),//Symlink ~/.node_libraries/uglify-js.js to.. /uglify-js.js
  4. JSP = Uglify.parser,
  5. Pro = Uglify.uglify;

Replaced by

 
  
  
  1. Global.sys = require (/^v0\.[ 012]/.test (process.version)? "SYS": "Util");
  2. var fs = require ("FS");
  3. Require.paths.unshift (__dirname + "/.. /");
  4. var uglify = require ("index"),
  5. JSP = Uglify.parser,
  6. Pro = Uglify.uglify;

5. CD command to uglifyjs/bin/directory. Execute./UGLIFYJS/Your JS path/xx.js if the installation succeeds, your zip code will be displayed directly on the command line. You can use the./uglifyjs 1.js 2.js to save the compressed code to 2.js

Now that the UGLIFYJS installation is complete, I have also written a PHP class. Can call UGLIFYJS for compression

 
 
  1. /*
  2. # Code by Https://fengyin.name
  3. # DEMO http://sweet.fengyin.name/
  4. # Dual licensed under MIT
  5. */
  6. $UGLIFYJS = new Uglifyjs (Array (
  7. ' Node_home ' => '/usr/local/bin/node ',
  8. ' Uglifyjs_path ' => '/usr/uglifyjs/bin/uglifyjs ',//uglifyjs the path of the compression tool.
  9. --------Additional parameters---------//
  10. ' Prefix ' => '-B ',//input parameter-B is formatted without input for compression, and more please refer to command usage.
  11. ' Append ' => '//save file, if empty, directly output the result at the command line.
  12. ));
  13. $results = $uglifyjs->compress ($_files[' file '] [' tmp_name ']);
  14. Class uglifyjs{
  15. function __construct ($options = Array ()) {
  16. $this->options = $options;
  17. }
  18. function args ($option) {
  19. return $option [' Node_home ']. ' '. $option [' Uglifyjs_path ']. '
  20. '. $option [' prefix ']. ' '. $option [' file ']. ' '. $option [' Append '];
  21. }
  22. function exec ($cmd) {
  23. EXEC ($cmd. ' 2>&1 ', $out, $status);
  24. Return Json_encode Array (
  25. ' Shell ' => $cmd,
  26. ' Output ' => implode ("\ \", $out),
  27. ' Status ' => $status
  28. ));
  29. }
  30. function Compress ($file) {
  31. $this->options[' file '] = $file;
  32. return $this->exec ($this->args ($this->options));
  33. }
  34. }
  35. ?>

The end of the article is attached to the UGLIFYJS command line Chinese instructions

Use:

This is a script helper (tool)-bin/uglifyjs-Use this library to compress a script to the minimum.

Brief introduction:

UGLIFYJS [Options ...] File

File parameters should be placed behind the options, and UGLIFYJS will read the JavaScript code in the file for processing.

If you do not specify the file name of the output, then he will output the processed content to the command line.

Example: Uglifyjs 1.js 2.js will create a 2.js file to enter the 1.js processed content into this

Example: Uglifyjs 1.js reads 1.js content and outputs the results to the command line.

Supported options:

-B or--beautify-parameter-B or--beautify is used to beautify (format) the code. The code will be compressed before it is beautified (not validated.) )

-i n or--indent N-indent level of spaces

Example: Uglifyjs-b 1.js 2.js will be formatted with 1.js content and then output to 2.js

-Q or--quote-keys-quote keys in literal objects (by default, only the keys that cannot is identifier names would be quotes).

The-NM or--no-mangle-variable name does not shorten, meaning that the variable name of the code is not shortened to ABCDEFG

-ns or--no-squeeze-does not use Ast_squeeze () (You can make the code after the various optimizations smaller and more readable. )

-MT or--mangle-toplevel-mangle names in the toplevel scope too (by default we don ' t does this).

--no-seqs-when Ast_squeeze () is invoked (unless you add a--no-squeeze parameter to not be invoked) it reduces the rows that are repeatedly declared in a block of code. For example: "a = 10;" b = 20; Foo (); " will be rewritten as "a=10,b=20,foo ();". On various occasions, this will allow us to discard the parentheses in the code block (after some of the statement blocks become a separate declaration line). This is the default because, as we tested it, it seems to be more secure and can save hundreds of bytes, but it will be banned after adding--no-seqs.

--no-dead-code-by default, UGLIFYJS will delete statements that are clearly inaccessible in your code (such as return, throw, break or continue statements, and some declarations that are not functions/variables). This option allows you to disable this optimization feature.

-NC or--no-copyright-By default, UGLIFYJS will retain some of the initial markup (assuming copyright information, etc.) in the generated code. If you set this parameter will be prohibited.

The-o filename or--output filename-outputs the results 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 result will be written into the same file.

--ast-pass this is want to get the Abstract Syntax tree instead of JavaScript as output. Useful for debugging or learning about the internals.

--ast-pass If you're going to get an abstract syntax tree that replaces JavaScript output, these parameters can be useful when you're debugging or learning about internal components.

-V or--verbose-output some notes on STDERR (for now just how long, Operation takes).

-V or--verbose-output some standard error hints (showing how long each time is spent)

--extra-enable additional optimizations that have not yet been the extensively. These are might, or might not, and break your code. If you have a bug using this option, please have a test case.

When you find a bug you can add a breakpoint to optimize the code that has not yet been thoroughly tested, and you can use this option to generate a test case.

--extra-enable

--unsafe-enable additional optimizations that are known to is unsafe in some contrived situations, but could still be generally useful. For now, this:

Foo.tostring () ==> foo+ ""

It is also possible to foo.tostring () this usage, but there are some people insecure situations that need to be added to some optimizations such as: foo+ ""

--max-line-len (default 32K characters)-add a newline after around 32K characters. I ' ve seen both FF and Chrome croak when all the code is on a single of around 670K. Pass–max-line-len 0 To disable this safety feature.

When you add a line to more than about 32K bytes, I've found that it beeps when Firefox and Chorme have more than 670K in the next line of code. By –max-line-len 0 to still this security attribute

--reserved-names-some libraries rely on certain names to is used, as pointed out with issue #92 and #81, so option al Exclude such names from the Mangler. For example, to keep names require and $super intact your ' d specify–reserved-names ' require, $super.

--reserved-names-Some class libraries rely on specific names, such as #92 and #81 so this option allows you to exclude some keywords.

"Edit Recommendation"

    1. Super JavaScript Editor Webstorm 2.1 release
    2. JavaScript cross-domain summarization and solutions
    3. Implementing tabular data Management with JavaScript
    4. Hacker survey: What is the most popular JavaScript library?
    5. JavaScript version of several common sorting algorithms to share
"Responsible editor: Chen Yu new TEL: (010) 68476606"


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.