Shell Text Processing--the most basic way to compress JS files

Source: Internet
Author: User

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 ');};} .......

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.