Skip to content

Instantly share code, notes, and snippets.

@adaam2
Created December 17, 2015 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adaam2/57b7209ea0c254df6bd2 to your computer and use it in GitHub Desktop.
Save adaam2/57b7209ea0c254df6bd2 to your computer and use it in GitHub Desktop.
Basic setup of BundleTransformer with Minification of Scripts and Styles
using BundleTransformer.Core.Builders;
using BundleTransformer.Core.Orderers;
using BundleTransformer.Core.Resolvers;
using BundleTransformer.Core.Transformers;
using System.Web;
using System.Web.Optimization;
namespace uk.co.adamjamesbull
{
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
// Initialize all of the related builders, transformers, orderers etc.
var nullBuilder = new NullBuilder();
var styleTransformer = new StyleTransformer();
var scriptTransformer = new ScriptTransformer();
var nullOrderer = new NullOrderer();
// Replace a default bundle resolver in order to the debugging HTTP-handler
// can use transformations of the corresponding bundle
BundleResolver.Current = new CustomBundleResolver();
// Initialize the common styles bundle
var commonStylesBundle = new Bundle("~/bundles/styles");
commonStylesBundle.Include("~/Styles/main.scss");
commonStylesBundle.Builder = nullBuilder;
commonStylesBundle.Transforms.Add(styleTransformer);
// Add the css minifier
commonStylesBundle.Transforms.Add(new CssMinify());
commonStylesBundle.Orderer = nullOrderer;
bundles.Add(commonStylesBundle);
// Initialize the common scripts bundle
var commonScriptsBundle = new ScriptBundle("~/bundles/scripts");
commonScriptsBundle.Include("~/Scripts/main.js");
commonScriptsBundle.IncludeDirectory("~/Scripts/", "*.js", true);
// Add the script transformer - important: this is the thing that adds the versions onto the end of the js file
commonScriptsBundle.Transforms.Add(scriptTransformer);
// Add the js minifier transform
commonScriptsBundle.Transforms.Add(new JsMinify());
commonScriptsBundle.Orderer = nullOrderer;
commonScriptsBundle.Builder = nullBuilder;
bundles.Add(commonScriptsBundle);
// If this is set, even if the web config is in debug mode = true, minification will take place
BundleTable.EnableOptimizations = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment