clojurescript

maybenot 2024-11-30T06:02:51.610339Z

Letโ€™s say I have a bunch of pure clojure functions Can I use Clojurescript compiler to turn them into a big js file without any dependencies and include it into node, browser, react native, etc?

Ken Huang 2024-11-30T07:13:08.460639Z

I think so, like using shadow-cljs, you can build it for different targets, browser, esm, node, etc.

thheller 2024-11-30T07:24:55.433089Z

of course you can. that is what clojurescript basically is ๐Ÿ˜›

thheller 2024-11-30T07:27:28.770079Z

however many people often misjudge the ergonomics. "no dependencies" still means that you'll be including cljs.core for all the base functions/datastructures you are going to use. so the baseline is about 30kb gzip and it can often involve a lot of datastructure conversion overhead when working with JS directly, just because JS doesn't understand clojure datastructures and you'll rarely work with JS objects directly in CLJS. you of course can but it won't exactly look like idiomatic CLJS code

maybenot 2024-11-30T13:18:11.000379Z

Thank you, the thing I don't get is this: when I compile with target browser or bundle clj -M -m cljs.main --target browser --output-to main.js -c hello.core I got something like this

window.CLOSURE_UNCOMPILED_DEFINES = {"cljs.core._STAR_target_STAR_":"browser"};
window.CLOSURE_NO_DEPS = true;
....
So, window reference, which is not universal When I use target node I got
require(path.join(path.resolve(".") 
which is ofc node specific What I want is just a big js file without document or require etc, which I can include to JSC, react-native, node, or browser Probably I'm missing something idk

thheller 2024-11-30T13:19:44.845779Z

cljs.main --target browser. this command literally tells it to expect a browser environment and generate code accordingly

thheller 2024-11-30T13:20:05.757809Z

shadow-cljs has support for more generic targets

thheller 2024-11-30T13:20:31.439199Z

blame JS for having so many different "delivery methods"

maybenot 2024-11-30T13:22:19.797709Z

right, browser is obvious, but I thought maybe none is what I need but seems like no, so I thought maybe thereโ€™s something else, probably config toggle or something, especially using bundle

thheller 2024-11-30T13:23:06.564919Z

cljs.main doesn't have many options regarding this. shadow-cljs does.

maybenot 2024-11-30T13:24:59.675839Z

Iโ€™ll try shadow thank you! Seems like generic is something useful to include in cljs, I was thinking about writing core in Clojurescript, but still use js for react expo, cause hooks and modern react spoiled all ecosystem..

thheller 2024-11-30T13:26:08.210839Z

well even JS has rules for how you access stuff. the "just throw it into the global scope" ways of old aren't really used anymore. so instead you'd use something like ESM (or commonjs before that)

maybenot 2024-11-30T13:31:47.631029Z

Yea, universal ESM module sounds even better for what I thought of Probably webpack could do the trick

thheller 2024-11-30T13:32:26.495959Z

https://shadow-cljs.github.io/docs/UsersGuide.html#target-esm

๐Ÿ”ฅ 1
maybenot 2024-11-30T13:34:54.276129Z

I wonder if anyone still uses stock cljs at this point.. ๐Ÿ˜ถโ€๐ŸŒซ๏ธ

thheller 2024-11-30T13:35:55.115789Z

shadow-cljs is just a build tool. it is still just CLJS ๐Ÿ™‚ the regular cljs.main is not used a lot according to the clojure survey results

๐Ÿš€ 1