Fork me on GitHub
#clojurescript
<
2024-01-25
>
Luca Panofsky22:01:41

Hello! I am trying to emit a javascript string from clojure using the cljs.compiler.api but i am having issues with the analyzer. Has someone tried to do something similar?

p-himik23:01:04

Is this what you want?

(binding [cljs.env/*compiler* (cljs.env/default-compiler-env)]
  (cljs.closure/compile '(defn f [x] (inc x)) {}))
=> "cljs.user.f = (function cljs$user$f(x){\nreturn (x + (1));\n});\n"

❤️ 1
p-himik23:01:37

Found out how to do it by reading quite useful (comment ...) forms in CLJS. :)

p-himik23:01:24

And with using only the API namespaces:

(let [env (cljs.analyzer.api/empty-env)
      form '(defn f [x] (inc x))
      ast (cljs.analyzer.api/analyze env form)]
  (cljs.compiler.api/emit ast))
=> "cljs.user.f = (function cljs$user$f(x){\nreturn (x + (1));\n});\n"

❤️ 1
hifumi12300:01:29

using the API namespaces is the way to go. emit is the method you want after creating an AST

hifumi12300:01:58

since this is asked in the #C03S1L9DN channel, I want to believe they want to do this in (self-hosted) CLJS, which in case they must use cljs.js namespace to parse CLJS code into an AST, then they may emit

👍 1