Node Compression js/css/image

Source: Internet
Author: User

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

//jsvarfs = require(‘fs‘);varjsp = require("uglify-js").parser;varpro = require("uglify-js").uglify; functionjsMinifier(flieIn, fileOut) {     varflieIn=Array.isArray(flieIn)? flieIn : [flieIn];     varorigCode,ast,finalCode=‘‘;     for(vari=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

//cssvar cleanCSS = require(‘clean-css‘);functioncssMinifier(flieIn, fileOut) {     varflieIn=Array.isArray(flieIn)? flieIn : [flieIn];     varorigCode,finalCode=‘‘;     for(vari=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‘);//图片varimgMinifier = 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

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.