A handy script for compressing JavaScript and CSS with PHP

Source: Internet
Author: User

Young40 blog Source: http://www.cnblogs.com/young40/archive/2012/03/22/2411239.html

 

The number of HTTP connections to be opened on the same web page has a great impact on the speed of the website and the pressure on the server, therefore, we need to adopt a variety of methods to minimize HTTP connections while ensuring functions. for example, you can combine several small JS files and CSS files into one file, and then compress them. This reduces the number of HTTP connections while reducing the amount of data transmitted.

SAE does not support disk writing. If you want to use this script, you need to run it locally to compressCodeUpload through SVN.
In the future, I will continue to improve and eventually support online output of compressed JS files.

In fact, when I am going to debug locally, it is generated every time I open the website page (or at a certain probability. local debugging and calling of the original file compilation check errors, online applications call compressed JS/CSS

This time, I did it in a hurry and did not add a detailed test. Compression and CSS merging were not implemented/tested at all.
Although I hope you can use this script more, I still want to remind you that you need to test more.
In addition, I hope you can find more bugs and give more comments.

What is the effect?
In addition to the scripts I wrote for testing, I mainly used jquery's uncompressed version for testing.
For example, jquery-1.7.1.js 248,235 bytes (250 kb on disk)
After compression, 158,214 bytes (160 KB On disk) are compressed to nearly 100 kb.
Of course, the official jquery compression version is 93,868 bytes (94 KB On disk ).ProgramThere is still much room for improvement.
You may wonder why the compressed version is nearly 70 KB larger than the official version? This is because jquery official Compression not only removes comments, but also uses obfuscation.
For example
Function Test (number_one, number_two) {return number_one + number_two;} Will be obfuscated
Function Test (a, B) {return a + B ;}
We can see that the obfuscated function is only half the length of the original function.
Of course, this is a very easy-to-understand example. If it is a huge function or class, it will be difficult to understand after it is obfuscated, and obfuscation functions will be considered in the future.

I originally wanted to use regular expressions to analyze JS files. Unfortunately, regular expressions are insufficient.
For example, I cannot use regular expressions to differentiate the following. I hope you will discuss it.

/**
I am a multiline comment
*
*/

VaRSTR = '/** I am not a comment, I am a string **/';

So I used a stupid method to implement it. The syntax analysis token method is not as elegant as regular expressions.
Directly paste the operation code for usage and analysis.

Call method.

$ MC=NewMincode ();

$ MC-> Turncleanentersoff ()//
-> Turncleanspaceoff ()
-> AddFile ($ Base. '/JS/ajaxupload. js ')
-> AddFile ($ Base. 'Js/jquery-1.7.1.js ')
-> AddFile ($ Base. '/JS/U. js ')
-> AddFile ($ Base. 'Js/u2.js ')
-> AddFile ($ Base. 'Js/u3.js ')
-> Save ($ Base. './JS/J. Min. js ');

Core Analysis Code:


 $ Content = File_get_contents ($ File );

$ Token_one = False ; // Single quotes
$ Token_two = False ; // Double quotation marks
$ Token_pattern = False ; // Encountered Regular Expressions
$ Token_comment_quick = False ; // Encountered // such Annotation
$ Token_comment_big = False ; // Encountered/***/Such Annotation

$ Rs = '';
$ Normal =True ;
$ Skip = False ;
$ Skipped = "";

$ Length = Strlen ( $ Content );

For ( $ I = 0; $ I < $ Length ; $ I ++)
{
$ Skip = False ;

$ C = $ Content [ $ I ];
$ P = Isset ( $ Content [ $ I -1])? $ Content [ $ I -1]: False ;
$ N = Isset ( $ Content [ $ I + 1])? $ Content [ $ I + 1]: False ;

If ( $ Normal | $ Token_pattern )
{
If ( $ Normal && $ C = '/'&& $ N ! = False && $ N ! = '/'&& $ N ! = '*') // The first time I met a/and it was not // and /*
{
$ Token_pattern = True ;
$ Normal = False ;
}
Elseif ( $ Token_pattern && $ C = '/'&& $ P ! = False && $ P ! = "\\") // The second time/is encountered and is not escaped/That is not \/
{
$ Token_pattern = False ;
$ Normal = True ;
}
Elseif ( $ Token_pattern && In_array ( $ C , Array ("\ R", "\ n", "\ n \ r "))) // This line is over, but the regular expression is not over. The javascript code may be faulty.
{
$ Token_pattern = False ;
$ Normal = True ;
}
}

If ( $ Normal | $ Token_one )
{
If ( $ Normal && $ C = "'") // String start
{
$ Token_one = True ;
$ Normal = False ;
}
Elseif ( $ Token_one && $ C = "'") // String end
{
$ Token_one = False ;
$ Normal = True ;
}
}

If ( $ Normal | $ Token_two )
{
If ( $ Normal && $ C = '"') // String start
{
$ Token_two = True ;
$ Normal = False ;
}
Elseif ( $ Token_two && $ C = '"') // String end
{
$ Token_two =False ;
$ Normal = True ;
}
}

If ( $ Normal | $ Token_comment_big )
{
If ( $ Normal && $ C = '/'&& $ N ! =False && $ N = '*') // Encountered /*
{
$ Token_comment_big = True ;
$ Normal = False ;
}
If ( $ Token_comment_big && $ C = '/'&& $ P ! = False && $ P = '*') // */
{
$ Token_comment_big = False ;
$ Normal = True ;
$ Skip = True ;
}
}

If ( $ Normal | $ Token_comment_quick )
{
If ( $ Normal && $ C = '/'&& $ N ! = False && $ N = '/') // Encountered // Annotation
{
$ Token_comment_quick = True ;
$ Normal = False ;
}
If ( $ Token_comment_quick && In_array ( $ C , Array ("\ R", "\ n", "\ n \ r "))) // When you press enter, the comment on a single line ends.
{
$ Token_comment_quick = False ;
$ Normal = True ;
}
}

If ( $ Normal | $ Token_pattern | $ Token_one | $ Token_two )
{
If (! $ Skip )
{
$ Rs . = $ C ;

If ( In_array ( $ C , Array ("\ R", "\ n", "\ n \ r "))&&
! In_array ( $ Skipped [Strlen ( $ Skipped )-1], Array ("\ R", "\ n", "\ n \ r ")))
{
$ Skipped . = $ C ;
}
}
Else
{
$ Skipped . = $ C ;
}
}
Else
{
$ Skipped . = $ C ;
}
}

If ( $ This -> _ Cleanenters)
{
// The following characters in the string will be damaged. The next version will be repaired.
$ Rs = Str_replace ( Array ("\ R", "\ n \ r", "\ n", "\ t "),'', $ Rs );
}

If ( $ This -> _ Cleanspace)
{
// It will destroy multiple consecutive spaces in the string, and the next version will be repaired.
For ( $ I = 0; $ I <1, 10000; $ I ++)
{
If (! Strstr ( $ Rs ,''))
{
Break ;
}

$ Rs = Str_replace ('','', $ Rs );
}
}

Return $ Rs ;


I did not write a blog for a few days and found that no attachments will be inserted.

if you want to download the attachment, please move to the http://cloudbbs.org/forum.php? MoD = viewthread & tid = Floor 2, 684

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.