jetheredge / SquishIt

Lets you *easily* bundle some css and javascript! Check out the Google group if you have questions!
http://groups.google.com/group/squishit
MIT License
459 stars 119 forks source link

Add to bundle in partial views then render in master/layout view #336

Open brgrz opened 7 years ago

brgrz commented 7 years ago

I can't seem to find an example whether it is possible to add to a bundle in different views across the application then render the bundle in master/layout view.

I tried but the bundle seemed to be empty when rendering.

A step further would be to have named bundles, adding scripts to them,then render them out by name.

brgrz commented 7 years ago

I'd be willing to take a shot at implementing it if not supporting - if @AlexCuse could point me in the right direction.

AlexCuse commented 7 years ago

There is support that @syndicatedshannon added a while back. See the AspNetMvcTest for details, under "Auto-Bundling"

https://github.com/jetheredge/SquishIt/blob/master/SquishItAspNetMvcTest/Views/AutoBundling/Combined.cshtml

The code to support it is in SquishIt.Mvc.

brgrz commented 7 years ago

I'll take a look

syndicatedshannon commented 7 years ago

Hey guys. Didn't mean to start something and bail. I transitioned to Angular almost right after I got involved here, but would still be glad to lend a hand if I can. @brgrz , want to chat?

brgrz commented 7 years ago

Hey, I don't really know what autobundling feature was all about, I have to look at it but didn't have time yet. Will probably do it on friday. In the meantime, if you could explain what it was about?

brgrz commented 7 years ago

Also I see #337 and this one related meaning we could have server side modules which register scripts and styles they need and we'd render them out as needed per current view/module that's active since with HTTP/2 we don't have to bundle anymore (some say it still makes sense to but let's assume for these two issues that we don't bundle anything anymore). So we truly get views that load up only the resources they really need.

AlexCuse commented 7 years ago

If you look at that view I linked, the two HtmlHelper extension methods are what you are after.

.AddResources adds the various resources, then .ResourceLinks renders them.

You can also use .AddScriptResources / .AddStyleResources and .StyleResourceLinks / .StyleResourceLinks if you want to add/render in different sections of the page. .AddResources routes to the appropriate bundle by file extension.

https://github.com/jetheredge/SquishIt/blob/master/SquishIt.Mvc/HtmlHelperExtensions.cs

@syndicatedshannon this seems to work well just wanted to give you credit for the work. The fact it is not documented is on me :smiley:

brgrz commented 7 years ago

@syndicatedshannon @AlexCuse this seems to do what I am after but it doesn't render minified and hash-stamped files.

Another thing, I am using custom view engine to locate views. My views register resources they require which are commonly placed in the folder right next to them. When I used my own code to render out links, I registered them by also providing ViewVirtualPath to the registration method which then rewrited the href/src accordingly. SquishIt of course currently cannot do that and points them to the root of the site.

This is what it renders (note that the view virtual path actually is there available to SquishIt it just doesn't use it):

<link rel="stylesheet" type="text/css" data-autobundle-declaring-view="~/Plugins/Roswell.Plugins.Calendar/Views/Shared/_PluginLayout.cshtml" href="/roswell/styles/plugin.min.css">

brgrz commented 7 years ago

Looking at the Autobundler class I think what I need is a convention in place that would say

when viewPath.startsWith("~/plugins") rewrite resource link to "~/plugins/PluginName/[resource-link-path]"

As per the above example I'd take the data-autobundle-declaring-view value, strip it down to the plugin root folder and merge it with the hrefvalue getting

href="~/plugins/roswell.plugins.calendar/styles/plugin.min.css"

My question is, should I implement that in the Autobundler class?

AlexCuse commented 7 years ago

What do you mean "but it doesn't render minified and hash-stamped files." - are you running in debug mode? I would expect release bundle rendering to go through the same pipeline, rendering a single minified, hashed file per bundle.

It sounds like you have a very specialized use case, and probably would want to implement your own auto-bundler. If we can help by adding extensibility points to the existing auto-bundler I am all ears.