figwheel-main

Nim Sadeh 2023-06-20T16:54:08.033029Z

hello, I am new to figwheel/clojurescript, though I have a background in FP. I have a web app running on figwheel-main and I’d like to read a JSON file into ClojureScript. what’s the best way to do that? I found https://clojureverse.org/t/using-none-code-resources-in-cljs-builds/3745 for shadow-cljs, is there an analog in Figwheel? If not, what’s the simplest way to do this? I am open to putting it into a JS file as an object and reading it from there but I don’t know how

athomasoriginal 2023-06-20T19:09:48.364829Z

Is the JSON file for your app or to configure figwheel-main?

Nim Sadeh 2023-06-20T19:10:08.539969Z

It’s for my app, it’s test/development data

athomasoriginal 2023-06-20T19:11:46.430399Z

Cool. My approach has been to write a macro which reads in the file. For example, I have a markdown file which I read in, transform to hiccup and then render in my app. I do a similar thing with SVG files.

Nim Sadeh 2023-06-20T19:12:40.490499Z

Got it, so I can basically copy the macro from shadow-cljs then? tbh I am new to Clojure and have never actually written a macro

athomasoriginal 2023-06-20T19:13:19.808709Z

Yep. For sure. You can even configure your macro to run when you make changes to your JSON file 🙂

Nim Sadeh 2023-06-20T19:14:13.120869Z

It would also take me about 15-20 minutes to move my code to shadow-cljs tbh - probably less time than to copy and test the macro laughcry

athomasoriginal 2023-06-20T19:14:46.731089Z

Maybe. But the learning you get is pretty great.

👍 1
Nim Sadeh 2023-06-21T04:19:28.365689Z

tried my best and spent the whole evening on this but couldn't get the macro to work 😞

athomasoriginal 2023-06-21T12:15:18.171299Z

What does the code look like? What issues are you running into?

Nim Sadeh 2023-06-21T14:40:34.068349Z

Making a note to respond tonight - this stuff is on my private machine at home and I am at work

Nim Sadeh 2023-06-22T00:15:27.740559Z

This is my code:

(ns nim.slurp     (:require [ :as io]
                            [clojure.string :as str]
                            [cljs.env :as env])
    (:import [java.nio.file Paths]
             [ File]))

(def normalize-name
  (if (= File/separatorChar \/)
    identity
    (fn [^String name]
      (str/replace name File/separatorChar \/))))

(defn ns->path [name]
  (-> name
      (str)
      (str/replace #"\." "/")
      (str/replace #"-" "_")))

(defn ns->cljs-filename [name]
  (-> name
      (ns->path)
      (str ".cljs")))

(defn slurp-resource [environment path]
  (let [current-ns (-> environment :ns :name)
        path (let [parent (-> (Paths/get current-ns (into-array String []))
                              (.getParent))]
               (when-not parent
                 (throw (Exception. (str "Could not resolve path " path " from " current-ns))))
               (-> parent
                   (.resolve path)
                   (.normalize)
                   (.toString)
                   (normalize-name)))
        rc (io/resource path)]
    (when-not rc
      (throw (.Exception "Could not resolve RC")))
    (slurp rc)))

(defmacro inline
  [path]
  (when-not (string? path)
    (throw (.Exception (str "shadow.resource/inline must be called with a literal string argument"))))
  (slurp-resource &env path))
when I load the file I get
clj꞉nim.slurp꞉> 
; Evaluating file: slurp.clj
; Unexpected error (NullPointerException) macroexpanding inline at (slurp.clj:46:1).
; null
Frankly I think this is a bit too much for me at this stage of learning ClojureScript. I'd rather switch to shadow-cljs and use the built in functionality

athomasoriginal 2023-06-22T04:03:36.774069Z

What does your call-site look like?