r/Clojure • • Jun 26 '23

A case for ClojureScript 2.0

https://tonsky.me/blog/clojurescript-2/
82 Upvotes

25 comments sorted by

View all comments

56

u/thheller Jun 26 '23

Unfortunately there are so many wrong takes in this that I completely disagree with the premise. Yes, :advanced can be intimidating. Yes, it can break a build. However, the reason that is breaks is interop with anything that is NOT part of those :advanced optimizations.

If you can stomach to 3-4x your build size just run with :simple? Arguably, if you can live with that you can also probably take the very minor performance hit that comes with it. :simple requires no externs whatsoever and is almost on par with the minification done by other JS tools. Modern JS tools can tree shake a little, however it is not compatible with :simple so that is a problem. It is also possible to set :compiler-options {:property-renaming :off} in your build config. That will disable to most aggressive optimization of :advanced, but honestly at that point you can just use :simple.

Let's take shadow-cljs with datascript as an example. The reason it breaks is not the lack of externs. The reason it breaks is that the code was written by complecting two entirely different usage scenarios. One is Datascript as a library for CLJS consumers, the other is Datascript as a library published for JS consumers. If you are using the CLJS code from CLJS you do not require any externs whatsoever. The reason it breaks is ignoring the rules, not because :advanced arbitrarily breaks your code. I suspect the code for scenario 2 is just so old that it was written before good options from the build tools existed and never revised.

:advanced is stricter than other tools yes, and interop with not-:advanced compiled output can be tricky. Externs inference made it much less of a pain though. shadow-cljs also tries to infer more externs from the node_modules code it processes, so that it is often even less of an issue.

Now, onto the topic of ClojureScript 2.0. I'm totally with you, there is potential is doing such a thing. Modern JS has changed a lot over the years. Many things are possible today that weren't when CLJS was first released. Many of the choices made are simply outdated. Choosing the Closure Compiler and :advanced optimizations is not one of them. They are optional today, and they would be optional in CLJS 2.0. I'd still work on supporting them because I absolutely want them. I very much care about the sizes of my builds.

Now, I'm not even trying to defend the Closure Compiler itself. Their project management style is horrible. Constantly making breaking changes for no reason is very frustrating. But shadow-cljs for example makes use of many non-public APIs, so this is sort of understandable and expected. It's a choice I made specifically, so I'm fine to deal with it. Fact remains :advanced is still years ahead of any other tool, and not having to write it ourselves made our life much easier.

Comparing the dynamic-ness of CLJ is also sort of a bad take. Clojure absolutely has many issues if you factor in AOT compilation and other kinds of "optimizers" such as GraalVM. You have to sacrifice some dynamic-ness to get those benefits, CLJS is no different on that front.

So, I'm all for starting a CLJS 2.0 effort. shadow-cljs is already working in that direction in some ways. I'm actively trying to avoid a CLJS fork, since what we have to gain is sort of minimal IMHO. Blaming :advanced however is not a premise to start that work on. Just emit modern JS, and :advanced can process it just fine. If you don't want that use something else. The reason that interop requires many hacks nowadays is that CLJS still emits a dead and otherwise unused ClosureJS format. Getting rid of that should be the goal, and the Closure Compiler becomes fully optional as a bonus.

5

u/didibus Jun 26 '23

Why can't JS bundlers deal with the output of simple?

8

u/thheller Jun 29 '23 edited Jun 29 '23

They can deal with :simple, they just can't tree shake it or minify it meaningfully. The namespacing alone can get very verbose after all.

:advanced first collapses namespaces, so cljs.core.assoc becomes cljs_core_assoc (no dots, nested objects), then just like any other variable it just gets renamed to x or whatever. :simple does not do this, and neither does any other JS tool.