Fork me on GitHub
#clojurescript
<
2018-10-05
>
sneakypeet07:10:23

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

thheller09:10:59

@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

sneakypeet09:10:43

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 [clojure.java.shell :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"]
                    opt
                    ["-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")
       compile-cljs-str))

;;; USAGE
(compile-cljs
 (fn [a] a)
 (prn 1234))

thheller09:10:42

you can use the cljs.build.api directly from clojure so you don't have to shell out

thheller09:10:00

also cljs.analyzer.api may be useful

sneakypeet10:10:02

šŸ˜˜ This is exactly what I was looking for šŸ˜„ oh well learned some new things. Thank you

dominicm10:10:52

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] java.io.FileNotFoundException: /tmp/hello-world.app/leaflet.pm/leaflet.pm.js (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?

dominicm10:10:10

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?

dominicm10:10:27

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

dominicm10:10:20

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.

mfikes11:10:36

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

grav11:10:55

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

grav11:10:59

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

grav11:10:41

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

grav11:10:07

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

mfikes11:10:06

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

mfikes12:10:11

In Lumo, though, you can have a ā€œfull-fledgedā€ resolve. Just copy planck.core/resolve from Planck.

mfikes12:10:32

Once Lumo is updated to the latest ClojureScript, and has eval, it would be trivial to write (see https://github.com/anmonteiro/lumo/issues/397)

šŸ‘ 4
mfikes12:10:32

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

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

martinklepsch12:10:37

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?

dominicm12:10:15

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

mfikes12:10:23

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

dominicm12:10:57

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

mfikes12:10:47

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

dominicm12:10:07

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

mfikes12:10:37

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

{code:title=src/user.cljs}
(def a 3)
{code}
See https://dev.clojure.org/jira/browse/CLJS-2917 which this example comes from.

mkarp13:10:34

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

dnolen13:10:50

whatā€™s the question?

dominicm13:10:17

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

mkarp15:10:28

@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?

dnolen15:10:29

I donā€™t really understand what youā€™re saying

dnolen15:10:37

you can have multiple source maps, nothing else is required

mkarp17:10:26

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

dnolen15:10:01

Iā€™ve never encountered problem with this myself

dnolen15:10:33

this isnā€™t really a ClojureScript thing - itā€™s how source maps work

JH16:10:25

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.

Jimmy Miller18:10:04

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

JH13:10:12

Appreciate it @U5K8NTHEZ

mkarp17:10:26

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

dnolen18:10:11

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
richiardiandrea19:10:03

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

thheller19:10:28

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
shader21:10:28

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