Much like the way stylus
middleware automatically compiles needed css for a template (jade in my case) when a route is triggered, is there a way to do it automatically outside of the route -> middleware flow?
I have a build system where I render some html files via app.render
and then save them to the filesystem (many people do similar for email templates). I want to make sure that when I app.render("foo.jade", func...
, that any of the .css files needed by foo.jade
are automatically compiled from their styl
counterparts. This happens perfectly and automatically if I have a route that renders foo.jade
, but in this case, there are no routes. I understand that I can use stylus'
api and call stylus.render
, but that would mean I'd have to specify and render every one of the styl/css
files needed by my template "by hand."
I want to make sure that when I app.render("foo.jade", func..., that any of the .css files needed by foo.jade are automatically compiled from their styl counterparts
If you want to do that ahead of time, as in when foo.jade
is rendered as opposed to when GET /foo.css
is requested, no there's almost certainly nothing that is going to parse you HTML on the way out to the filesystem and dynamically render all referenced CSS. At least not exactly to your specifications. However, there do exist tools to read an HTML file and inline it's referenced CSS, so you may be able to run your HTML through those.
You may want to look at juice but since it's geared toward email and inlines CSS into the HTML itself, it may not do what you want (which is probably generating .css files next to the HTML).
My suggestion is parse the HTML for linked CSS URLs yourself and render the corresponding stylus to disk appropriately. Use consistent patterns and you should be able to handle all your templates with one or just a few functions.
However, if you want to render stylus to CSS on the fly when a GET /foo.css
request comes in, sure there are existing middlewares to do that or you could role your own. And no, you wouldn't need a route for every CSS path, just some URL patterns/params and good old programming.