Skip to content

Instantly share code, notes, and snippets.

@bitsprint
Created June 14, 2013 09:18
Show Gist options
  • Select an option

  • Save bitsprint/5780582 to your computer and use it in GitHub Desktop.

Select an option

Save bitsprint/5780582 to your computer and use it in GitHub Desktop.
Bundles
using Web.AppStart;
using WebActivator;
[assembly: PostApplicationStartMethod(typeof(Bundles), "Start")]
namespace Web.AppStart
{
using System.Collections.Generic;
using System.Web;
using System.Web.Optimization;
public static class Bundles
{
public static void Start()
{
CreateJsBundles();
CreateCssBundles();
}
public static void CreateJsBundles()
{
CreateLayoutJsBundle();
CreateCommonJsBundle();
CreateValidationJsBundle();
CreateOfflineJsBundle();
CreateTemplatesBundle();
CreateFlasherJsBundle();
}
private static void CreateCssBundles()
{
var commonBundle = new Bundle("~/CommonCSS", new CssMinify());
commonBundle.AddFile("~/Content/css/Common/reset.css", false);
commonBundle.AddFile("~/Content/css/Common/bootstrap.css", false);
commonBundle.AddFile("~/Content/css/Common/customer.css", false);
BundleTable.Bundles.Add(commonBundle);
var allBundle = new Bundle("~/Content/css/AllBundle", new CssMinify());
allBundle.AddFile("~/Content/css/all.css");
BundleTable.Bundles.Add(allBundle);
CreateSpellCheckCssBundle();
}
private static void CreateSpellCheckCssBundle()
{
var bundle = new Bundle("~/Content/css/SpellCheckCss", new CssMinify());
bundle.AddFile("~/Scripts/jquery.highlighttextarea/jquery.highlighttextarea.css");
bundle.AddFile("~/Content/css/Shared/spellCheck.css");
BundleTable.Bundles.Add(bundle);
}
private static void CreateSpellCheckJsBundle()
{
var bundle = new Bundle("~/bundles/scripts/spellCheck", new JsMinify());
bundle.AddFile("~/Scripts/jquery.highlighttextarea/jquery.highlighttextarea.js");
bundle.AddFile("~/Scripts/spellCheck/spellCheck.js");
BundleTable.Bundles.Add(bundle);
}
private static void CreateOfflineJsBundle()
{
var bundle = new Bundle("~/bundles/js/OfflineLayoutBundle", new JsMinify());
bundle.AddFile("~/Scripts/jquery-1.8.1.min.js");
bundle.AddFile("~/Scripts/jquery.validate.js");
bundle.AddFile("~/Scripts/jquery.validate.unobtrusive.js");
bundle.AddFile("~/Scripts/jquery-ui-1.9.2.min.js");
bundle.AddFile("~/Content/Scripts/Common/datetime.js");
bundle.AddFile("~/Scripts/underscore.min.js");
bundle.AddFile("~/Scripts/knockout-2.1.0.js");
bundle.AddFile("~/Scripts/knockout.mapping-latest.js");
bundle.AddFile("~/Content/scripts/Common/flasher-1.0.js");
BundleTable.Bundles.Add(bundle);
}
private static void CreateLayoutJsBundle()
{
var bundle = new Bundle("~/bundles/LayoutBundle", new JsMinify());
bundle.AddFile("~/Content/js/jquery.placeholder.js");
bundle.AddFile("~/Content/js/main.js");
BundleTable.Bundles.Add(bundle);
}
private static void CreateValidationJsBundle()
{
var bundle = new Bundle("~/bundles/jQueryValidation");
bundle.AddFile("~/Scripts/jquery.validate.js");
bundle.AddFile("~/Scripts/jquery.validate.unobtrusive.js");
BundleTable.Bundles.Add(bundle);
}
private static void CreateFlasherJsBundle()
{
var bundle = new Bundle("~/bundles/Flasher", new JsMinify());
bundle.AddFile("~/Content/scripts/Common/flasher-1.0.js");
BundleTable.Bundles.Add(bundle);
}
private static void CreateCommonJsBundle()
{
var commonBundle = new Bundle("~/CommonJS");
commonBundle.AddFile("~/Scripts/jquery-1.7.2.js");
commonBundle.AddFile("~/Scripts/jquery-tiny-pubsub/ba-tiny-pubsub.min.js");
commonBundle.AddFile("~/Scripts/bootstrap/bootstrap.js", false);
commonBundle.AddFile("~/Scripts/bootstrap/bootstrap-dropdown.js", false);
commonBundle.AddFile("~/Scripts/bootstrap/bootstrap-tooltip.js", false);
commonBundle.AddFile("~/Scripts/jquery.unobtrusive-ajax.js", false);
commonBundle.AddFile("~/Content/scripts/logger.js", false);
commonBundle.AddDirectory("~/Content/scripts/Common", "*.js", true);
BundleTable.Bundles.Add(commonBundle);
}
private static Bundle MergeBundles(string newBundlePath, IEnumerable<string> bundles, bool minify = true)
{
var newBundle = minify ? new Bundle(newBundlePath, new JsMinify()) : new Bundle(newBundlePath);
var bundleContext = new BundleContext(new HttpContextWrapper(HttpContext.Current), BundleTable.Bundles, "~/bundles");
bundles.ForEach((bundlePath) =>
{
var bundle = BundleTable.Bundles.GetBundleFor(bundlePath);
bundle.EnumerateFiles(bundleContext).ForEach((file) =>
{
var path = string.Format("~{0}", file.FullName.Replace(HttpContext.Current.Request.ServerVariables["APPL_PHYSICAL_PATH"], "/").Replace(@"\", "/"));
newBundle.AddFile(path);
});
});
return newBundle;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment