3
votes

I would like to make my existing Clojure libraries usable from clojurescript as well as create new applications which run in both Clojure and Clojurescript.

I understand Clojure and Clojurescript have their differences, and that there are metaprogramming solutions such as cljx. But, I have limited my Clojure code to a compatible subset of Clojurescript and I would simply like to call clojure from clojurescript (and possibly vice-versa). I have seen this possible in libraries such as core.match, but I am unable to replicate what is done there.

I am currently getting No such namespace, I am not sure if it is because it is not possible, or because my namespacing or directory structure is wrong, or some other reason.

For concreteness, I have a directory structure of the form

src/
  clj/
    clopjys/
      vector.clj
  cljs/
    cljdemo.cljs

I am trying to use vector.clj from within cljdemo.cljs, i.e:

(:require
  [clophys.vector :refer [coords std-uniform-vector]])

My project.clj has the following entries

  ...
  :source-paths ["src/clj"]
  :cljsbuild {
      :builds [{
          ; The path to the top-level ClojureScript source directory:
          :source-paths ["src/cljs"]
   ...
2

2 Answers

2
votes

You can't include clj code in cljs at runtime. The clj files will get included for the macros (which are defined in clj and is probably what you saw in core.match).

The best solution is cljx really, and if your code is actually compatible with clj and cljs then you will have a cljx file that will look exactly like a normal clj file. It is pretty simple and cool, try it out!

2
votes

Since this question has been written, Clojure reader conditionals have appeared in the core language (whereas .cljx is a library).