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.

7

u/dustingetz Jun 26 '23

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.

What/which interop hacks are solved if the emitter is upgraded?

8

u/thheller Jun 29 '23

One big assumption in the ClosureJS format is that everything will live in the same global scope. This is perfect since it makes hot-reload and the REPL trivial to implement. Therefore it is technically enough to just concatenate all unoptimized files together and you get a working "build".

However, not a single other "package mechanism" over the years adopted this. requirejs, commonjs, AMD, ESM and whatever else they were all called, all went with scope isolation. Each file can only import and another exported. ESM in many ways is far too strict, that even JS people try to "cheat". It also makes the REPL and hot-reload tricky of course, but I think its doable although it won't look like nice efficient code during dev.

Interop hacks in shadow-cljs for :target :esm are that it tries to make the code look like ESM code. It of course isn't and has to actively work arround the Closure Compiler to do so. :target :npm-module (should have been called :target :commonjs) predates that by many years and is even more hacky in many ways.