ligershark / WebOptimizer

A bundler and minifier for ASP.NET Core
Apache License 2.0
768 stars 114 forks source link

AddJavaScriptBundle and similar always minifies no matter the settings. #196

Closed tamashburn closed 2 years ago

tamashburn commented 2 years ago

I had to add my own extension methods that do not call .Minify() after Concatenate(). I call them when CurrentEnvironment.IsDevelopment() is true. Looking at the source code, every overload of AddBundle has a Minify() call. Yes, some have the settings argument, but that doesn't seem to matter.

This did not turn off minification:

        services.AddWebOptimizer(
            CurrentEnvironment,
            new CssBundlingSettings { Minify = !CurrentEnvironment.IsDevelopment() },
            new CodeBundlingSettings { Minify = !CurrentEnvironment.IsDevelopment() }, // etc

    // My extensions
    public static IAsset AddJavaScriptBundleNoMinify(this IAssetPipeline pipeline, string route, params string[] sourceFiles)
    {
        return pipeline.AddBundle(route, "text/javascript; charset=UTF-8", sourceFiles)
            .EnforceFileExtensions(".js", ".jsx", ".es5", ".es6")
            .Concatenate()
            .AddResponseHeader("X-Content-Type-Options", "nosniff");
    }

    public static IAsset AddCssBundleNoMinify(this IAssetPipeline pipeline, string route, params string[] sourceFiles)
    {
        return pipeline.AddBundle(route, "text/css; charset=UTF-8", sourceFiles)
            .EnforceFileExtensions(".css")
            .AdjustRelativePaths()
            .Concatenate()
            .FingerprintUrls()
            .AddResponseHeader("X-Content-Type-Options", "nosniff");
    }
Nouwan commented 2 years ago

We have had a similar issue. We ended up using a work around by providing all AddJavaScriptBundle call's with the following codesettings object.

CodeSettings codeSettings = new CodeSettings { MinifyCode = false, OutputMode = OutputMode.MultipleLines, ReorderScopeDeclarations = false }; I have no clue if this is by design and the global level settings are only used for files automatically bundled from the wwroot folder, but it's either broken or confusing.

tamashburn commented 2 years ago

Thanks. We have decided to stick with Bundler and Minifier for now using the VS 2022 update. We'll move to some node-based something later.