Created
December 17, 2015 10:36
-
-
Save adaam2/57b7209ea0c254df6bd2 to your computer and use it in GitHub Desktop.
Basic setup of BundleTransformer with Minification of Scripts and Styles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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