开发者

Remove newlines from a JSMin minified file and parsing error on IF condition

I'm trying to minify on the fly a javascript file with php and JSMin. Everything works fine, but when I try to remove newlines

$jsMinifiedClean = str_replace( array("\r","\n"),"",$jsMinified);

I get a javascript parse error. After a quick search in the code I found the problem:

if( condition ) stat开发者_如何学运维ement
else statement;

The "if" condition doesn't have the " ; " at the end of the line.

Why with a manual YUI compression of the code I get no error? Is the semicolon really mandatory at the end of the if to avoid the problem in compressing with JSMin?


Is the semicolon really mandatory at the end of the if to avoid the problem in compressing with JSMin?

It is if you remove the newline. By default, JSMin does not remove the newline for exactly this reason, the horror that is automatic semicolon insertion. From the JSMin page:

It is more conservative in omitting linefeeds, because linefeeds are sometimes treated as semicolons. A linefeed is not omitted if it precedes a non-ASCII character or an ASCII letter or digit or one of these characters:

\ $ _ { [ ( + -

and if it follows a non-ASCII character or an ASCII letter or digit or one of these characters:

\ $ _ } ] ) + - " '

If you then remove the newline, you're taking it upon yourself to be sure that the newline isn't significant because of ASI.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜