WebOptimizer icon indicating copy to clipboard operation
WebOptimizer copied to clipboard

[Question] Is there a way to disable bundling/minification in development, but still use the named endpoint?

Open mattspeterson opened this issue 2 years ago • 3 comments

When using ASP.NET's previous built-in bundling/minification capabilities, you could register a bundle, reference that in a view (using @Scripts.Render), and it would render the correct script tags depending on the environment -- the un-bundled, un-minified script files that comprise the bundle in development, and the fully-bundled/minified version in production.

Is there any way to accomplish this using WebOptimizer? It seems that you can selectively enable bundling and minification in the pipeline (by checking env.IsDevelopment() as shown in the README), but how can you use one <script> tag in your view for both environments? It seems the only way is to do something like this:

<environment include="Development">
    <script src="/lib/libfoo/foo.js"></script>
    <script src="/lib/libbar/bar.js"></script>
</environment>
<environment exclude="Development">
    <script src="/js/bundle.js"></script>
</environment>

...but then you are essentially defining your bundle in two places. Let me know if there is an option I'm missing. Thanks!

mattspeterson avatar Aug 10 '22 20:08 mattspeterson

It would be great,

I tried to bundle without minification, but without success.

2 remarks :

  • in your exemple you can use the asp.netcore taghelper asp-src-include to include all files in one folder :
<environment include="Development">
    <script asp-src-include="/lib/**/*.js"></script>
</environment>
<environment exclude="Development">
    <script src="/js/bundle.js"></script>
</environment>
  • A little hack to have un-minified files in development with one config, if you don't worry about having bundle in development, is to use BuildBundlerMinifier to create bundle un-minified :
    "outputFileName": "wwwroot/js/app.js",
    "inputFiles": [
      "Scripts/lib/lib1.js",
      "Scripts/lib/lib2.js",
    ],
    "minify": {
      "enabled": false
    }
  }

And minified with weboptimizer :

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

That's a little bit tricky but it works

titiBeOne avatar Sep 02 '22 13:09 titiBeOne

Running into this same problem! There has got to be a better way to register the bundles in one place and then simply list out the individual file references on the page when in development mode. What am I missing?

Airn5475 avatar Dec 01 '22 15:12 Airn5475

I ran into this same issue. The solution for me was that I forgot to add the Tag Helpers to the _ViewImports.cshtml file. Once I did that, it automatically disabled bundling when running locally and re-enabled bundling once deployed.

See: https://github.com/ligershark/WebOptimizer#tag-helpers

randyburden avatar Oct 30 '23 23:10 randyburden