Why doesnt the MVC4 bundling bundle Knockout.js?

4.7k views Asked by At

I have downloaded MVC4 and trying to work out how the bundling feature works in a standard project. It would seem that having the bundle:

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

Brings back all the jquery.js files but not the knockout.js files in the included scripts. Why is this? And also what is the _references.js file about and why is the knockout.js file commented out?

3

There are 3 answers

0
tpeczek On BEST ANSWER

If you look into your project Global.asax file, you should find there something like this:

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

Now the RegisterTemplateBundles is registering only a predefined subset of scripts:

  • jquery-*
  • jquery.mobile*
  • jquery-ui*
  • jquery.unobtrusive*
  • jquery.validate*
  • MicrosoftAjax.js
  • MicrosoftMvc.js
  • modernizr*
  • AjaxLogin.js

If you want some additional files you could either change RegisterTemplateBundles to EnableDefaultBundles:

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

Or create your own custom bundle (you can read more about bundling and minification here). You should also know that EnableDefaultBundles has some performance impact.

The _references.js file is used by Visual Studio for JavaScript intellisense. You can learn more from following article:

0
RickAndMSFT On

tpeczek is correct. Most folks don't use Knockout.js, that's why it's not included by default. Bundling/Minification (BM) has changed considerably for RC and it will be much simpler to add files to bundles. See my Bundling and Minification tutorial

0
Sean Lynch On

I was able to add a file to the bundle with the following line of code added after the RegisterTemplateBundles()

BundleTable.Bundles.RegisterTemplateBundles();
BundleTable.Bundles.Where(x => x.Path == "~/Scripts/js").First().AddFile("~/scripts/knockout-2.0.0.js");