Fork me on GitHub

@borkdude with :target :esm shadow-cljs will output actual ESM code. meaning as far as JS is concerned there is actual import * as X from "term-size"; in the code when you do (:require ["term-size" :as x]). regular CLJS does not have such a output mode and at can only output require. import/require acting differently is decided by the host


it may work if you use :target :bundle and run it through webpack but directly in node require just works different than import

💯 2

Makes sense, but I’ve never been able so far to ‘require’ an ES module in NodeJS but I could try to find some tool that transforms the code somehow.


hmm yeah not sure its possible. hmm didn't they discuss allowing js/import in commonjs code? or did that never happen? guess not


are you sure you don't want to switch to commonjs? esm just seems to make your life harder 😛

🙂 2
Michaël Salihi08:09:52

Look like someone fell in the JavaScript Rabbit Hole 😛

🙃 2

@thheller switching to commonjs based requires (based on createRequire) is what I did before but this disallowed using ES Modules from NPM and some libraries (like term-size) only offer their new versions as ESM. That is the only reason I switched to ESM-based :require again (implemented using shadow.esm: compatibility with the wider range of libs in the node ecosystem and this works with both ES modules and CommonJS modules (at the cost of the user adding $default to their imports).


ah right. yeah this interop business is annoying.

Luciano Laratelli15:09:23

Hi everyone, I have an object, #object[laratelli$pages$blog_post] , and I would like to get the blog_post string out of it. I used cljs-bean to accomplish this with some very ugly, fragile code:

> (last (s/split (:displayName (bean (:G__1 (bean (:view (:data @match)))))) "."))
;; => "blog_post"
the match atom is from reitit. is there a better way to do this?


you shouldn't do this at all. the name of a function shouldn't be used for anything since it'll be gone in optimized builds

👀 2

just put more data into the route if you want that? like the name?

Luciano Laratelli15:09:25

got it, I will try that out. thank you @thheller!


I'm trying to use the node ws websocket module in clojurescript, but I think I'm getting stuck with how requires work. The example in the docs is this:

import WebSocket from 'ws';
const ws = new WebSocket('');
which I've translated into this:
(ns foo.core (:require ["ws" :as node-ws]))
(def ws (new node-ws/WebSocket ""))
but I get this error:
Execution error (TypeError) at (<cljs repl>:1).
fluree.db.util.xhttp.node$module$ws.WebSocket is not a constructor


I've definitely required something, since I can see the node-ws object: (js/Object.keys node-ws) ;;=> #js["CONNECTING" "OPEN" "CLOSING" "CLOSED" "createWebSocketStream" "Server" "Receiver" "Sender"]


@winsome the error is probably accurate


(ns foo.core (:require ["ws" :as WebSocket]))


that JS import is naming thing - not a destructuring import


Thanks! That works just as I would expect


When the GC clears an atom, do all watches added with add-watch also get GCed? (Or do I have to manually ensure that remove-watch gets called?)


This prints {:v nil} in Clojure, so I would guess it is the same in ClojureScript:

(let [atom-watcher #(println %)
        my-atom (atom "")
        _ (add-watch my-atom :my-atom atom-watcher)
        weak-watcher-ref (WeakReference. atom-watcher)]
      (Thread/sleep 50)
      (prn {:v (.get weak-watcher-ref)})))
@leif (which suggests you don't need to call remove-watch )


Mmm...yup, looks like, thanks. 🙂