Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 546
Gary Liddon Avatar asked Dec 07 '13 09:12

Gary Liddon


1 Answers

Have a look at cljx. It let's you prefix s-expressions with e.g. #+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.

like image 87
xsc Avatar answered Oct 17 '22 17:10

xsc