clojurescript

ClojureScript, a dialect of Clojure that compiles to JavaScript http://clojurescript.org | Currently at 1.10.879
jerger_at_dda 2021-02-23T17:50:20.068800Z

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?

jerger_at_dda 2021-02-23T17:50:23.069Z

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

p-himik 2021-02-23T17:54:42.069100Z

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).

jerger_at_dda 2021-02-23T17:56:34.069400Z

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

👍 1
borkdude 2021-02-23T22:32:32.070Z

In general this can be done using a macro

2021-02-23T22:58:30.072300Z

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.

2021-02-23T23:06:16.076500Z

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)))

2021-02-23T23:12:16.080400Z

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.

2021-02-24T21:22:50.081800Z

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!

👍 1
2021-02-25T02:34:04.082300Z

another option might be using sci (https://github.com/borkdude/sci)