mhallin / forest

CSS modules for Clojurescript
MIT License
63 stars 3 forks source link

try to compile to string as much as possible #6

Closed piranha closed 7 years ago

piranha commented 7 years ago

This branch adds few modifications which make forest compile stylesheets to this:

test.root.root_styles = new cljs.core.PersistentArrayMap(null, 2, [new cljs.core.Keyword(null,"source","source",-433931539),".test_root_root-styles__nav\n{\n  align-items: center\n}\n\n.test_root_root-styles__icon\n{\n  margin-right: 32px\n}",new cljs.core.Keyword(null,"full-name","full-name",408178550),"test.root/root-styles"], null);

rather than current version:

test.root.root_styles = new cljs.core.PersistentArrayMap(null, 2, [new cljs.core.Keyword(null,"source","source",-433931539),clojure.string.join.call(null,"\n\n",new cljs.core.PersistentVector(null, 2, 5, cljs.core.PersistentVector.EMPTY_NODE, [[cljs.core.str(".test_root_root-styles__nav"),cljs.core.str("\n{\n"),cljs.core.str(clojure.string.join.call(null,";\n",new cljs.core.PersistentVector(null, 1, 5, cljs.core.PersistentVector.EMPTY_NODE, ["  align-items: center"], null))),cljs.core.str("\n}")].join(''),[cljs.core.str(".test_root_root-styles__icon"),cljs.core.str("\n{\n"),cljs.core.str(clojure.string.join.call(null,";\n",new cljs.core.PersistentVector(null, 1, 5, cljs.core.PersistentVector.EMPTY_NODE, ["  margin-right: 32px"], null))),cljs.core.str("\n}")].join('')], null)),new cljs.core.Keyword(null,"full-name","full-name",408178550),"test.root/root-styles"], null);

This obviously speeds up loading a bit plus reduces file size (even with advanced minification). I guess it could be improved further, so that if a single variable is used, everything except for this one variable is converted to a string - but that is a little bit more work. :)

mhallin commented 7 years ago

Nice!

I was thinking of adding an optimization layer that could merge/shorten consecutive strings and this is a great first step.

piranha commented 7 years ago

To be honest, it would also be nice to create a function which will convert [(str "a: " x ";\n") (str "b: " y ";\n")] into [(str "a: " x ";\n b: " y ";\n")], it'll be just a little bit more general - maybe a bit more work, like flatten or something - it certainly needs to unroll join. But I just understood that, what I did was much easier, but much less general - joining strings will not cross the borders, unfortunately, and a single variable will break all those optimizations. :-))