JS compression uglify (2)

Source: Internet
Author: User
Tags install node

First, the story always has its background

The end of the year will come, a lot of leisurely time, so brush micro bo, contact with a variety of information-the United States its name "learning." Luck is good, met a term,uglifyjs. is said to be used to compress JS files, is said to be able to optimize JS, is said to be based on node, is also said to be more than Google Closure compiler sense, oh? Aroused my curiosity. Hundred of the Valley, yo, related to the introduction of a lot. And then toss for an hour, basically know what's going on.

I think, or some use, even if it is not engaged in node development, smoking a small space, share, so there is this article. People always have impetuous time, I am no exception, UGLIFYJS internal deep operation is how, alas, I am feebly, do not want to toss, so, this article more is stay at application level. Well, it's a lot more popular. After all, the people who like to see "Tae-so" are more than the "Master of the Generation".

Second, node-related fixed processes

Mentioning the node application in all things is like the following process:

    1. Install node
    2. Installing the NMPNPM
    3. Install current app –uglifyjs

The first two online too many motioned, I have also said before, sorry, I am very lazy.
UGLIFYJS Install the following code:

NPM Install Uglify-js-g

A carriage return, and then snapped, there is something to show:

So, the following can be compressed JS.

Third, Uglifyjs JS compression

UGLIFYJS compression has a number of parameters, see its GitHub hosting project. There are some optional parameters, as follows:

What beautify compression, how long the space, variable name becomes short name, etc... You are interested to see for yourself, if today is not Friday, if not to go back to buy food, I will translate the next-you, please forgive me, Amen!

The following shows how to compress JS using UGLIFYJS.

I have a folder called Formini on my desktop, I will put the files to be compressed, and then compress. Now, I copied a JS file called Inet.js into this folder from the inside, and then:

    1. Open cmd, this is not going to be my choice.
    2. Directory lead to Formini folder, CD and then what, this is not going to be my way.
    3. The following code:
      Uglifyjs Inet.js-o Inet-min.js
    4. Then run the following code to test the -m optional parameters:
      Uglifyjs inet.js-m-O inet.min.js

      As follows:

Oh, now is the result, see the following:

90K is not running -m parameters, 70K (69.4) is running. -mparameter So it turns the variable name intoa, b, c, d, ...

From the practical point of view, JS on-line obviously to compress variables, reduce certain file size.

Compared with yuicompressor, the size is only small 0.6K, but the online reflection uglifyjs compared to Yui compression is obvious, I here is only the size optimization 0.6÷174=0.345% , this can only show that I write the code is relatively optimized, haha (suddenly think of the wife, do not have to take a deep breath ...). Deep breath...... )!

Four, UGLIFYJS compression batch processing

We can not open the cmd every time to type compression code execution, easy to write wrong (such as the above chart), but also time-consuming, think will make people crazy. Lazy people have lazy method, took a bit of kung fu, tossing a batch of files, later, want to compress JS, just double-click to run this .bat file on it! The complete code is as follows:

@echo off:: Set the root directory of the compressed JS file, the script will automatically find and compress all jsset by tree level Jsfolder=c:\users\administrator\desktop\forminiecho is looking for JS file chdir/ D%jsfolder%for/r. %%a in (*.js) do (    @echo compressing%%~a    ... Uglifyjs%%~fa  -m-o%%~fa) echo Complete!pause & exit

Create a new TXT document, any name, paste the above code, modify the JSFOLDER following folder directory for your own, and then change the .txt suffix .bat is ok! Then double-click to use the UGLIFYJS compression js file in bulk!

Iv. concluding remarks

For yourself, the content of this article counts as a memo. For now, I'm still uglifyjs can't compress a CSS file. But it's always helpful to know. In haste, if there is any inaccuracy in the text, please correct it.

Original article, reprint please indicate from Zhang Xin Xu-Xin space-Xin Life [http://www.zhangxinxu.com]
This address: http://www.zhangxinxu.com/wordpress/?p=2946

JS compression uglify (2)

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.