Fork me on GitHub

Hi, we're creating a lib compiling to jvm & js. If I create a clj-lib I can put some commonly used resource files in my classpath and load them at execution time. Is there a way to achieve a similar behavior in cljs? E.g. load & inline these files at compile time?


Artifacts will be published to clojars, npm & be used in browser.


If you're using shadow-cljs, there's a built-in function for that: If you're using some other build tool, you can just copy the source of that function to your project (check the license first though).


cool, exactly what I looked for. Fortunately we're using shadow-cljs - thanx :-)

👍 1

In general this can be done using a macro


I am having trouble using eval, getting lots of undeclared var warnings, and I THINK the problem is that I have not configured something like the env/default-compiler-env correctly. I think this because if I look at the return of (empty-state), it is very empty, with almost no namespaces.


Shoot. I wasn't finished... In contrast, when I walk through this tutorial (, I see that (empty-state) provides lots of namespaces, including much from cljs.core, and this works for me. But, when I copy the code into my project, it does not work, and I notice (empty-state) has almost nothing in it. The undeclared vars include things like fn, +, -, * and my local vars too. E.g. the eval of the read-string of "(fn[x] (+ x 2))" triggers several undeclared vars. This is the code from post that I put in my project...

(ns cljs-eval-example.core
  (:require ...
            [ :refer [read-string]]
            [cljs.js :refer [empty-state eval js-eval]]))

(defn eval-str [s]
  (eval (empty-state)
        (read-string s)
        {:eval       js-eval
         :source-map true
         :context    :expr}
        (fn [result] result)))


Shoot, again. I just habitually hit <Return> and fire my message before I am done.... Anyhow, the difference in tutorial code vs mine includes fact that it was started with "lein new reagent" and mine with luminus re-frame. I start the REPL in the tutorial with figwheel whereas I am using shadow-cljs. Any tips on what is involved in getting (empty-state) to return an environment that can support evaluating functions that do basic math like +,-,*,/,log,abs,etc. would be very appreciated.


Evaluating CLJS in a browser requires a build with self-hosting capabilities. AFAIK, even these days it's still a finicky and not that well documented thing - not that many need that functionality, especially given that it inflates the bundle size. With that being said, it's no surprise that you can't get it working when you use a different template, a different build tool, and a different way to start REPL.


Also, that guide is more than 5 years old. Some things might've changed.


using the self-hosted stuff with shadow-cljs requires doing this:


the stuff described in the other guide will not work with shadow-cljs


Thanks @U05224H0W and @U2FRKM4TW! This advice will save me lots of wasted time on my misdirected ideas and points me towards several ideas. I am not sure which is best yet, but I think the self-hosted ClojureScript compiler (great job on the blog!) will work, as would a request to back-end Clojure eval. I also remembered I've used instaparse withe some macro processing. That might work fine for supporting a small set of math operations. Thanks again!

👍 1

another option might be using sci (