-
Notifications
You must be signed in to change notification settings - Fork 48
Can I plug in my own minifier?
mwrock edited this page Oct 14, 2011
·
5 revisions
RequestReduce attempts to follow a decoupled architecture which allows developers to swap out certain parts with their own behavior. To override RequestReduce's use ofthe Micosoft Ajax inifier library, you simply create a class that derrives from IMinifier. There is not much to IMinifier:
public interface IMinifier
{
string Minify<T>(string unMinifiedContent) where T : IResourceType;
}
Here Is RequestReduce's implementation:
public class Minifier : IMinifier
{
private readonly Microsoft.Ajax.Utilities.Minifier minifier = new Microsoft.Ajax.Utilities.Minifier();
private readonly CodeSettings settings = new CodeSettings {EvalTreatment = EvalTreatment.MakeAllSafe};
public string Minify<T>(string unMinifiedContent) where T : IResourceType
{
if (typeof(T) == typeof(CssResource))
return minifier.MinifyStyleSheet(unMinifiedContent);
if (typeof(T) == typeof(JavaScriptResource))
return minifier.MinifyJavaScript(unMinifiedContent, settings);
throw new ArgumentException("Cannot Minify Resources of unknown type", "unMinifiedContent");
}
}
It's not difficult to imagine how you would change this implementation to use something like the YUI Compressor for.Net. Lets say you had a YuiMinifier class tat you want RequestReduce to use instead of its own minifier. You would just need to add the following code to your startup code:
RRContainer.Current.Configure(x => x.For<IMinifier>().Use<YuiMinifier>());
That's it. Now your minification code will be used.