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: https://clojureverse.org/t/using-none-code-resources-in-cljs-builds/3745 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 :-)
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 (https://yogthos.net/posts/2015-11-12-ClojureScript-Eval.html), 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 http://yogthos.net post that I put in my project...
(ns cljs-eval-example.core
(:require ...
[cljs.tools.reader :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.
Thanks @thheller and @p-himik! 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!
another option might be using sci (https://github.com/borkdude/sci)