1. Installing Nodejs
http://nodejs.org/
2. Install the respective node package
JS I'm using UGLIFYJS GitHub address: Https://github.com/mishoo/UglifyJS
CSS I'm using clean-css GitHub address: https://github.com/GoalSmashers/clean-css
The picture is Node-smushit GitHub address: Https://github.com/colorhook/node-smushit
3. Writing code
//js
var
fs = require(
‘fs‘
);
var
jsp = require(
"uglify-js"
).parser;
var
pro = require(
"uglify-js"
).uglify;
function
jsMinifier(flieIn, fileOut) {
var
flieIn=Array.isArray(flieIn)? flieIn : [flieIn];
var
origCode,ast,finalCode=
‘‘
;
for
(
var
i=0; i<flieIn.length; i++) {
origCode = fs.readFileSync(flieIn[i],
‘utf8‘
);
ast = jsp.parse(origCode);
ast = pro.ast_mangle(ast);
ast= pro.ast_squeeze(ast);
finalCode +=
‘;‘
+ pro.gen_code(ast);
}
fs.writeFileSync(fileOut, finalCode,
‘utf8‘
);
}
//jsMinifier(‘./file-src/test2.js‘, ‘./file-smin/test-min.js‘); //单个文件压缩
jsMinifier([
‘./file-src/test.js‘
,
‘./file-src/test2.js‘
],
‘./file-smin/test-min.js‘
);
//合并压缩
命令翻译
|
Uglifyjs use
uglifyjs [ Options ...] [ file]
The file parameters should be placed after the option, 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.
Supported options:
-B or –beautify-The output formatting code, when passed in this parameter, the following additional options for more beautiful control formatting:
-i n or –indent N-indent level (number of spaces)
-Q or –quote-keys-whether to enclose the key of the string object in quotation marks (default only causes the key name that cannot be correctly flagged)
–ASCII-The default UGLIFYJS outputs Unicode characters without processing character encoding and converts non-ASCII encoded characters into sequences of \cxxxx by passing in this parameter (the output is always encoded in UTF8, but the output is ASCII-encoded by passing in this option).
-NM or –no-mangle-does not change the variable name
-ns or –no-squeeze-do not call the Ast_squeeze () function (this function makes a number of optimizations to make the results smaller and less readable)
-MT or –mangle-toplevel-the variable name is scrambled at the top-level scope (default does not open)
–no-seqs-When calling Ast_squeeze () will merge multiple statement blocks into a block of statements, such as "A=10; b=20; Foo () "will be converted to" A=10,b=20,foo () "
–no-dead-code-The default uglifyjs will remove code that is not used, and this function is disabled by passing in the parameter.
-NC or –no-copyright-The default uglifyjs adds comment codes such as copyright information in the output code, which is disabled by passing in the parameter.
-o file name or –output file name-Specifies the output file name, if not specified, prints to standard output (STDOUT)
–overwrite-If the incoming JS code comes in from a file instead of a standard input, the output overwrites the file.
–ast-Passing This parameter gets an abstract syntax tree instead of JavaScript, which is useful for debugging or understanding internal code.
-V or –verbose-output some information in the standard error (current version is only available for output operation)
–extra-Additional optimizations are turned on, and these optimizations are not fully tested.
–unsafe-Turn on additional optimizations that are known to be unsafe under certain circumstances and are currently supported only:
Foo.tostring () ==> foo+ ""
–max-line-len (default 32K bytes)-Adds a newline character to the 32K byte, passing in 0 disables this feature.
–reserved-names-some class libraries will depend on some variables, the name specified by this parameter will not be confused, and multiple separated by commas
//css
var cleanCSS = require(
‘clean-css‘
);
function
cssMinifier(flieIn, fileOut) {
var
flieIn=Array.isArray(flieIn)? flieIn : [flieIn];
var
origCode,finalCode=
‘‘
;
for
(
var
i=0; i<flieIn.length; i++) {
origCode = fs.readFileSync(flieIn[i],
‘utf8‘
);
finalCode += cleanCSS.process(origCode);
}
fs.writeFileSync(fileOut, finalCode,
‘utf8‘
);
}
//cssMinifier(‘./file-src/indexw_20120913.css‘, ‘./file-smin/index.css‘); //单个文件压缩
cssMinifier([
‘./file-src/index_20120913.css‘
,
‘./file-src/indexw_20120913.css‘
],
‘./file-smin/index.css‘
);
//图片
var
imgMinifier = require(
‘node-smushit‘
);
//imgMinifier.smushit(‘./file-src/images‘, {recursive: true}); //递归
imgMinifier.smushit(
‘./file-src/images‘
);
|
Turn from: Use node compression, merge js,css, picture
Node Compression js/css/image