How to merge/compress JavaScript with UGLIFYJS basics

Source: Internet
Author: User
Tags git clone
The code in Build.js calls the Uglifyjs interface function to perform the compression task.

1, go to GitHub to download the latest UGLIFYJS. Two ways to download, if Git is installed, go to git console and use the following command
git clone git://github.com/mishoo/uglifyjs.git

or download using the HTTP method, click Zip download. After decompression, its directory structure is as follows

2, create a new item (folder) myApp and copy the Uglify-js.js and LIB directories to your project. As follows

3, create a new compress.js in MyApp, as follows

Copy Code code as follows:

var fs = require (' FS ');
var jsp = require ("./uglify-js"). Parser;
var pro = require ("./uglify-js"). Uglify;

var origcode = "var abc = function () {var one = 5; return one;} ";
var ast = Jsp.parse (Origcode); Parse code and get the initial AST
AST = Pro.ast_mangle (AST); Get a new AST with mangled names
AST = Pro.ast_squeeze (AST); Get a AST with compression optimizations
var finalcode = Pro.gen_code (AST); Compressed code here
Console.log (Finalcode);

The approximate meaning of this code is to take the FS module, which is the file module of node. Then take the UGLIFYJS two modules. The back is the UGLIFYJS compression process.

4, open the command line, execute compress.js

The console outputs the compressed code. Well, that's simple.
5, since in the node environment, of course, you can write a function directly read the source file, compressed and output to the specified directory. Encapsulate the above code into a function, as follows

Copy Code code as follows:

Reads a file, compresses the
function Buildone (Fliein, fileout) {
var origcode = Fs.readfilesync (Fliein, ' UTF8 ');
var ast = Jsp.parse (Origcode);
AST = Pro.ast_mangle (AST);
AST = Pro.ast_squeeze (AST);

var finalcode = Pro.gen_code (AST);

Fs.writefilesync (Fileout, Finalcode, ' UTF8 ');
}

Compress the ajax-1.0.js I write and output it to the MyApp directory
Copy Code code as follows:
Buildone (' ajax-1.0.js ', ' ajax-min.js ');

Sample Code Uglifyjs_test

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.