Fork me on GitHub
#squint
<
2022-12-21
>
dangercoder14:12:36

Are there any examples on how to create and require macros using squint? Or are macros unsupported? My use case is that i want to get rid of js* boilerplate 🙂

dangercoder15:12:29

I keep on running into “mymacro is not defined”

borkdude15:12:57

The way to do this is is place the macros in src/my_macros.cljc

borkdude15:12:13

and then use (:require-macros [my-mymacros :refer [macro]])

borkdude15:12:24

this has changed since earlier versions of cherry / squint

dangercoder15:12:08

Thanks :star-struck:

borkdude15:12:04

let me know if this works for you

1
dangercoder17:12:23

example here, still getting undefined for my macro. Does the macro ns file have to be defined in root src? https://github.com/Dangercoder/svelte-cljs-example/tree/macros/src/lib

borkdude17:12:44

yes, else it's lib.my-macros

borkdude17:12:58

where do you use the macro?

dangercoder17:12:09

ah, thought there were some special rules to how this was done in squint 🙂

borkdude17:12:55

currently it's only possible to add the macros in src , let's say this is the classpath and then every nesting from there yields another segment in the namespace name

borkdude17:12:15

in the future this is going to be more flexible, but this was just a shortcut to get them working quickly

dangercoder17:12:30

I’m trying to import the macros ns which is defined in src/macros.cljc

borkdude17:12:41

yeah, so don't use the string namespace here

borkdude17:12:48

did it work? :)

dangercoder17:12:48

nope still getting “reactive is not defined”

borkdude17:12:05

let me try locally

thanks3 1
borkdude18:12:42

ok, the issue is that compile-string doesn't resolve any macros (yet) but compile-file does... the reason for this is that compile-string is platform agnostic, e.g. in a browser we don't load files

borkdude18:12:06

compile file is in squint.compiler.node

borkdude18:12:15

I'll see if it's possible to use this...

borkdude18:12:51

hmm no, the only exposed function right now is compileString

borkdude18:12:20

What about a hack:

(def $:foo 1)
which outputs:
var $_COLON_foo = 1
and then string/replace this afterwards in the svelte preprocessor, e.g. var \$_COLON_\S+ => $: foo

borkdude18:12:00

If I would expose the node compile function, it would work

borkdude18:12:07

let me see if I can quickly do this

borkdude18:12:49

@UBN9SNVB4 any idea how to return a promise from the svelte preprocessor?

borkdude19:12:51

@UBN9SNVB4 I've got something working, but I get

Error while preprocessing /private/tmp/svelte-cljs-example/src/routes/+page.svelte - Cannot read properties of undefined (reading 'dependencies')
Is it possible to restore your macros branch in a state that's working but without the macro?

borkdude19:12:50

hmm already worked my way there from your master branch

borkdude19:12:00

but still getting this error after I introduce the macro

borkdude19:12:36

got it, I forgot return in the svelte preprocessor ;)

borkdude19:12:26

@UBN9SNVB4 I have a working example here: https://github.com/borkdude/dangercoder-svelte-cljs-example/tree/macros2 You need to check out a branch of squint-cljs/compiler-common (svelte-macros), then compile this with bb build and then change the package.json entry to refer to your local build

borkdude19:12:41

I want to review this a bit more before I decide on the API, but in theory this now works

bananadance 1
borkdude19:12:04

(ns lib.code
  (:require ["../components/Nested.svelte$default" :as Nested])
  (:require-macros [macros :refer [defreactive]]))

(def cats [])

(defreactive answer (* 2 (count cats)))

(defn addCat []
  (let [_new-cat-id (+ 1 (count cats))
	new-cat {:id 1 :name "Danger Cat"}]
    (set! cats (conj! cats new-cat))))

dangercoder20:12:55

Hey thanks for looking into this, exciting! Was afk. I’ll check out your branch and try it out 🙂

dangercoder20:12:48

Another way of doing it is to import the emitted .js file directly as src in the scripts tag 💭, doesn’t utilise the pre-processor though but it’s an acceptable workaround

dangercoder20:12:07

The power of emitting es6 compatible javascript ♨️

borkdude20:12:33

ah yeah, you can do it like that, but I found the preprocessor pretty cool :)

borkdude20:12:40

and you don't have the restarts of the process

borkdude20:12:49

so it's faster feedback cycles

dangercoder21:12:00

I added a file watcher which runs squint compile on any changed cljc/cljs files but I agree, cool to use the pre-processor 🙂

borkdude21:12:48

I guess the preprocessor doesn't watch .cljs files right?

dangercoder21:12:07

Perhaps svelte can be extended though but for now I’m using babashka tasks

borkdude21:12:31

I think squint itself should have a watch mode but if svelte / vite whatever doesn't see that an included js file changed, then it won't help much there

dangercoder21:12:45

> I guess the preprocessor doesn’t watch .cljs files right? I think my previous answer is incorrect. I got Svelte to hot reload code when I changed .cljs files which were referred in <script lang=“cljs”>

dangercoder21:12:03

the reason I wanted the bb file-watcher is when e.g. doing svelte server-side things, e.g. https://github.com/sveltejs/realworld/blob/master/src/routes/login/%2Bpage.server.js +page.server.cljs -> +page.server.js

dangercoder21:12:45

It get’s automatically picked up by Svelte by naming conventions

borkdude17:12:38

@UBN9SNVB4 So I added a Node.js API that supports macros now and you can see it in action here: https://github.com/borkdude/dangercoder-svelte-cljs-example/tree/macros2

borkdude17:12:46

published in 0.0.6

dangercoder20:12:44

👍 thank you!

Aleed05:12:28

@borkdude so compileString via the node API supports macros now? any other gotchas to be aware of?

borkdude09:12:54

@UPH6EL9DH yes, but not that the CLI has always already supported this. macros are expected to be in the src directory (at this moment)

👍 1