简体   繁体   中英

Can I conditionally compile clojure / clojurescript?

Is there a way to in clojure / clojurescript to conditionally compile something depending on whether you're compiling to JVM bytecode of Javascript?

I am writing a small game in ClojureScript but want to keep the majority of the code platform neutral so I can convert to Clojure at some point. I also find that compiling in Clojure is better for finding errors in my code.

I have this working fine by having a directory of clj files that cljsbuild converts to cljs using crossovers.

Where I've come unstuck is trying to use core.async in my clj files. This is needed for cljs:

(ns gaz.system
    (:require-macros [cljs.core.async.macros :refer [go]])
    (:require 
        [cljs.core.async]))

While this is needed for clj to work

(ns gaz.system
    (:require
        [core.async ]))

I'd love to have one file with some form conditional require depending on how it's being compiled. Is that possible at all? Con

Have a look at cljx . It let's you prefix s-expressions with eg #+clj or #+cljs to create different code for Clojure and Clojurescript.

Also, though I have not tried it so far, there is lein-dalap which seems to rely on pure, compilable Clojure to generate Clojurescript.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM