문제

I've downloaded Yahoo JavaScript Uglify (yuglify) from https://github.com/yui/yuglify and I intent to use it for JS and CSS compression on some PHP projects hosted on a apache server. Tried to minify a file with these original contents:

var _cmFrameList = new Array ();    // a pool of reusable iframes
var _cmFrameListSize = 0;           // keep track of the actual size
var _cmFrameIDCount = 0;            // keep track of the frame id
var _cmFrameMasking = true;         // use the frame masking

// disable iframe masking for IE7
/*@cc_on
    @if (@_jscript_version >= 5.6)
        if (_cmFrameMasking)
        {
            var v = navigator.appVersion;
            var i = v.indexOf ("MSIE ");
            if (i >= 0)
            {
                if (parseInt (navigator.appVersion.substring (i + 5)) >= 7)
                    _cmFrameMasking = false;
            }
        }
    @end
@*/

var _cmClicked = false;             // for onClick

But got this instead:

var _cmFrameList=new Array,_cmFrameListSize=0,_cmFrameIDCount=0,_cmFrameMasking=!0,_cmClicked=!1

In the contents of the original file, there's some conditional compilation code that's needed to work on older IE browsers (commented lines starting with @cc_on, @if, @end, etc). It seems that yuglify is treating these conditional compilation lines as simple comments, and as such, is removing all of it.

By curiosity, I've tested yuicompressor on this same file too ( https://github.com/yui/yuicompressor/downloads ), and it seems that all conditional compilation code is intact. Even the linebreaks are still there:

var _cmFrameList=new Array();var _cmFrameListSize=0;var _cmFrameIDCount=0;var _cmFrameMasking=true;
/*@cc_on
    @if (@_jscript_version >= 5.6)
        if (_cmFrameMasking)
        {
            var v = navigator.appVersion;
            var i = v.indexOf ("MSIE ");
            if (i >= 0)
            {
                if (parseInt (navigator.appVersion.substring (i + 5)) >= 7)
                    _cmFrameMasking = false;
            }
        }
    @end
@*/
var _cmClicked=false;

Older IE browsers gets garbled without those conditional compilation lines. Is it possible to use yuglify without removing it?

도움이 되었습니까?

해결책

Found a way to solve it, by using a little workaround with eval command.

Firtly, I created these global variables:

var is_IE_browser = eval('/*@cc_on !@*/false');
var jscript_version = (is_IE_browser) ? eval("/*@cc_on @_jscript_version @*/") : (0);
var IE_version = (is_IE_browser) ? (get_IE_version()) : (0);

And then, I created this function:

function get_IE_version(){
    var IE_version;
    if(jscript_version == 5.6 || (jscript_version == 5.7 && navigator.userAgent.toLowerCase().indexOf("msie 6.") != -1)) {
        IE_version = 6;
    } else if(jscript_version == 5.7){
        IE_version = 7;
    } else if(jscript_version == 5.8){
        IE_version = 8;
    } else if(jscript_version == 9){
        IE_version = 9;
    } else if(jscript_version == 10){
        IE_version = 10;
    } else {
        IE_version = 0;
    }
    return IE_version;
}

By doing this, now I can replace all Conditional Compilation code (cc_on) with a few conditions using the variables above.

The variable "is_IE_browser" will return true if browser is IE, and false if not.

The variable "IE_version" will return IE version number if browser is IE, and zero if not.

Using this code, I can detect reliably most IE browsers (except IE6, that needs a little search on browser user agent data, and it's vulnerable to browser sniffing, but I can live with it).

This way, I can detect older browsers fine, and continue using yuglify without having my JS file damaged.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top