Javascript minifying

I’ve just been integrating the MVC ScriptManager for combining scripts and minifying them for performance and I came across an oddity with the minifier and javascript.  The problem is caused by the following snippet of code,

cssHandler : {
    position: "relative",
    /*top:"2px",*/
    right:"-3px",
    float:"right",
    /*width:"0px",*/
    borderRight:"1px solid #fff",
    borderLeft:"1px solid #fff",
    height:"20px",
    cursor:"col-resize"
    },

The float is a reserved word and the minifier choked on it.  Technically the code works so I dug a little deeper.  The minifier being used was the .net version of the YUI Compressor and after a dig into the source I found the reservedKeywordAsIdentifier property.  If I tweak the JavaScriptCompressor.Parse function to call setReservedKeywordAsIdentifier(true) on the compilerEnvirons object before the Parser object is created it compresses the javascript correctly.

A search for the expected behaviour of reserved words as identifiers turned up this article from 2002 by Douglas Crockford.  In it he suggests that javascript has a lame restriction that means identifiers are not allowed as member names (I’m paraphrasing).  That partially explains the way the library is setup. At least some versions of javascript wouldn’t like that bit of code.

The actual javascript parsing stuff appears to come from the Mozilla Rhino project.  The one thing I can’t figure out is how I’m supposed to configure the parser correctly for a particular version of javascript.

On balance though I figure the simplest solution is to simply change the original code so that I use a string rather than a bare word and that will bypass the whole set of problems.

Anyway, if you come across the Exception ‘System.InvalidOperationException: invalid property id’ when using the compressor with the MVC ScriptManager that might be the problem.  The rest of the stack trace starts like this,

  at Yahoo.Yui.Compressor.CustomErrorReporter.Error(String message, String sourceName, Int32 line, String lineSource, Int32 lineOffset)
   at EcmaScript.NET.Parser.AddError(String messageId)
   at EcmaScript.NET.Parser.ReportError(String messageId)
   at EcmaScript.NET.Parser.primaryExpr()

I’m hoping that the next time I come across this problem I’ll find my own post with google!

About these ads
Tagged

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 58 other followers