ligershark / WebOptimizer

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

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

Open mattspeterson opened 2 years ago

mattspeterson commented 2 years ago

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!

titiBeOne commented 2 years ago

It would be great,

I tried to bundle without minification, but without success.

2 remarks :

<environment include="Development">
    <script asp-src-include="/lib/**/*.js"></script>
</environment>
<environment exclude="Development">
    <script src="/js/bundle.js"></script>
</environment>
    "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

Airn5475 commented 1 year ago

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?

randyburden commented 10 months ago

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