google / closure-stylesheets

A CSS+ transpiler that lints, optimizes, and I18n-izes
Apache License 2.0
314 stars 65 forks source link

Automatic reordering of stylesheets #84

Open jart opened 8 years ago

jart commented 8 years ago

If stylesheets use @provide and @require then it is possible for the compiler to put them back in the correct order. Closure Compiler has been able to do this with JS sources for at least a few years. I feel like this is important because it allows the build system to be more forgiving, if you do things like sort your srcs list, or use glob(["*.css"]) rather than have a separate closure_css_library for each and every CSS file.

I might be able to contribute this as a 20%er at some point in the future.

mikesamuel commented 8 years ago

I use TopoSort for this in closure-maven-plugin.

jart commented 8 years ago

Oh my gosh thank you for sharing this @mikesamuel! That and CssDepGraph are really going to save me a lot of time.

Out of curiosity, is your checkForCycles() function guaranteed to halt? I'm not sure considering the way it's going back in there to try and find shorter cycles. But even if it weren't for that, I think the algorithm might still be O(2^|V|). I wrote a very elegant implementation last year of Tarjan's algorithm for finding strongly connected components. It's O(|V| + |E|) and returns the set of all cycles in the graph. Although in all fairness, SCC's are intuitively more like "tangled clusters" rather than cycles. You could display them all, or just the smallest one.

Edit: O(2^|V|) not O(|V|^2)

jart commented 8 years ago

Ooh also you could probably use Sets.newIdentityHashSet() for your seen variable.