Bundles


Creating fixed order Bundle

For some css and javascript it is important that the script comes in a certian order because of dependecies. In order to do this you may use the folloing code example to fix the order of the Bundle components:

using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Optimization;

namespace MyApplicationSite
{
    public class BundleConfig
    {
        public static void RegisterBundles(BundleCollection bundles)
        {
            var bundleScripts = new ScriptBundle("~/bundles/js");
            var bundleStyles = new StyleBundle("~/bundles/css");

            bundleScripts.Orderer = new NonOrderingBundleOrderer();
            bundleStyles.Orderer = new NonOrderingBundleOrderer();

            bundleScripts.Include(
                "~/Content/js/libs/jquery.js",
                "~/Content/js/libs/jquery-ui-1.10.4.custom.js",
                "~/Content/js/modernizr.js",
                "~/Content/js/gumby.js",
                "~/content/js/common.js"
            );

            bundleStyles.Include(
               "~/Content/css/gumby.css",
               "~/Content/css/jquery-ui-1.10.4.custom.css",
               "~/Content/css/style.css"
           );

            bundles.Add(bundleScripts);
            bundles.Add(bundleStyles);
        }
    }

    class NonOrderingBundleOrderer : IBundleOrderer
    {
        public IEnumerable<FileInfo> OrderFiles(BundleContext context, IEnumerable<FileInfo> files)
        {
            return files;
        }
    }
}

Published: 2016-08-25