The compression of JavaScript code files is mentioned in the previous article (http://blog.csdn.net/u010487568/article/details/19701575), generally there are three ways:
- Compress only whitespace, comments and other characters (the most basic method)
- Compress whitespace, comment, and replace variable name
- Compress horror, annotate, replace variable names, and minimize all words in a file
recently learning more about the shell has made it more efficient and convenient for this ancient tool, thus implementing the most basic method of Shell version for this topic. The main strategies are as follows:
- Remove a single line comment
- Remove line breaks and tabs
- Compress multiple spaces for a single
- Remove multi-line dwelling content
- "(", ")", "{", "}", "; ",": "," = "characters on both sides of the space to remove
The specific implementation is as follows:
##!/usr/bin######################################################### #Filename: Compreee-js.sh#author : Oshyn Song#time : 2014-8-19#desc : Compress the JavaScript file by basic method##################################### ################## #if [$#-ne 1];then echo "Usage:sh $ js-filename"; Exit;fijsfile=$1;echo "Compress $jsfile start ..."; Cat-s $jsfile | Sed ' s!//.*$!! G ' | Tr-d ' \n\t ' | Tr-s ' | Sed ' s!/\*.*\*/!! G ' | Sed ' s! \?\ ([+=\!&%$\*\|><{} ();,:]\) \?! \1!g ' > "Compress.${jsfile}"; echo "process finished.";
The test is as follows:native Code
;(function () {function __ (ID) {return document.getElementById (ID);} function Ajax (options) {if (typeof XMLHttpRequest = = ' undefined ') {XMLHttpRequest = function () {return new ActiveXObject ( Navigator.userAgent.indexOf (' MSIE 5 ') >= 0? ' Microsoft.XMLHTTP ': ' Msxml2.xmlhttp ');};} ..........
Post-Compression code:
;(function () {function __ (ID) {return document.getElementById (ID);} function Ajax (options) {if (typeof xmlhttprequest== ' undefined ') {xmlhttprequest=function () {return new ActiveXObject (Navigator.userAgent.indexOf (' MSIE 5 ') >=0? ' Microsoft.XMLHTTP ': ' MSXML2. XMLHTTP ');};} .......