Fork me on GitHub
#clojurescript
<
2021-09-01
>
thheller05:09:39

@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

thheller05:09:32

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

💯 2
borkdude06:09:59

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.

thheller07:09:31

hmm yeah not sure its possible. hmm didn't they discuss allowing js/import in commonjs code? or did that never happen? guess not https://github.com/nodejs/modules/issues/454

thheller07:09:53

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
borkdude11:09:35

@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).

thheller13:09:50

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?

thheller15:09:06

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
thheller15:09:21

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!

winsome16:09:41

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

winsome16:09:27

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"]

dnolen16:09:28

@winsome the error is probably accurate

dnolen16:09:00

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

dnolen16:09:28

that JS import is naming thing - not a destructuring import

winsome16:09:26

Thanks! That works just as I would expect

leif19:09:52

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?)

isak20:09:50

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)]
    (future
      (Thread/sleep 50)
      (System/gc)
      (prn {:v (.get weak-watcher-ref)})))
@leif (which suggests you don't need to call remove-watch )

leif20:09:03

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