1
votes

I'm struggling a little with the cljs API. I have a .cljs file containing a series of forms on my classpath and want to obtain the corresponding javascript as a string, without having to write to any kind of filesystem. I can get it through the analyzer, but don't know how to get the javascript emitted:

(with-core-cljs nil (fn []
                        (analyze-file "us/quartyard/wat.cljs")
                        (println(all-ns)) ;; prints essentially what I expect.
                        (now what???)))

I've read the docs and it is by no means obvious what to call after analyze-file.

Edit (adding more information):

There is a function cljs.compiler.api/emit which return some js given an AST node. But the only way I have found to get an AST node is to call (analyze form) where form is a single cljs form.

To analyze a complete file defining a namespace with many forms, the only obvious way use to call analyze-file, which returns nothing and adds the AST for the namespace to the ambient compiler state. But it's not clear how to get that AST back out.

For example:

(with-core-cljs nil (fn []
                        (analyze-file "us/quartyard/wat.cljs")
                        (emit (analyze (empty-env) '(println "ok"))))

emits a single line of js that prints "ok", but nothing from my file.

Meanwhile,

(with-core-cljs nil (fn []
                        (analyze-file "us/quartyard/wat.cljs")
                        (find-ns 'us.quartyard.wat)))

returns nil, rather than an AST for my namespace.

So what am I missing?

1
Try (cljs.compiler.api/emit) - ClojureMostly
@ClojureMostly: That was my first thought, but I don't see how to make it work. I've added additional context/explanation to my question. - mac01021

1 Answers

0
votes

You should be able to do it this way:

(cljs.build.api/compile {} "us/quartyard/wat.cljs")