javascript.netminifycassette

Stop Cassette from minifying a JavaScript file


I'm using Cassette to minify my JavaScript. I don't want Cassette to minify one of my JavaScript files because it's causing an error. I'd rather use the already minified version provided by the original library authors.

How can I add a JavaScript file to Cassette without it minifying the file?


Solution

  • You can use the following code for Cassette 1.x to create a IAssetTransformer that doesn't perform any minification

    public class NoMinification : IAssetTransformer
    {
        public NoMinification() {}
    
        public Func<Stream> Transform(Func<Stream> openSourceStream, IAsset asset)
        {
            return openSourceStream;
        }
    }
    

    And then update your CassetteConfiguration to put the already minified file it's own bundle, because you have to set the minifier for all of the files in a single bundle. If this javascript file has a dependency on another file, which will minified by cassette and end up in it's own bundle, you can use .AddReference as I show in the commented out line.

    public class CassetteConfiguration : ICassetteConfiguration
    {
        public void Configure(BundleCollection bundles, CassetteSettings settings)
        {
            //So, we set a no-op minifier for this bundle and force it into it's own bundle.
            bundles.Add<ScriptBundle>("Scripts/already-minified-file.min.js", b => {
                b.Processor = new ScriptPipeline { Minifier = new NoMinification() };
                //b.AddReference("~/Scripts/dependent-scripts.js");
            });
        }
    }