1

I have an MVC bundle where one of the files is simply not being included in the output, even though it can be found properly. I am familiar with all the standard bundling rules (min vs no min, .debug vs .js, etc), but this appears to have something to do with the contents of the file.

Edit: Configuration (should have included this to start with)

  • "Microsoft.AspNet.Web.Optimization v1.0
  • WebGrease v1.1
  • MVC Stuff v4.0.20710.0

After some playing, I discovered that if I modify the file a little bit, it will be included in the output. If undo the change, then the entire file is simply not included in the output. Not garbled, corrupted, etc, simply not there.

The bundle looks like this:

bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
    "~/Scripts/jquery-{version}.js",
    "~/Scripts/jquery.unobtrusive*",
    ...
    "~/Scripts/underscore/underscore.js",
    "~/Scripts/underscore/underscore.string.js"
));

The problem is in underscore.string.min.js. This file starts out like this (it is 1 line):

!function (e, t) { "use strict"; var n = t.prototype.trim, //...

I have found that if I make this change hen the undescore.string.min.js file is included properly. Some changes, such as adding a comment, do not work. Adding a call to console.log(""); as the first line works too, but the actual call to console.log() is ¿removed?

;
!function (e, t) { "use strict"; .... 
  • Placing the ; on the first line doesn't work (;!function (e, t) {)
  • Deleting the .min.js file and letting ASP.NET MVC works fine
  • What is that ! there for anyway!

The file before it, underscore.min.js, looks like this:

//     Underscore may be freely distributed under the MIT license.
(function(){/* lots of code */}).call(this);
//# sourceMappingURL=underscore-min.m

And the final bundled minified output looks like this:

function"==typeof define&&define.amd&&define("underscore",[],function(){return n})}
.call(this),!function(n,t){"use strict";var a=t.prototype.trim,s=t.prototype.trimRight

This has all made me a little nervous to use any provided .min.js files, since I am not quite sure what will fail and when. If it does fail, there is no notice or warning anywhere I can find that tells me something went wrong processing a particular file (e.g. it resulted in no output?). Am I better off just abandoning pre-minified assets?

Andrew
  • 8,322
  • 2
  • 47
  • 70
  • 2
    for the exclamation mark, have a look at [this question](http://stackoverflow.com/questions/3755606/what-does-the-exclamation-mark-do-before-the-function) – Daniel J.G. Oct 20 '14 at 09:07
  • I get the `!` now, gracias. I thought there might be more to it than that... – Andrew Oct 20 '14 at 09:18
  • 3
    I wonder why the bundling process breaks with that pattern. Maybe someone can shed some light... – Daniel J.G. Oct 20 '14 at 09:29
  • I will see if I can make a simpler project, and try to reduce the problem. Maybe it's a more complicated explanation – Andrew Oct 20 '14 at 09:45
  • 1
    In a new MVC5 application, creating a bundle with jquery, jquery.unobtrusive, underscore 1.5.1 and the latest underscore.string.min.js seems to work fine. The project is using Microsoft.AspNet.Web.Optimization 1.1.3 and WebGrease 1.5.2 – Daniel J.G. Oct 20 '14 at 10:27
  • Unfortunately this is a v4 app. I am also having issues with knockout.js now. It seems to include the pre-minified file (not the `debug.js` one), but then proceeds to somehow **break** it. – Andrew Oct 20 '14 at 10:46
  • @DanielJ.G., can you post that as an answer, the suggestion to upgrade `Optimization`? That seems to have fixed it for me. I also had to updgrade `WebGrease` but that was painless as far as I can tell. – Andrew Oct 20 '14 at 10:58

1 Answers1

1

This seems to work with the latest versions of Microsoft.AspNet.Web.Optimization and WebGrease:

  • Microsoft.AspNet.Web.Optimization 1.1.3
  • WebGrease 1.5.2

With those versions, creating a bundle as follows works fine:

bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
    "~/Scripts/jquery-{version}.js",
    "~/Scripts/jquery.unobtrusive*",
    "~/Scripts/underscore/underscore.js",
    "~/Scripts/underscore/underscore.string.min.js"
));

While this doesn't explain the issue, upgrading to the latest versions will fix it.

Daniel J.G.
  • 34,266
  • 9
  • 112
  • 112
  • Certainly fixed it for me. Since the fix doesn't involve updating any other part of MVC, it is nice and safe. Also, my problem with knockout was that the minified js was wrong. Maybe nuget only updated the contents of one file, or some other error happened. Put it under bad luck and move on. – Andrew Oct 20 '14 at 12:06