Fork me on GitHub
#clojurescript
<
2018-10-21
>
borkdude20:10:07

why does clj -m cljs.main -re node -e '(println "hello")' -e '(.exit js/process 0)' hang?

mfikes21:10:13

Maybe something in this loop fails to realize Node is no longer alive, blocking on some I/O indefinitely https://github.com/clojure/clojurescript/blob/master/src/main/clojure/cljs/repl/node.clj#L91-L110

grav21:10:38

How do I test if I’m on a nodejs runtime?

grav21:10:49

(programatically :-)

grav21:10:19

I’m currently leaning to (exists? js/process) but that’s a bit brittle, and not very explicit

hlolli21:10:41

these tests can be tricky, there's so much of polyfills that can distort the result, in one project at my company I just used this https://www.npmjs.com/package/is-node

Dormo21:10:32

@grav's way, @hlolli's link, and my link are all basically the same

grav21:10:53

Cool, thanks for the pointers. Does seem a bit involving. Maybe I should just explicitly do (exists? js/process.env), since that’s what it’s all about anyway.

hlolli21:10:56

and it's just one line, with 15k downloads/week

mfikes21:10:57

You could look at *target*

mfikes21:10:13

I don’t know it it is meant to be stable / public

mfikes21:10:52

For example, look at the source for array?

grav21:10:06

@mfikes Ah, yes. Seems to work in both Shadow-cljs and Lumo

mfikes21:10:54

Oh, that’s actually a goog-define so if you are using :advanced you can eliminate conditional code that way…

grav21:10:37

I’m using :none, but just to understand, what conditional code would I be able to eliminate?

mfikes21:10:27

@grav If you look at the source for array?, copied here for reference,

(defn ^boolean array?
  "Returns true if x is a JavaScript array."
  [x]
  (if (identical? *target* "nodejs")
    (.isArray js/Array x)
    (instance? js/Array x)))
when this is passed through :advanced since identical? is being used, Google Closure Compiler can strip the code down to just
(.isArray js/Array x)
(This is normal conditional code elimination that you can do using :closure-defines)

grav21:10:13

Ah, cool!

mfikes21:10:34

See the sentence "You can use the variables set in :closure-defines to eliminate parts of your code at compile time (DCE). " at https://clojurescript.org/reference/compiler-options#closure-defines

richiardiandrea22:10:50

Sorry for jumping in, is identical? mandatory for dce? Can an = be used instead?

mfikes21:10:41

And, just saying a nice thing about this is that it can work in library code, and is compatible with :aot-cache, all because this "stripping" is deferred until :advanced processing.

grav21:10:42

Impressive what the Closure compiler can do. I’m probably not appreciating it as much as it deserves 🙂

grav21:10:21

Pretty neat. Hope Lumo will get more attention. I did see that anmonteiro has started working on it again.

mfikes21:10:23

Hah! His talents are needed on the compiler itself. 😁