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