Fork me on GitHub

Is it possible to read and evaluate a clojurescript string from a clojure context (.clj file instead of .cljs). I know you could do something like this with clojurescript. Is there an equivalent for clojure. Is it even possible?

(ns test
            [ :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)))


@sneakypeet you could probably try the nashorn REPL but its not exactly setup to be used that way. so yes it is possible but probably requires some amount of setup/effort


So I managed by outputting the js to a file, running clj tools using sh and then slurping the result. Not great but works for my needs 1. assuming clj tools installed 2. make a basic clj project at resouces/inline-js (basically just has the deps.edn with cljs dep) 3.

(ns sneakycode.cljs
  (:require [ :as sh]
            [clojure.string :as string]))

(def source-proj "resources/inline-cljs")
(def source-path (str source-proj "/src/tojs/core.cljs"))
(def dest-path (str source-proj "/out/main.js"))
(def out-ns "tojs.core")

(defn- add-ns [s]
  (str "(ns " out-ns ")\n\n" s))

(defn compile-cljs-str
  ([s] (compile-cljs-str "--optimizations advanced" s))
  ([opt-str s]
   (let [code (add-ns s)]
     (spit source-path code)
     (let [opt (string/split opt-str #"\h")
           options (concat
                    ["clj" "--main" "cljs.main"]
                    ["-c" out-ns :dir source-proj])
           compile-result (apply sh/sh options)]
       (when-not (= 0 (:exit compile-result))
         (throw (ex-info "CLJS Compilation Failed" compile-result))))
     (slurp dest-path))))

(defmacro compile-cljs [& body]
  (->> body
       (map str)
       (string/join "\n\n")

 (fn [a] a)
 (prn 1234))


you can use the directly from clojure so you don't have to shell out


also cljs.analyzer.api may be useful


😘 This is exactly what I was looking for 😄 oh well learned some new things. Thank you


I'm having some issues with deps.cljs. I have a :local/root dependency with a deps.cljs. When I use a classpath relative path to the .js files, I get the error: [Figwheel:SEVERE] /tmp/ (No such file or directory) (note that it's not looking in src!). Are deps.cljs on the filesystem given different treatment to those found in jars?


Actually, I've been double stupid. It's not even looking in the right directory at all. It should be looking at a folder in /home/dominic/src/... but it isn't. So it's searching relative to the cwd only?


It would appear that git clojurescript from master doesn't have this problem.


This appears to be because clojurescript via git behaves differently to cljs via maven. I don't really know how to move forward from that.


@dominicm You built ClojureScript master (to confirm that the difference is git vs. maven, as opposed to 1.10.339 vs. master)?


is it possible to resolve a function var given a string in ClojureScript?


(resolve 'my-ns/my-fn) works, but (resolve (symbol "my-ns/my-fn")) causes an exception


Which is weird to me, since (= 'my-ns/my-fn (symbol "my-ns/my-fn")) is true


Using self-hosted cljs with Lumo. Maybe that has something to say


@grav So long as the symbol can be calculated at compile time, you could write a macro to call resolve


In Lumo, though, you can have a “full-fledged” resolve. Just copy planck.core/resolve from Planck.


Once Lumo is updated to the latest ClojureScript, and has eval, it would be trivial to write (see

👍 4

cljs.user=> (let [sym (symbol "cljs.core/filter")] (eval `(~'var ~sym)))

Chris Dewar-English12:10:01

I have a question about boot. After a production build should the target folder contain a main.out folder alongside main.js? I thought the final artefact should just be a main.js file?....


If you use :optimizations :advanced the main.js file is all that's needed but if you use no optimizations the files are actually not bundled and loaded separately in the browser

Chris Dewar-English12:10:08

Ok thanks. Am I right in thinking I need to use :foreign-libs to allow me to use :advanced with 3rd party libraries?


@mfikes I used git pointed at b1ade48e21f9e7f78d9db74559ce4dd5846d0c94 (tagged 1.10.339) and saw this behavior. When I use a mvn/version of 1.10.339 I don't see it.


@dominicm Thanks. That should work. If you are in a position to create a minimal repro, I'd suggest filing a JIRA.


Wait, sorry. I said that the wrong way round @mfikes. I don't see it with git. I do see it with maven.


(The goal is that there should really be no fundamental difference between Git and Maven, but perhaps there is something subtle going on.)


@mfikes how is it best to upload this? I expect there's 4 files at least.


@dominicm If the files are small enough, I'd use something like this in the description

(def a 3)
See which this example comes from.


Has anyone dealt with source maps when using foreign-libs?


what’s the question?


I may have just had an invalid js file. Face meet palm. I'm rather confused now.


@dnolen I’d like to be able to symbolicate stack traces that include code paths from a foreign-lib file. I’ll try to explain what I mean: • I reference a minified js file as a foreign-lib. • This js file is included in the cljs output file when using simple optimisations. • This js file has a source map produced next to it as a side product of minification. • If a stack trace includes code paths from the js file, it’s not possible to use cljs file’s source map to symbolicate the code path. • Is there a way to somehow include foreign lib’s source map in the source map for the cljs output file?


I don’t really understand what you’re saying


you can have multiple source maps, nothing else is required


Correct. My problem is that I have multiple source maps for a single JavaScript file. One comes from the ClojureScript compiler’s output, and the other one comes with a minified foreign-lib


I’ve never encountered problem with this myself


this isn’t really a ClojureScript thing - it’s how source maps work


Does anybody have any recommendations for how to open a pdf returned by a get call in a new window? That caviot is that we need to send a *tolkien in the request header*. Libraries being used [cljs-http "0.1.45"] Without the header, this is done easily with just (.open js/window "uri-to-pdf), integrating the header is the tricky part.


You should be able to do the same method as long as you make the pdf a blob


Appreciate it @U5K8NTHEZ


Correct. My problem is that I have multiple source maps for a single JavaScript file. One comes from the ClojureScript compiler’s output, and the other one comes with a minified foreign-lib


right what you want just isn’t supported - it’s not a much requested feature so I doubt we’ll spend much time on that - though if somebody wants to work on that - go for it

👍 4

I think the Google Closure compiler has an option to kind of "merge" source maps


it does support input source maps yes but those only apply to code it actually processes which it doesn't for foreign-libs. you could however generate an index source map.

👍 4

does anyone here use clojure/clojurescript with couchdb? clutch seems really out of date, and I'm running into strange errors with it that could be resulting from incompatibilities with newer clj/cljs versions...