Disable WebOptimizer bundling for DEBUG mode

272 views Asked by At

I am migrating a .NET Framework project to .NET 6.

Old bundling logic in .NET framework had the below code:

#if DEBUG
   BundleTable.EnableOptimizations = false;
#else
   BundleTable.EnableOptimizations = true;
#endif

I believe, the code above disables bundling for DEBUG mode & enables it for RELEASE mode?

In .NET 6, I am using WebOptimizer for bundling.

Is there a similar option or setting in WebOptimizer as well?

3

There are 3 answers

1
Ozan BAYRAM On BEST ANSWER

So actually, this is not specific to .NET 6. I believe you may use something like this.

#if DEBUG
   // just do not add web optimizer code
#else
   app.UseWebOptimizer();
#endif

Also in documentation there is something like this:

Disabling minification: If you want to disable minification (e.g. in development), the following overload for AddWebOptimizer() can be used:

if (env.IsDevelopment())
{
    services.AddWebOptimizer(minifyJavaScript:false,minifyCss:false);
}

https://github.com/ligershark/WebOptimizer

Regarding to your code in answer below, it is better to do like this:

public void Configure(WebApplication app, IWebHostEnvironment env)
{
    if (app.Environment.IsDevelopment())
    {
        app.UseWebOptimizer();
    }

    app.UseHsts();
    app.UseHttpsRedirection();

    app.Run();
}
1
Ashutosh Gupta On

I think following will also mean the same.

public void Configure(WebApplication app, IWebHostEnvironment env)
        {
            if (!app.Environment.IsDevelopment())
            {
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseWebOptimizer();
            app.Run();
}
0
Oskar Sjöberg On

Given that you configure AddWebbOptimizer with a pipeline and that you are using the included taghelpers to "render" the bundle just change EnableTagHelperBundling appropriately like this:

#if DEBUG
   var useWebOptimizer = false;
#else
   var useWebOptimizer = true;
#endif

services.AddWebOptimizer(
    pipeline =>
    {
       pipeline.AddJavaScriptBundle("/bundles/js","/scripts/a.js","/scripts/b.js");             
       pipeline.AddCssBundle("/bundles/css","/scripts/a.css","/scripts/b.css");
    },
    option =>
    {
       option.EnableCaching = true;
       option.EnableTagHelperBundling = useWebOptimizer;
    });