Fork me on GitHub
#squint
<
2023-07-27
>
aiba05:07:57

Is there a way to use squint within a server-side clojure web app that is generating hiccup and wants to add a splash of javascript? I'm currently using scriptjure's js macro but it has bugs and a lot of limitations. Would be amazing to use squint instead.

chromalchemy13:07:35

Ive seen example somewhere. Require squint in file with hiccup. Slurp cljs code to string, and pass it squint complile fn. Place resulting js string value in script tag in page hiccup.

borkdude13:07:59

yeah, let me look up that example

aiba14:07:26

Cool! My 2c: this is a killer use case and would warrant an example on the squint README. I wasn't sure if squint was even possible to run in JVM/clojure land.

borkdude14:07:18

oh yes, let me look it up again (I forgot meanwhile, sorry)

borkdude14:07:35

here is the example with cherry, but squint should work similarly: https://twitter.com/borkdude/status/1555947698578198532

borkdude14:07:48

let me know if you get any problems with this approach

aiba14:07:38

Oh fascinating, yeah this is exactly what I'm looking for. Interesting es-module voodoo I had not seen before. Can't wait to try this out when I'm at my computer later today, will report back. Thank you!

👍 2
borkdude14:07:30

The es module shim might not be necessary anymore, import-maps should be available anywhere now, but back when I tried it, Safari gave some problem

chromalchemy15:07:58

Here are some related squint examples: https://github.com/babashka/nbb/blob/main/examples/squint/example.cljs https://www.reddit.com/r/Clojure/comments/ydqvfd/comment/ittvcu9/

$ clj -Sdeps '{:deps {io.github.squint-cljs/squint {:git/sha "7759ebc409e97d8b67a9ca3cabcfd83d2fcb2d33"}}}'
Clojure 1.11.0-alpha4
user=> (require '[squint.compiler :as sq])
nil 
user=> (sq/compile-string "(defn foo [] (+ 1 2 3))") 
"var foo = function () {\nreturn (1 + 2 + 3);\n}\n;\n\nexport { foo }\n"
`

👍 2
aiba19:07:05

Update: working beautifully, no shim necessary at least on my versions of firefox/safari. Here's a public example repo which you're free to link to from the squint docs if you think it'd be worthwhile: https://github.com/aiba/squint-hiccup-example/blob/master/src/squint_hiccup_example/core.clj

🎉 4
borkdude19:07:41

Nice. Perhaps add a README with how to run it, before I tweet it :)

aiba19:07:09

Would be my pleasure!

aiba19:07:55

My only remaining question is whether I should be using squint or cherry for my use case. Originally I thought squint would be more lightweight, but it looks like the required core library is about the same size as cherry's, so I wonder if I might as well be using cherry? Curious if you have a thought on this.

borkdude19:07:43

how big is squint core vs cherry core?

borkdude19:07:56

I thought squint was much smaller but I could be wrong

borkdude19:07:20

squint core is 22kb, cherry core is over 300kb

aiba19:07:40

i was looking at

aiba19:07:14

is that not the right module map for squint-cljs/core.js

aiba19:07:01

ohhh, it should probably be

aiba19:07:19

ok that does work better / more lightweight for my use case. thank you!

borkdude19:07:48

right, I didn't know cljs_core.js was in the squint package, I'll remove it ;)

👍 2
borkdude19:07:42

yeah, there some old cruft in there that was copied from cherry

aiba20:07:02

OK, I'm happy with this example, I think it's tweetable if you're so inclined: https://github.com/aiba/squint-hiccup-example/ (though open to feedback if you think it could use more explanation or any changes!)

borkdude20:07:33

An example of calling squint from a JVM server process and generating JS on the fly in the response: https://github.com/aiba/squint-hiccup-example/ - thanks @aiba!

😍 6
👏 3
🌶️ 2
sergey.shvets21:07:23

Very cool, thanks! Haven't thought about compiling the code like that.