using WebOptimizer and pre-minimized files?

1k views Asked by At

If I am using the WebOptimizer -- https://github.com/ligershark/WebOptimizer -- for bundling and minification in my ASP.Net Core application is there any need to keep the minified versions of the client-side libraries that are being used around?

For example, do I still need code like this in the layout page:

<environment include="Development">
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
</environment>
<environment exclude="Development">
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
</environment>

Or can I just have the link to the un-minified version and WebOptimizer will minify it on the fly and then I can delete the boostrap.min.css file from my project?

And on a related note, what would the WebOptimizer middleware do if it encountered a pre-minified file?

1

There are 1 answers

1
Oscar On BEST ANSWER

This is a bit old but I thought it was good to answer this.

Instead of doing it on the HTML part of things, you could configure the bundle to minify the file or not in your configuration, and just reference bootstrap.css in your html normally.

Here you can find an example of how this is done: https://www.vinayjadav.com/posts/bundle-js-css-aspnet-core

   public static class WebOptimizerBootstrapper
   {
       public static void Configure(ServiceRegistry services)
       {
           string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
           bool isDevelopment = environment == Environments.Development;
           services.AddWebOptimizer(pipeline =>
           {
               pipeline.AddCssBundle("/css/site.css", "css/**/*css");
               pipeline.AddJavaScriptBundle("/js/site.js", "js/**/*.js");
               if (!isDevelopment)
               {
                   pipeline.MinifyCssFiles();
                   pipeline.MinifyJsFiles();
               }
           });
       }
   }

And in your Program.cs (for .net core)

builder.Services.AddWebOptimizer(services=>WebOptimizerBootstraper.Configure)