squint

borkdude 2024-10-25T10:54:19.310559Z

I've got something browser-repl-ish working The only problem I have is to evaluate stuff that references libraries which are loaded using dynamic import. Normally vite resolves these, but when I use straight up eval this isn't working of course.

🔥 9
m3tti 2024-10-25T11:28:14.279749Z

Oh my god this is so awesome :O

m3tti 2024-10-25T11:28:59.398479Z

I'll add that thing directly to borkweb if it runs this would even change more than it does right now :O

m3tti 2024-10-25T11:31:22.868169Z

What about doing a browser reload?

m3tti 2024-10-25T11:34:49.515659Z

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import i found that which says that

There is also a function-like dynamic import(), which does not require scripts of type="module".

borkdude 2024-10-25T11:35:30.459119Z

that's already being used

m3tti 2024-10-25T11:35:45.995239Z

Ok and doesnt work :(

borkdude 2024-10-25T11:36:28.804429Z

the issue is just that when doing import("squint-cljs/core.js") in the browser, it of course doesn't know where to get this in node_modules on disk since this done by vite

borkdude 2024-10-25T11:36:44.552239Z

but I can probably work my way around this

borkdude 2024-10-25T11:37:04.424149Z

by defining those libraries as globals and then referencing those globals

m3tti 2024-10-25T11:37:26.362759Z

Cant that be done with import maps?

borkdude 2024-10-25T11:38:08.871279Z

with import maps you can resolve names to other urls, but I don't think you can resolve to anything in node_modules probably and then the user would have to insert import maps just to use the REPL

m3tti 2024-10-25T11:39:09.656319Z

what about exposing an endpoint for the static node_modules and than using import maps

borkdude 2024-10-25T11:39:39.194339Z

I suspect vite is already doing something like that, could be worth checking out how it does this

borkdude 2024-10-25T11:40:10.088399Z

but then you're still left with the problem of having to construct an import map

m3tti 2024-10-25T11:41:42.020769Z

So yeah endpoint like localhost:1234/node_modules

m3tti 2024-10-25T11:42:13.960679Z

Yeah thats right with the importmap but couldnt it be constructed just on the beginning?

m3tti 2024-10-25T11:42:32.902149Z

And everytime you wish to add a lib you have to restart?

m3tti 2024-10-25T11:43:02.345959Z

Or do you want it dynamic dynamic meaning everytime someone calls import it loads it directly

borkdude 2024-10-25T11:43:22.486499Z

I don't generate the index.html, it's something the user defines in their vite project. So I think I prefer doing it without import maps first. I'll figure something out

m3tti 2024-10-25T11:43:55.319149Z

Ah ok you still want to rely on vite or a bundler

borkdude 2024-10-25T11:44:18.334289Z

I'm currently using vite for hot reloading and I want to have a REPL in addition to that

m3tti 2024-10-25T11:44:28.996789Z

Than maybe there is a hook or something you could call

m3tti 2024-10-25T11:44:43.583499Z

Or trigger the reloading if someone uses import

m3tti 2024-10-25T11:44:56.841549Z

Just some ideas :D

borkdude 2024-10-25T11:47:11.702789Z

It would be most streamlined if vite could do the evaluation so perhaps I could inject the snippet that is to be evaluated somewhere. I don't know if vite has some API to call "eval" or so

m3tti 2024-10-25T11:48:33.015589Z

https://vite.dev/guide/api-hmr

m3tti 2024-10-25T11:48:38.340619Z

Found that

tianshu 2024-10-25T12:26:56.921899Z

Will it be simpler if run the compiled code on deno? It doesn't have much build tools, and the ways of import seems to be unified.

m3tti 2024-10-25T13:59:25.786169Z

i guess deno or node doesn't matter in that case. You have to hook up into the build tool somehow. If you use deno your options are limited which is true. But also with squint vite is quite a standard

m3tti 2024-10-25T13:59:58.886059Z

its also what most javascript developers use nowadays due to the fact that it uses native js stuff like esm

borkdude 2024-10-25T12:55:46.835459Z

What's the fastest way to get a browser REPL going with shadow-cljs on my system? I want to see how it works (with respect to evaluating the namespace form and libraries)

borkdude 2024-10-25T12:59:24.748129Z

nm, found something here: https://github.com/shadow-cljs/quickstart-browser

tianshu 2024-10-25T13:02:40.887049Z

also npx shadow-cljs browser-repl, it has a builtin server at http://localhost:8000, you don't need a build config.

👍 1
borkdude 2024-10-25T14:53:33.129759Z

Ah, vite rewrites "squint-core/src/core.js" to "/@fs/Users/borkdude/dev/squint/core.js" and if I import that, it works also in the REPL, but only if I could access to how vite needs to rewrite this somehow

m3tti 2024-10-25T14:55:26.551639Z

This @fs isn't that just a library path. So an alias for a path.

borkdude 2024-10-25T14:55:50.318119Z

the vite server routes that to the right file

m3tti 2024-10-25T15:03:21.184429Z

In js this @something are just internal mappings. I assume that vite than searches for this file in a certain location on disck if it gets that. https://www.w3resource.com/npm/npm-scoped-packages.php the feature is called scoped packages

borkdude 2024-10-25T15:05:12.657819Z

well, in this case @fs/ is just a special route of the vite dev server:

curl 

m3tti 2024-10-25T15:08:38.907119Z

Oh but look isnt that the full path on disc after that @fs?

borkdude 2024-10-25T15:08:56.384119Z

yes

borkdude 2024-10-25T15:10:46.515429Z

it is just a convention of vite, a prefix: https://github.com/search?q=repo%3Avitejs%2Fvite%20%40fs&type=code

borkdude 2024-10-25T15:12:28.639669Z

it looks like I might want to call this before I send it to the browser: https://github.com/vitejs/vite/blob/main/packages/vite/src/node/plugins/importAnalysis.ts#L195

borkdude 2024-10-25T15:12:46.269899Z

Perhaps I need to write this stuff as a vite squint-repl plugin or so

m3tti 2024-10-25T15:13:44.050889Z

That looks like the exact thing that you are looking for imho. At least the docu seems legit

m3tti 2024-10-25T15:16:15.467229Z

I guess squint repl plugin i guess or not? This way its directly in squint or? My idea would be doing it as overloaded function or some kind of macro if thats sufficient if you only have to rewrite the import path to this @fs format

borkdude 2024-10-25T15:17:06.469839Z

well, I don't want to include vite stuff directly in squint

m3tti 2024-10-25T15:17:34.152459Z

Ok than its clear to have it as vite plugin :D

m3tti 2024-10-25T15:23:38.838079Z

Btw how do you do the browser repl? Is it more or less "just" an endpoint that gets polled for changes and do an eval if there is a change?

borkdude 2024-10-25T15:28:20.761209Z

no polling, similar setup as scittle nREPL

borkdude 2024-10-25T15:28:58.793709Z

a server runs and listens on a websocket. the browser connects to the websocket. server also runs an nREPL client, editor connects to that

borkdude 2024-10-25T15:29:12.943629Z

so the server is a kind of proxy between nREPL and the websocket

borkdude 2024-10-25T16:00:17.901379Z

I had another idea. If there would exist a plugin that did this kind of thing: https://github.com/vitejs/vite/discussions/18471 then in squint I could just prefix all the imports with that route and be done

borkdude 2024-10-25T16:00:30.363399Z

and/or perhaps I could just write that plugin myself

yogthos 2024-10-25T15:26:09.838169Z

another interesting library from the jotai author https://github.com/pmndrs/valtio

🔥 1
yogthos 2024-10-25T15:26:19.974389Z

looks like this one lets you do reactive stuff with watches 🙂

borkdude 2024-10-25T15:28:00.005009Z

looks a bit like re-frame subscriptions?

yogthos 2024-10-25T17:01:25.604059Z

yeah exactly