Fork me on GitHub
#clojurescript
<
2021-05-08
>
weiqiu02:05:22

Hi, is it possible to mix ClojureScript and JavaScript in single project and compile them to a single .js file?

weiqiu02:05:23

The customer told us they can't write cljs and we are considering js plugins for them.

t-cool06:05:59

#shadow-cljs makes it possible I guess.

❤️ 3
weiqiu06:05:25

cool, found a experimental feature in shadow-cljs https://shadow-cljs.github.io/docs/UsersGuide.html#classpath-js

🎉 3
niwinz08:05:56

on penpot, we have many js files among the cljs files

niwinz08:05:02

shadow-cljs is awesome for it

niwinz08:05:20

you have two options: use the ES Modules format or google closure module format, the advantage of using gclosure module format, is that is 100% transparent to cljs and you can (:require it like any other cljs namespace

niwinz08:05:16

I can provide examples if you need 😉

weiqiu08:05:55

sounds awesome! could you share it to us?

niwinz08:05:42

this is an example usin ES module, we integrate with draft editor here and prefer doing it on JS, we just import it using the ["./text_editor_impl.js" :as impl]

niwinz08:05:05

is using gclosure module format

niwinz08:05:51

and we require it like any other cljs namespace [app.util.quadtree :as qdt]

niwinz08:05:48

the second format works in both shadow-cljs and regular cljs compiler, the first one (ES module format) is shadow-cljs only

niwinz08:05:05

each one has its own tradeoffs and advantatges

weiqiu09:05:40

It helps a lot, thanks for your kind sharing!

niwinz09:05:28

feel free to ask if you have more doubts 😉

lilactown14:05:33

https://github.com/hashql/hashql I think this is kind of clever, and something we could do in Clojure(Script) too

lilactown14:05:47

reading the source, it's a library that allows you to write a SQL query in your source, and at compile time will generate a hash for the client and a lookup on the server side for that hash

p-himik15:05:05

With macros, it should be possible to do that with anything, not just SQL queries. But I'm not sure if it's a good thing to do - both for SQL and for any other things.

vinurs15:05:14

hello, is there any lib that i can read and write excel in cljs in electron

p-himik16:05:53

If you don't find anything specifically for CLJS, you can search for a JS library and just use it via interop.

vinurs16:05:24

yes, i found this lib, https://github.com/exceljs/exceljs#reading-xlsx, but i don't know how to interop this in cljs

vinurs16:05:03

(let [workbook (Excel/Workbook.)
      file (.-xlsx workbook)]
  (.then (js/Promise.resolve (.readFile file "/Users/vinurs/Downloads/test.xlsx"))
         #(do
            (let [worksheet (. % getWorksheet 1)]
              (prn worksheet)
              (.  worksheet eachRow (fn [row rownumber]
                                      (prn row)))
              )
            ))
  )

vinurs16:05:17

i try this for prn each row, but it seems cant

p-himik16:05:38

Why did you use Promise.resolve there?

p-himik16:05:05

The original JS examples on that page don't use it.

vinurs16:05:36

because it

await workbook.xlsx.readFile(filename);

p-himik16:05:28

It's just .then, without any extra explicit promise creation.

p-himik16:05:41

An async JS function already returns a promise - use interop with it, that's it.

p-himik16:05:55

So, in your code above, just remove js/Promise.resolve.

vinurs16:05:00

like this ?

(let [workbook (Excel/Workbook.)
      file (.-xlsx workbook)]
  (.then (.readFile file "/Users/vinurs/Downloads/test.xlsx")
         #(do
            (let [worksheet (. % getWorksheet 1)]
              (prn worksheet)
              (prn (. worksheet getRow 1))
              ;; const row = worksheet.getRow(5);
              ;; (.  worksheet eachRow (fn [row rownumber]
              ;;                         (prn row)))
              ))))

vinurs16:05:02

now i want to prn each row

vinurs16:05:33

how can i do this?

p-himik16:05:41

Just uncomment the last two commented lines?

p-himik16:05:08

Ah, and wrap eachRow in () along with the function itself. Actually, that's not needed. I never use . so I don't remember its syntax.

vinurs16:05:23

(let [workbook (Excel/Workbook.)
      file (.-xlsx workbook)]
  (.then (.readFile file "/Users/vinurs/Downloads/test.xlsx")
         #(do
            (let [worksheet (. % getWorksheet 1)]
              (prn worksheet)
              (prn (. worksheet getRow 1))
              (.  worksheet (eachRow (fn [row rownumber]
                                      (prn row))))
              ))))

p-himik16:05:47

Does it not work?

vinurs16:05:56

nothing is print

p-himik16:05:42

No idea, I would try coming up with a JS example first for that particular spreadsheet. Then I'd try debugging.

vinurs16:05:33

(let [workbook (Excel/Workbook.)
      file (.-xlsx workbook)]
  (.then (.readFile file "/Users/vinurs/Downloads/test.xlsx")
         #(do
            (prn "hello ")

            ;; (let [worksheet (. % getWorksheet 1)]
            ;;   ;; (prn worksheet)
            ;;   ;; (prn (. worksheet getRow 1))
            ;;   ;; (.  worksheet (eachRow (fn [row rownumber]
            ;;   ;;                         (prn row))))
            ;;   )
            )))

vinurs16:05:43

code like this it doesn't output hello

p-himik16:05:23

How do you run the code?

vinurs16:05:51

in emacs, connect to shadow-cljs repl

p-himik16:05:41

Evaluate this:

(let [p (js/Promise. (fn [resolve _] (js/setTimeout #(resolve "hello") 500)))]
  (.then p js/console.log))
Does it output "hello"?

vinurs16:05:15

(prn "hello")

vinurs16:05:18

this can output

p-himik16:05:39

Your REPL setup doesn't catch print statement in async functions, that's it. Maybe asking in #shadow-cljs will help.

vinurs16:05:32

ok, thank u very much

👍 3
vinurs17:05:01

sorry, it output in the console, not in repl, i didn't see it

👍 3
Emi Gal21:05:45

I'm building a react native app using shadow-cljs, expo, re-frame, reagent. Struggling a little bit to make react-navigation work properly, and I was wondering if anyone can point me to best practices on how to implement it in the most idiomatic clojure way?

sova-soars-the-sora23:05:50

I recall something called secretary ..

Emi Gal01:05:32

Thank you! Unfortunately I don't think that works with react native, but found a solution here: https://hackmd.io/@byc70E6fQy67hPMN0WM9_A/rJilnJxE8#7-Navigation