Clojurescript macros: using node api at compile time

I am writing a cli script with lumo and want the following macro but using readFileSync

from nodejs.

(defmacro compile-time-slurp [path]
  ;; slurp is not defined in self hosted cljs
  (slurp path))

      

Is it possible?

EDIT: To be clearer, this is in a self-serving clojurescript where the function is slurp

not available during macro expansion.

+3


source to share


1 answer


ClojureScript macros are written in the Clojure language and have approximately this life cycle:

  • jvm loads Clojure runtime, prepares and a bunch of other stuff.
  • the macro is compiled
  • the macro runs and creates a new ClojureScript expression
  • if this expression is again a macro loop.


This excludes all the parts done in the rest of the ClojureScript compiler (which is the most), so we can focus on the fact that ClojureScript macros only have access to parts of Clojure that are accessible from the JVM and not a node while they are running , the form returned by this macro is which will become part of the finished ClojureScript program has access to the node API like readFileSync.

In short, your macro should return a readFileSync call, not read the file while the macro is running. If your code really needs to read some files when evaluating a macro because, for example, they contain code to output or whatever, then you will need to use the Clojure form to read files like the call slurp

you have above.

+4


source







All Articles