Skip single file from Minifying?

I am trying to use ASP.Nets BundleTable to optimize some javascript files, but run into a problem when a particular add-on (jQuery-Timepicker) does not work when the code has been reduced. See here .

Currently, the package code is similar to:

// Add our commonBundle var commonBundle= new Bundle("~/CommonJS" + culture.ToString()); // JQuery and related entries. commonBundle.Include("~/Scripts/jquery-1.7.2.js"); commonBundle.Include("~/Scripts/jquery-ui-1.8.22.js"); commonBundle.Include("~/Scripts/jquery.cookie.js"); commonBundle.Include("~/Scripts/jquery-ui/jquery-ui-timepicker-addon.js"); // This is the one that does not work when bundled // JS Transformer commonBundle.Transforms.Add(new JsMinify()); BundleTable.Bundles.Add(commonBundle); 

If I delete the jquery-ui-timepicker-addon.js , add it separately to my web page, then it will work correctly. (Otherwise, I get an Uncaught TypeError: undefined is not a function error).

I am wondering if I can somehow customize my batch code to skip mining this single file (but still include it in the kit)? I look around, but have not come up with any solutions for this.

+8
source share
4 answers

So the problem is that all the files are linked together and then the whole package is minimized. As a result, you cannot easily skip minimizing just one file. Probably the best way to do this is to create a new Transform that will add the contents of this file that you want unminified. Then you add this Transform to the registered ScriptBundle:

 commonBundle.Transforms.Add(new AppendFileTransform(""~/Scripts/jquery-ui/jquery-ui-timepicker-addon.js"")); 

AppendFileTransform will simply add the contents of the file to the linked response. You would no longer include the timepicker in the package, but instead this conversion will include it, and this will effectively give you the behavior you are looking for, since the JsMinify conversion will be run first and minimize the package, and then you will add the file you want at the end was not anonymous.

+3
source

This can be solved better from a different direction - instead of not trying to minimize one file, instead add transformations for individual elements.

First create a class that implements IItemTransform and uses the same code to minimize the given input:

 public class JsItemMinify : System.Web.Optimization.IItemTransform { public string Process(string includedVirtualPath, string input) { var min = new Microsoft.Ajax.Utilities.Minifier(); var result = min.MinifyJavaScript(input); if (min.ErrorList.Count > 0) return "/*minification failed*/" + input; return result; } } 

Second - add this item to separate files and remove the package conversion:

 var commonBundle= new Bundle("~/CommonJS"); // the first two includes will be minified commonBundle.Include("~/Scripts/jquery-1.7.2.js", new JsItemMinify()); commonBundle.Include("~/Scripts/jquery-ui-1.8.22.js", new JsItemMinify()); // this one will not commonBundle.Include("~/Scripts/jquery.cookie.js"); // Remove the default JsMinify bundle transform commonBundle.Transforms.Clear(); BundleTable.Bundles.Add(commonBundle); 
+2
source

You cannot configure the Bundle to skip mining certain files and minimize the rest of the files.

You can implement your own Bundle or Transform by overriding the Bundle.ApplyTransform or JsMinify.Process , but you need to take care not to disturb the tracking of file changes, key generation, cache invalidation, etc ... (or do some ugly hacking). It is not worth the effort.

I would save a separate js file, as you already mentioned.

+1
source

This is just a complete example based on Hao Kung's answer.

 var myBundle = new ScriptBundle("~/bundles/myBundle").Include( "~/Scripts/script1.js", "~/Scripts/script2.js", ); myBundle.Transforms.Add(new AppendFileTransform("~/Scripts/excludedFile.min.js")); bundles.Add(myBundle); 

And here is an example implementation of AppendFileTransform:

 public class AppendFileTransform : IBundleTransform { private readonly string _filePath; public AppendFileTransform(string filePath) { _filePath = filePath; } public void Process(BundleContext context, BundleResponse response) { response.Content += File.ReadAllText(context.HttpContext.Server.MapPath(_filePath)); } } 
0
source

All Articles