Fork me on GitHub

Following up on yesterday's discussion: wrapping my unwanted code in delay helped the compiler eliminate it. Taking a closer look at the JS output, I notices that there are other function calls that the DCE doesn't seem to eliminate, such as the following. Sets:

(def unused-set #{...})
(def unused-atom (atom ...))
Some fn calls:
(defn f [x] (str x "bar")
(def unused-map {:a {:b (f "foo")}})
$f$ = function($x$) {[$str$($x$), "bar"].join("");};
It seems like if possible, helping the Closure compiler eliminate these things could lead to pretty large savings in some circumstances. The emitted JS for some parts of my codebase consists almost entirely of this type of dead code.


@wombawomba that is interesting about delay - but I don't really see anything pressing for us to do here

Ferdinand Beyer15:03:26

@wombawomba — I would be super interested in a short summary of tips how to write code that optimizes for code elimination based on your findings 🙂


by pressing I mean nothing critical wrt. priority - some of these seem like minor patches we'd be happy to take if provided


specifically static sets, and unused atoms - these two cases seem odd


the str map thing - not going to do anything about that


@ferdinand sure, I'll write something up


Hi guys, how do you log with clojurescript?


Was using timbre, but now thinking limiting the unnecessary dependencies. Since Goog ships with cljs, I shall directly use goog


tools.logging is missing in cljs


Google Closure Library has a logging thing - I believe it's been wrapped to be generic by some open source libs?


Are you directly using that library?


have used it directly, and wrapped in some cases, haven't personally found shared logging to be particularly useful

Yosevu Kilonzo19:03:14

Hello, I’m trying to translate some JS to CLJS and running into an error. Does this syntax look correct? Working JavaScript

const processor = unified()
  .use(rehypeReact, { createElement }) // Same error if I comment this line

const file = await processor.process(`* org-mode example\n your text goes here`);

Not working ClojureScript (Uncaught TypeError: Cannot process without Compiler)
(def processor (-> (unified)
                   (.use org-parse)
                   (.use org-to-rehype)
                   (.use rehype-parse)
                   (.use rehype-to-react (clj->js {:createElement createElement})))) ;; I think the problem is here

(go (let [file (<p! (.process processor "* org-mode example\n your text goes here"))] ;; Fails here
      (.log js/console (:result file))))


Depending on what createElement is exactly, maybe replacing (clj->js {...}) with #js {...} will help.


Apart from that, your code looks pretty much what it should be. If that <p! is the only reason you need the go block here then I wouldn't use it at all - IMO better to use promise API, i.e. (.then ... (fn [file] ...)).

🙏 1
Yosevu Kilonzo20:03:41

Thanks @U2FRKM4TW, I did also try #js {...} , same issue.


Maybe the error lies elsewhere then, and not in that particular piece of code.

👍 1

(:result file) should be (.-result file)?


and FWIW don't use go for a single "await". use .then instead. the go will generate substantially more code when this is all you really need.

(-> (.process processor "* org-mode example\n your text goes here")
    (.then (fn [^js file]
             (js/console.log (.-result file)))))

Yosevu Kilonzo15:03:59

Thanks @U05224H0W! That was my initial syntax error. Also, note taken about go There is another issue underlying this one since rehype-react is an ESM only package. I’ll ask in #shadow-cljs.