在编译.cljs的时候如何在编译时定义目标env?

我想编译浏览器和node.js环境的.cljs文件,以获得服务器端的渲染。 据我所知,没有办法在编译时使用读者macros条件来定义cljs env,例如:

 #?(:clj ...) #?(:cljs ...) 

所以,我不能轻易地告诉编译器在node.js env中处理类似于#?(:cljs-node ...)东西。

我在这里看到的第二个选项是开发一个在编译时定义env的macros文件。 但如何定义目前的构build是针对node.js? 也许,我可以通过一些参数不知何故编译器或得到:target编译器参数?

这里是我的启动文件:

application.cljs.edn:

 {:require [filemporium.client.core] :init-fns [filemporium.client.core/init]} 

application.node.cljs.edn:

 {:require [filemporium.ssr.core] :init-fns [filemporium.ssr.core/-main] :compiler-options {:preamble ["include.js"] :target :nodejs :optimizations :simple}} 

我不知道一个公共API来实现这一点。 但是,你可以在你的macros中使用cljs.env/*compiler* dynamic var来检查在你的:compiler-optionsconfiguration了:target的目标平台(即NodeJS vs browser),并且发出或者禁止包含在macros中的代码:

 (defn- nodejs-target? [] (= :nodejs (get-in @cljs.env/*compiler* [:options :target]))) (defmacro code-for-nodejs [& body] (when (nodejs-target?) `(do ~@body))) (defmacro code-for-browser [& body] (when-not (nodejs-target?) `(do ~@body))) (code-for-nodejs (def my-variable "Compiled for nodejs") (println "Hello from nodejs")) (code-for-browser (def my-variable "Compiled for browser") (println "Hello from browser")) 
Interesting Posts