31 ms·
Create ad-hoc ClojureScript scripts on Node.js with nbb
Nbb is a scripting environment that aims to make it easier to create ad-hoc scripts on Node.js using ClojureScript. It does not need a JVM for compilation: just use npm and you're good to go. Code is interpreted instead of compiled, which for scripting is usually ok. If your project outgrows the scripting phase, you can migrate to a proper ClojureScript compiler setup. Give it a try if you're curious about ClojureScript.
https://github.com/borkdude/nbb https://github.com/borkdude/nbb
- CraigJPerry 5y agoThe UX is pretty flawless for getting started, i can just exec random clojurescript instantly on an old 6700k desktop (assuming i have nodejs installed, but nothing else): ~/Code/local/clojure ····································· at 15:03:13 time npx nbb -e "(:doc (meta #'meta))" "Returns the metadata of obj, returns nil if there is no metadata." 0.13s user 0.02s system 114% cpu 0.134 total
- Borkdude 5y agoThanks! Note that npx itself adds some startup time to the mix. It's usually faster when ran as a global command (which should be safe since nbb doesn't depend on any other node libraries). Notes on that here: https://github.com/borkdude/nbb#startup-time https://github.com/borkdude/nbb#startup-time
- brundolf 5y agoDoes it add startup time after the first run (where it downloads and caches the dependency)?
- ithrow 5y agoAll this just to avoid writing JS in nodejs for simple scripts? why add more complexity to simple things? just to write JS with parenthesis? clojurescript is overkill and definitely tedious to use for this because is not suited for scripts just doing lots of imperative, side-effecting stuff and dealing constantly with mutable APIs. I love lisp but... https://github.com/borkdude/nbb/blob/main/examples/puppeteer/example.cljs https://github.com/borkdude/nbb/blob/main/examples/puppeteer... "Look how cool I'm, I downloaded this thing just to add parenthesis to my JS script!"
- aaron-santos 5y agoWhy would I want nodejs? All of that to avoid writing C for simple things?
- rmetzler 5y agoWhy would I want C? All of that to avoid writing Assembler for simple things?
- aaron-santos 5y agoThis feels like a personal attack, bud.
- piercebot 5y agoI think the real advantage is scripting with a REPL in your IDE. Whipping up a script becomes trivial because you can test every data transformation (heck, every expression!) with a keystroke. There's no great way to do this in a language without s-expressions.
- harperlee 5y agoAs a datapoint, I don't know javascript but I do know clojurescript.
- Scarbutt 5y agoYeah, good luck with that.
- brundolf 5y agoThis is great, I love batteries-included all-in-one language runners. Is this a totally custom interpreter? If so, what's the coverage like of ClojureScript's built-in features and functionality? Do third-party libraries generally work in it without issues?
- Borkdude 5y agoNbb is to CLJS what babashka is to JVM Clojure. Both scripting environments are implemented using SCI, a Clojure(Script) interpreter which re-uses all the core functions when you call those, the rest (creating vars, functions, namespaces) is interpreted "glue code" and that code isn't compiled (to bytecode or JS); instead it uses interpretation while trying to stay as close as possible to the host language. There are some things that are currently not supported, most notably deftype. For SCI on JS this is just a matter of time, but on JVM the target for SCI is mostly GraalVM images which come with the restriction that you can't implement new types at runtime. SCI originates mostly from the work on babashka, but with nbb, I'm adding better support for CLJS over time. As for third-party libs: it depends if they are written in the subset that SCI supports. If the library is useful enough for scripting then it can be considered for added as a built-in. The classpath support is still something under development, but it already works for gitlibs. Here is an example of running HoneySQL from source in nbb: https://github.com/borkdude/nbb#classpath https://github.com/borkdude/nbb#classpath. Just let me know if something doesn't work out (on Slack or Github issues/discussions).
- brundolf 5y agoThanks! I'm not sure I'll use it for anything, I haven't really used ClojureScript, I'm just a language geek who was curious :)
- Borkdude 5y agoWell yeah, that might be a good use case for nbb too: people who just want to try out a bit of "CLJS" without spending too much time on setup first.
- gerritjvv 5y agonice, keep up the good work. clj gives a different way of thinking (imho better), but alas even introducing algebra to geometry had push back, and so will anything that upsets the norm. Another equally important benefit of clojure is its stability, i.e you can write a script today and it will run weeks/months/years/decades afterwards, the same cannot be said for js. This is all known to people already using clj and cljs, and now we can keep enjoying our clojure on nodejs also :) note: there is a misconception aparently that clojure does not support mutability, it does, just does so in a sane way so you dont shoot yourself in the foot. So yes you can mutate in clojure, and yes you can have side affecting stuff, clojure programmers do so every day .