0

for a bigger project which is a single website application I use very much of jquery, js and ajax calls.

As I would do in C# and ASP.Net of course I tried to split up functions / variables into a bunch of js-files for categories e.g. 1 file for my "Targets", 1 file for my "Commissions" (which are parts of my application)...

Now I nearly done with my work here and I ended up with around 20 to 25 js files, which I load one after antother in my default.aspx...

Is that the really correct (and fastest) way to handle this?

codea
  • 1,439
  • 1
  • 17
  • 31
ReneK
  • 109
  • 1
  • 9

3 Answers3

1

Bundling and minification is actually a new feature of ASP.Net 4.5

You can see it by creating a new ASP.Net MVC 4 web application using visual studio and you use the Web Application template, it creates a Bundle Config Class, that probably does what you want:

public class BundleConfig
{
    // For more information on Bundling, visit http://go.microsoft.com/fwlink/?LinkId=254725
    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-{version}.js"));

        bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
                    "~/Scripts/jquery-ui-{version}.js"));

        bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.unobtrusive*",
                    "~/Scripts/jquery.validate*"));

        // Use the development version of Modernizr to develop with and learn from. Then, when you're
        // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
        bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                    "~/Scripts/modernizr-*"));

        bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));

        bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
                    "~/Content/themes/base/jquery.ui.core.css",
                    "~/Content/themes/base/jquery.ui.resizable.css",
                    "~/Content/themes/base/jquery.ui.selectable.css",
                    "..."

    }
}

This bundle config class must be called on the Application_Start() method:

BundleConfig.RegisterBundles(BundleTable.Bundles);
codea
  • 1,439
  • 1
  • 17
  • 31
0

There are various tools that can come in useful for this. I would recommend using GruntJS http://gruntjs.com/ which has a plethora of tools/scripts ready to implement and take some of the grunt work out of deploying production-ready code as you are trying to do.

https://www.erianna.com/using-grunt-to-minify-and-compress-assets

A rather more hacky solution in the short-term is to use ySlow on firebug which via the tools` tab allows you to minify all javascript on a given page, output as text. You can then save that as one js file and then only reference that in your page which should suffice for testing purposes and if you JS codebase isn't going to change.

Rob Schmuecker
  • 8,934
  • 2
  • 18
  • 34
0

You can configure bundling in ASP.NET (depending on the version you are using MVC 4 or webforms with asp.net 4.5+). As already suggested, you have a class with the following static method:

 public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-{version}.js"));

        ...
        //your custom files
        bundles.Add(new ScriptBundle("~/bundles/customjs").Include(
                    "~/Scripts/js/file1.js",
                    "~/Scripts/js/file2.js"));
    }

You can then add them to your MVC page:

@Scripts.Render("~/bundles/customjs")

I think web forms is something like:

<%: Scripts.Render("~/bundles/customjs") %>

You can create custom bundles and group common files (maybe per section), so the page will only load these files. Either way, it should be a one liner on your page instead of copy & pasting the links on each page.

There is a caveat, if you've already minimised, I seem to remember the file won't be loaded.

However, if you are using an older version of ASP.NET, and you are looking to minify, then you can also use Web Essentials to minify each file.

Christian Phillips
  • 18,399
  • 8
  • 53
  • 82