Js compression tool yuicompressor tutorial _ javascript skills

Source: Internet
Author: User
Yuicompressor is written in pure Java and needs JDK or later to analyze and cut the JavaScript source file based on Rhino. Js and css compression tool yuicompressor 2.46
Introduction
Http://developer.yahoo.com/yui/compressor/
JS and css compression tools yuicompressor
Latest Version: 2.4.2
Run the need to install JDK first, official: http://www.oracle.com/technetwork/java/javase/downloads/jdk-7u1-download-513651.html according to your system to choose the appropriate version, under normal circumstances 586 that can be.
Yuicompressor is written in pure Java and needs JDK or later to analyze and cut the JavaScript source file based on Rhino. You can remove redundant spaces (spaces, line breaks, and tabs) in JavaScript and Css files. You can also confuse JavaScript files and change the name of local variables, change them to 1, 2, or 3 characters. In short, try to be as short as possible. For CSS, the representation of an optimized 0-value attribute value is also used to compress the file by optimizing the color value.

Tutorial
The following describes how yuicompressor is called using the command line method.
Command Line usage
Syntax: java-jar yuicompressor-x.y.z.jar [options] [input file]
Common parameters:
-H, -- help: displays help information
-- Type Specifies the file type of the input file.
-- Charset Encoding used to read the input file
-- Line-break Insert a line-bread symbol after the specified Column
-V, -- verbose displays info and warn Information
-O Specify the output file. The default output is the console.

Special JavaScript parameters:
-- Nomunge only compresses data and does not confuse local variables.
-- Preserve-semi retains all semicolons.
-- Disable-optimizations optimization is prohibited.

Example: java-jar yuicompressor-2.4.2.jar-o d:/temp/hello_min.js hello. js
The following article is written by another user. For more information, see.
Today, I wrote a lot of JS Code for my website. Looking at more and more code, I couldn't help but feel a little flustered. As a result, my website will be very slow. I suddenly remembered that my pony once said that there was a JS compression tool, then I asked Master A, who was enthusiastic about passing on the tool to me and taught me how to use it. It was really good to try it.
This is Yahoo's yuicompressor, which is very convenient and can be used as follows:

Installation steps:

1. Decompress jsminifier.rar to X: \ jsMinifier and add X: \ jsMinifier to the System PATH Environment Variable (if PATH is not added, You can manually modify the Registry file, which is the full PATH.

2. Rename X: \ jsMinifier. tpl. reg to jsMinifier. reg, and cssMinifier. tpl. reg to cssMinifier. reg. You can modify the configuration by yourself (see 1 ).

3. Run regedt32 to open the Registry Editor and import jsMinifier. reg and cssMinifier. reg to the Registry.


Test and install:

1. Right-click X: \ jsMinifier \ test. source. js and execute the "compressed JavaScript" menu. The system automatically generates the X: \ jsMinifier \ test. js file. If you perform another compression on the test. js file, the system will generate the test-min.js file, the content remains unchanged, This is the default file name for non-source. js File compression.

2. the CSS compression test is similar to 1.


Note: This tool can also compress CSS files.
For example

A file with-min after the file name is generated in the same directory.

If you need to compress files in batches, you can refer to the following script
Share the batch processing script in windows (copy it to notepad, save it as bat, and modify the corresponding path to apply it ):

The Code is as follows:


@ Echo off
: Set the YUI Compressor startup directory
Set yuifolder = C:
: Set Your JS and CSS root directories. The script will automatically search for and compress all JS and CSS layers by tree.
Set jsfolder = D: \ WebJsAndCss
Echo is searching for JavaScript, CSS...
Chdir/d % JSFOLDER %
For/r. % a in (*. js *. css) do (
@ Echo compressing % ~ A...
@ Java-jar % YUIFOLDER % \ yuicompressor-2.4.6.jar -- charset UTF-8 % ~ Fa-o % ~ Fa
)
Echo complete!
Pause & exi

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.