Fork me on GitHub
#clojurescript
<
2021-02-23
>
jerger_at_dda17:02:20

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_dda17:02:23

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

p-himik17:02:42

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_dda17:02:34

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

👍 1
borkdude22:02:32

In general this can be done using a macro

poverholt22:02:30

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.

poverholt23:02:16

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

poverholt23:02:16

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.

p-himik07:02:18

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.

p-himik07:02:38

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

thheller07:02:45

using the self-hosted stuff with shadow-cljs requires doing this: https://code.thheller.com/blog/shadow-cljs/2017/10/14/bootstrap-support.html

thheller07:02:19

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

poverholt21:02:50

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
bbss02:02:04

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