trembon
trembon

Reputation: 768

jquery not included in bundling

I am confused over a little problem i have with jquery in my project. The project is a MVC4 site in visual studio 11.

The problem is that jQuery is not included in the bundling, and it seems to been working before i updated to 1.7.2, but can't be sure.

I can see the comment of the jQuery file on top of the bundled file, but cant find any code of it. All other files seems to be included though (my own scripts and jquery ui files).

I have tried bundle filters and create my own bundle, but nothing seems to work.

I really would appreciate any help, as when the page load, all java scripts just crash because a function is not found. This does work if i just include the java script files normally.

Edit:

Global.asax:

protected void Application_Start()
{
    BundleTable.Bundles.EnableDefaultBundles();
}

_Layout.cshtml:

<script src="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/Scripts/js")"></script>

Update 1:

I added some code the the jQuery file, and the bundled file contained this at the top.

function Test(){ }

Then changed to this, and the code was not there this time.

(function Test(){  })

The wierd thing is that the other jQuery files, like the UI ones doesnt get removed, and they being the same way.

Edit: seems like this is intended

Update 2:

Tested some more and found out which file that breaks, if i include any of these files in my custom bundle, it breaks

Before them i have these, and they work without any error

It is still wierd, if i include these file as normal in the script with <script src="~/Scripts/jquery.validate.js"></script> they work as they should, without errors.

Though, if i bundle the working files, and then add the non-working one after as normally, they still breaks

_Layout.cshtml:

<script src="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/myBundle")"></script>
<script src="~/Scripts/jquery.validate.js"></script>

Global.asax:

var bundle = new Bundle("~/js", new JsMinify());
bundle.AddFile("~/Scripts/jquery-1.7.2.min.js");
bundle.AddFile("~/Scripts/jquery-ui-1.8.19.min.js");
bundle.AddFile("~/Scripts/bootstrap.min.js");
BundleTable.Bundles.Add(bundle);

Edit: seems like something is changed in the bundled jquery files, that makes the validate plugins to break

Work Around

I tried to add the old files, before the update, and it work! Managed to find out that it was the new jQuery UI 1.8.19 from NuGet that crashed.

I downloaded the jquery ui directly from their site, and used that one instead, and it worked!

So it seems that something in the jquery ui nuget version is changed that the bundling is picking up and changes/removes it, so it breaks..

which part to blame? jQuery UI NuGet packages or mvc4 bundling? And is there a solution to the bundling problem?

Upvotes: 5

Views: 7972

Answers (2)

trembon
trembon

Reputation: 768

Work Around

I tried to add the old files, before the update, and it work! Managed to find out that it was the new jQuery UI 1.8.19 from NuGet that crashed.

I downloaded the jquery ui directly from their site, and used that one instead, and it worked!

So it seems that something in the jquery ui nuget version is changed that the bundling is picking up and changes/removes it, so it breaks..

which part to blame? jQuery UI NuGet packages or mvc4 bundling? And is there a solution to the bundling problem?

Upvotes: 3

Steve Hobbs
Steve Hobbs

Reputation: 3326

I think EnableDefaultBundles() tries to include a specific version of jQuery, so if you're saying you've upgraded it (and presumably removed the old version) I would hazard a guess and say that MVC is trying to bundle a file which doesn't exist any longer.

You would have to ditch the use of EnableDefaultBundles(), or at least compile an additional bundle and include this as well.

You say you've tried creating your own, but could you try again? Maybe the following would help:

Bundle myJSBundle = new Bundle("~/MyJSBundle", typeof(JsMinify));
myJSBundle.AddDirectory("~/Scripts", "*.js", false);
BundleTable.Bundles.Add(myJSBundle);

Obviously change the paths and whatnot to match your own project. Then include it on the page like:

<script src="@Url.Content("~/MyJSBundle")" type="text/javascript"></script>

Source: http://www.dotnetexpertguide.com/2011/12/bundling-and-minification-aspnet-mvc4.html

Upvotes: 0

Related Questions