shadow-cljs

https://github.com/thheller/shadow-cljs | https://github.com/sponsors/thheller | https://www.patreon.com/thheller
thheller 2021-06-15T06:47:12.069500Z

@huxley and what was it?

2021-06-15T06:48:26.069800Z

I was working in a new file that was not yet required anywhere 😜

2021-06-15T06:49:26.070Z

and date-fns-tz has not been used anywhere else

2021-06-15T06:50:14.070200Z

the main cause of problems usually sits in front of the monitor

thheller 2021-06-15T06:53:29.070700Z

hmm well normally that shouldn't be a problem but I guess there is an issue with electron and eval then

thheller 2021-06-15T06:54:05.071100Z

(assuming you just load-file or require the file it should be fine)

2021-06-15T07:53:20.071300Z

as I did a require in another ns which is used in the application and is compiled, then suddenly everything started to work everywhere and in every ns

thheller 2021-06-15T08:04:07.071800Z

do you have any custom :js-options or so in your build config? anything electron related?

thheller 2021-06-15T08:04:33.072400Z

normally this should just work (and did in your and my tests)

West 2021-06-15T13:12:09.073300Z

Is it possible to call a node script from cljs? If so, how do I configure my shadow-cljs.edn so that I can call it?

thheller 2021-06-15T13:12:46.073700Z

what do you mean by "call a node script from cljs"?

West 2021-06-15T13:13:45.074300Z

Nevermind, I finally figured out the classpath business.

(ns app.data
  (:require [clojure.edn :as edn]
            [shadow.resource]
            ["/org_processor" :as org]
            ))

West 2021-06-15T13:14:20.075Z

deps.edn:

{:paths ["src" "scripts/js"]
...}

West 2021-06-15T13:14:56.076Z

But now I get a weird error.

[:app] Compiling ...
[:app] Build failure:
Closure compilation failed with 1 errors
--- org_processor.js:3
Parse error. '}' expected

thheller 2021-06-15T13:14:57.076200Z

ah you mean using a JS file in your build. by "call a node script" I assuming you mean run node some-script.js or so 😛

thheller 2021-06-15T13:15:41.077100Z

whats weird about that error? seems to be invalid js?

West 2021-06-15T13:15:47.077300Z

yeah, I want to use a node script as it interacts with my local filesystem, and shadow.resource just doesn’t suit my needs.

West 2021-06-15T13:16:05.077700Z

The error is weird because it’s output from a compiled cljs file.

West 2021-06-15T13:16:17.078Z

It should be valid

thheller 2021-06-15T13:16:19.078100Z

you are not making much sense to me here

thheller 2021-06-15T13:16:49.078700Z

its output from a compiled cljs file? what does that mean?

thheller 2021-06-15T13:17:14.079500Z

please describe what you are trying to do, if possible with actual example

West 2021-06-15T13:17:38.080200Z

I’m trying to use a library that turns org-mode documents into html.

West 2021-06-15T13:19:03.081700Z

Here’s my script.

(ns scripts.org-processor
  (:require ["unified" :as unified]
            ["rehype-stringify" :as html]
            ["uniorg-parse" :as uniorg]
            ["uniorg-rehype" :as rehype]
            ["uniorg-extract-keywords" :refer (extractKeywords)]
            [cljs-node-io.core :as io :refer [slurp spit file-seq]]
            ))

(def processor
  (->
   (unified)
   (.use uniorg)
   (.use extractKeywords)
   (.use rehype)
   (.use html)))

#_(def org-string "
#+title: Lorem Ipsum
#+subtitle: stuff test
#+date: 2021-04-15
#+tags: test
#+author: Christian Westrom
#+id: test-1

* Here's some stuff
  * Here's more stuff
  - more stuff")

(defn process-org
  [org]
  (.processSync processor org))

(defn org->html
  [org-string]
  (.-contents (process-org org-string)))

(defn org->data
  [org-string]
  (js->clj (.-data (process-org org-string))
  :keywordize-keys true))

(defn blog-post
  [org-string]
  {:content (org->html org-string)
   :data (org->data org-string)})

(defn ^:export all-posts
  [path]
  (js/console.log (map #(blog-post (slurp %)) (rest (file-seq path)))))

West 2021-06-15T13:19:41.082400Z

Now the function all-posts just outputs to the console for now, but I want to call it from my reagent site.

thheller 2021-06-15T13:20:27.082700Z

ok then requiring the file is that what you want here

thheller 2021-06-15T13:20:44.083100Z

I'd recommend to have that script output files to a certain location

thheller 2021-06-15T13:20:54.083400Z

and either loading those files dynamically via ajax at runtime

thheller 2021-06-15T13:21:01.083700Z

or including then via shadow.resource

thheller 2021-06-15T13:21:26.084600Z

I do NOT recommend trying to run that script as part of the compilation. that'll just end up with unnecessary avoidable problems all over the place

West 2021-06-15T13:21:43.085Z

Looks like I need ajax then lol Because there’s no equivalent to file-seq in shadow-resource.

thheller 2021-06-15T13:22:08.085300Z

well with a macro you could trivially write that yourself

West 2021-06-15T13:22:24.085500Z

how?

thheller 2021-06-15T13:23:20.087100Z

I wouldn't recommend doing that though so you'll have to figure that out yourself 😉

thheller 2021-06-15T13:23:39.087700Z

I mean you have the script to generate the stuff right there

thheller 2021-06-15T13:23:59.088500Z

so you could have that generate a single EDN or JSON file and just shadow.resource that

thheller 2021-06-15T13:24:32.089Z

but I would really recommend loading them dynamically

thheller 2021-06-15T13:24:46.089600Z

unless you enjoy recompiling your entire code every time you make a change in the org mode files

West 2021-06-15T13:25:06.090100Z

Ok cool, any good libraries for doing ajax in clojurescript?

thheller 2021-06-15T13:25:29.090500Z

if you don't care about really old browser then just use js/fetch

West 2021-06-15T13:25:49.090700Z

👍

West 2021-06-15T13:25:57.090900Z

Thanks a bunch!