r/Clojure • • Aug 18 '22

ClavaScript: a ClojureScript syntax to JS compiler

Introducing ClavaScript, a ClojureScript syntax to JavaScript compiler!

The primary use case for clava (its short name, not to be confused with Calva) is to produce JavaScript using ClojureScript syntax while producing ultra-small bundle sizes, by leveraging the JS platform directly. Clava does not protect you from the pitfalls of JS (mutability, equality), but lets you use the concepts you already know from ClojureScript and maps them more or less directly to JS where this makes sense. It comes with a standard library which has many of the core functions you know: map-indexed , for example, works like you would expect, but returns a mutable JS array instead of an immutable vector.

Repo: https://github.com/clavascript/clavascript

Npm: npm install clavascript

Read Differences with ClojureScript for... well, the differences with ClojureScript.

It has support for async/await and JSX.

Clava is open to contribution, especially in the area of porting core functions.

An example of a SolidJS app written using Clava:

https://clavascript.github.io/demos/clava/solidjs/ (source)

Give it a spin and let us know what you think.

81 Upvotes

10 comments sorted by

5

u/zeromath0 Aug 19 '22

:O very interesting

1

u/lucywang000 Aug 20 '22

May I ask how does it compare with nbb? https://github.com/babashka/nbb

4

u/Borkdude Aug 20 '22

You may! nbb is a ClojureScript interpreter for Node.js (based on SCI). You can run scripts with it directly and nbb is batteries included: it has libraries that you will commonly need for scripting. Nbb preserves CLJS semantics and what you get there are the data structures you're used to with CLJS. ClavaScript is a more general tool which compiles directly to JS and does _not_ include CLJS data structures: it returns plain JS objects and arrays in most cases. The benefit of doing so is that you can write JS using CLJS syntax and get really small bundle sizes, at the cost of not using the CLJS data structures.

1

u/noomey Aug 20 '22

I've been closely following your work on ClavaScript on twitter and I admit I find it a bit intriguing as I'm still not 100% clear on when I should use this compared to ShadowCLJS (as a confirmed js/ts developer and clojure/cljs beginner). I feel like I'm getting a better grasp thanks to your comment though, and now I'm wondering wether this proposition https://github.com/tc39/proposal-record-tuple could help ClavaScript to better "mimic" the immutable data structures of Clojure?

1

u/Borkdude Aug 20 '22

ClavaScript and Cherry are currently experimental. If you want to build a production application I would currently recommend shadow-cljs.

1

u/lucywang000 Aug 21 '22 edited Aug 21 '22

Thanks!

So clavascript is like coffeescript/typescript/rescript, which is a (heavy) layer of syntax sugar for javascript.

Here is my understanding: if javascript runtime is the "CPU" that executes "machine code", then we can say:

  • clavascript/coffeescript/typescript/rescript etc. is like the assembly language, i.e. human friendly way of writing machine code. (Actually, considering the great syntax difference between calvascript and javascript, it's much closer to rescript than coffeescript/typescript)

  • clojurescript is like c++, which has its own standard library

  • nbb is like java/python, which is interpreted by a runtime virtual machine (sci) .

1

u/Borkdude Aug 21 '22

Yes, kinda! Although clavascript has a growing standard library of CLJS-like functions that are analogous but using JS data structures mostly

30

u/[deleted] Aug 19 '22

[removed] — view removed comment

2

u/strranger101 Aug 21 '22

My only concern is that we could have had Baklava instead of Clava

1

u/DeepDay6 Aug 26 '22

...and it would probably be something like Babashka, only it would run on NodeJS instead of GraalVM...