Skip to content

Can I plug in my own minifier?

mwrock edited this page Nov 11, 2011 · 5 revisions

Yes you can use your own minifier with your own custom settings and behavior.

RequestReduce attempts to follow a decoupled architecture which allows developers to swap out certain parts with their own behavior. To override RequestReduce's use of the Micosoft Ajax minifier 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 that you want RequestReduce to use instead of its own minifier. You would just need to add the following code to your startup code:

RequestReduce.Api.Registry.RegisterMinifier<MyMinifier>();

That's it. Now your minification code will be used.

Clone this wiki locally