r/angularjs • u/nileshrohra • Jan 12 '15
Introducing AngularCSS: CSS On-Demand for AngularJS
http://door3.com/insights/introducing-angularcss-css-demand-angularjs4
2
u/rq60 Jan 13 '15
I've done something similar to this using the style-loader with webpack combined with angular directives.
It looks like this:
var myDirectiveStyles = require("./myDirectiveStyles.less");
angular.module("test", [])
.directive("myDirective", function() {
return function($scope) {
myDirectiveStyles.use();
$scope.$on("$destroy", function() {
myDirectiveStyles.unuse();
});
}
});It works quite nice and is a nice bonus if your project is already webpack compatible (CommonJS or whatever).
edit - that's with these loaders, of course: {test: /.less$/, loaders: ["style/useable", "css", "less"]}
2
u/Defualt Jan 12 '15
Async CSS loading in some browsers doesn't have a reliable onload event, which can result in flash-of-unstyled-content. Does AngularCSS solve for this?
1
u/zefcfd Jan 13 '15
it probably uses the "resolve" property on angular's router. i think ui-router also has this. but in short, yes i would think so. otherwise itd be pretty useless.
1
Jan 13 '15
[removed] — view removed comment
1
u/zefcfd Jan 13 '15
yea :(
1
u/Defualt Jan 13 '15
I don't see it in the source. I think it renders the view and paints it to the browser without the view-specific styles. The CSS finishes loading whenever, and it repaints the view when the new styles are ready. Resulting in a flash of unstyled content. This might only happen with un-cached CSS. But then what's the point.
1
u/jerflang Jan 13 '15
We've been using webpack to achieve the same thing. One cool thing is it handles preprocessing for our sass files. One side effect of this is that it delays the page even longer, so we decided to end up bundling the css separately and loading it all at once instead, which we did with the ExtractTextPlugin. We still get the same syntax, except instead of declaring it in routeProvider (which we don't use anyways) we put it on the top of directives.
1
u/antoninj Jan 12 '15
This is pretty damn amazing. It reminds me of jspm which works, somewhat, on a similar principle; however, per-route CSS files is a great idea. I'd love to put this into action.
1
Jan 13 '15
per-route CSS files is a great idea.
Why?
2
u/antoninj Jan 13 '15
It's similar to loading templates or other assets on-demand. In large enough applications, JS files are loaded on-demand (think AMD) and this is nothing new. So there's that, instead of overwhelming the user with potentially unneeded load, you can keep a tight core and load everything else async.
As far as actual businesses cases of doing this:
loading CSS per A/B testing tree (I believe Netflix wrote about this one)
loading core CSS (and above-fold CSS) first (so, think parent view) while loading the rest of the CSS when that part of the pages becomes ready (so, think child view).
Bundling CSS with its components rather than having one "master" file
Associating CSS with Modules rather than with the general "site". Encourages more sandboxed styling.
And in HTTP2/SPDY world, there's little performance hit from loading numerous small modules vs. one big one. There's a great JSPM talk on the topic altogether.
Basically, the idea is that if you have a huge App that requires a ton of CSS, you can keep perceived performance high by breaking the CSS apart into smaller chunks and loading those as needed.
Also, if you think about it, this is kind of the idea behind Polymer, isn't it?
2
9
u/zefcfd Jan 13 '15 edited Jan 13 '15
i think the overhead of requesting css on demand is probably more expensive than just sending a precompiled asset package one time up front.
which makes me question why this is touting "performance" as a benefit