Documentation

JavaScript Minification

Cassette uses the Microsoft Ajax Minifier library to minify JavaScript. This performs a set of optimizations, such as removing whitespace and shortening variable names, to reduce the size of JavaScript. Smaller JavaScript means faster downloads.

Customizing the minifier

Cassette registers the minifier in the IoC container. So your application is able to override this registration by implementing IConfiguration<TinyIoCContainer>.

public class CustomCassetteServices : IConfiguration<TinyIoCContainer>
{
    public void Configure(TinyIoCContainer container)
    {
        var settings = new CodeSettings();
        codeSettings.LineBreakThreshold = 60;
        container.Register<IJavaScriptMinifier>(new MicrosoftJavaScriptMinifier(settings));
    }
}

Alternatively, you can provide a completely new implementation of the IJavaScriptMinifier interface.

public class CustomCassetteServices : IConfiguration<TinyIoCContainer>
{
    public void Configure(TinyIoCContainer container)
    {
        container.Register<IJavaScriptMinifier>(new MyCustomJavaScriptMinifier());
    }
}

public class MyCustomJavaScriptMinifier : IJavaScriptMinifier
{
    public Func<Stream> Transform(Func<Stream> openSourceStream, IAsset asset)
    {
        return delegate
        {
            using (var reader = new StreamReader(openSourceStream()))
            {
                var output = MinifyJavaScript(reader.ReadToEnd());
                return output.AsStream();
            }
        };
    }
    
    string MinifyJavaScript(string input)
    {
        // TODO: call minifier here...
    }
}

The Cassette project would welcome some additional JavaScript minifier contributions!