madskristensen / BundlerMinifier

Visual Studio extension
Other
614 stars 172 forks source link

Is it possible to use this library to minify CSS and JS server side? #291

Open dustinmoris opened 7 years ago

dustinmoris commented 7 years ago

I was wondering if this NuGet package exposes a .NET class which I can use to pass in a CSS/JS string and get back a minified version of it?

Personally I don't think that it makes sense to do the bundling/minifying during the build step. I have an F# web application where I read all my CSS files before Kestrel starts, then I want to minify and bundle them and then I would create a hash from the entire string before setting up a static route where let's say /css?v={hash} will return the bundled and minifid string from memory.

This has the advantage that I can set maximum browser caching on this endpoint and whenever the css changes the hash would change and therefore every client would immediately invalidate and receive the latest css. Also it would do the job of minifying and bundling only once on appliaction start.

(Additional optimizations are made by CloudFlare on top of it.)

So my question is if this is possible with BundlerMinifier?

manigandham commented 7 years ago

Minification during build is more efficient since the files won't change during runtime. If you still want to do it on the fly, you can use a library like Smidge: https://github.com/Shazwazza/Smidge

Also if you just want to hash the files, that's already built into ASP.NET MVC, the tag helpers can do this with the asp-append-version attribute on the tags:

<script src="/path/to/output/file.js" asp-append-version="true"></script>

<link rel="stylesheet" href="/path/to/output/file.css" asp-append-version="true" />

dustinmoris commented 7 years ago

Hi @manigandham thanks for your input and pointing to Smidge. I don't use MVC and razor pages therefore tag helpers is not an option and I also didn't mean to minify on the fly. I meant that I can minify and bundle all my static css assets at startup, only once and then hash it and keep the small minified string, as well as the hash code in memory for the entire lifetime of the application and return the versioned css on an endpoint straight from memory. So the minification and bundling and hashing would happen only once and never again and it would happen before any client can make a web request because it would happen before Kestrel has spun up if that makes sense?

I'll have a look at Smidge.. thank you!

Also, regarding the tag helper, I am not familiar with it but will it not hash it every time on the fly?

manigandham commented 7 years ago

Sounds good. That's basically what the tag helper already does, it hashes it once then keeps the hash in memory:

https://stackoverflow.com/questions/33342643/how-does-javascript-version-asp-append-version-work-in-asp-net-core-mvc

https://docs.microsoft.com/en-us/aspnet/core/api/microsoft.aspnetcore.mvc.taghelpers.internal.fileversionprovider

grahamehorner commented 6 years ago

I would like this type of functionality, as my user case is allowing a user to customize the theme and persist it with in a theme gallery for that user; make perfect sense to me to use the same component/tools the creates the css for standard themes to also be used to create custom user theme at runtime