jQuery Script Map Causing Critical Error in jQuery 1.9.0

Script maps are a great addition to the debugging arsenal of any script developer. Even for those of us who are “consumers” of some of the libraries out there, the script maps will help us get at the details of why something is going sideways when we’re working with minified versions of the code. 

Without the map, it has been difficult to find the correlating code in the original file. Elijah Manor does a great job at going over how to take advantage of script map support and it’s benefits in jQuery 1.9.0.

Minification in MVC4 and Critical Errors from the jQuery Script Map

There is a chance that you’ll run into this if you’re dropping code in Visual Studio 2012 on Win 8 with Asp.Net MVC, using bundling and minification:

JavaScript critical error at line 1, column 11 in http://localhost:0000Scripts/jquery-1.9.0.min.map

SCRIPT1004: Expected ‘;’

If this is the case, check to see that your wildcard isn’t to greedy. For example, if you have something like this set up in your BundleConfig.cs:

    bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                "~/Scripts/jquery-1.*"));

…you’re telling the bundling engine to grab anything matching the script “jquery-1.anything”, and as a great optimization, the MVC Framework is smart enough to grab only one of library.js or library.min.js, and it prefers the min.js for you.  The downside here is that the bundle builder is using a prefix match here, and it seems to be picking up the .map file as well. This seems to be inadvertently causing the critical error mentioned above (map files aren’t to be shipped to the client as part of the request).

The Fix is In

The good thing is that you’re not stuck on wildcards, and it’s likely better to be using the semantic versioning replacement token (especially with the jQuery libraries).  Update your bundle to the following:

    bundles.Add(new ScriptBundle(“~/bundles/jquery”).Include(
“~/Scripts/jquery-{version}.js”));

The {version} token is a replacement that does a best match against the file according to SemVer.  Now, instead of all the files getting added, and the risk of files being added multiple times, we get a smarter match on the library we are requesting for bundling and minification.