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.
Oh my god this is so awesome :O
I'll add that thing directly to borkweb if it runs this would even change more than it does right now :O
What about doing a browser reload?
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".https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import
that's already being used
Ok and doesnt work :(
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
but I can probably work my way around this
by defining those libraries as globals and then referencing those globals
Cant that be done with import maps?
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
what about exposing an endpoint for the static node_modules and than using import maps
I suspect vite is already doing something like that, could be worth checking out how it does this
but then you're still left with the problem of having to construct an import map
So yeah endpoint like localhost:1234/node_modules
Yeah thats right with the importmap but couldnt it be constructed just on the beginning?
And everytime you wish to add a lib you have to restart?
Or do you want it dynamic dynamic meaning everytime someone calls import it loads it directly
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
Ah ok you still want to rely on vite or a bundler
I'm currently using vite for hot reloading and I want to have a REPL in addition to that
Than maybe there is a hook or something you could call
Or trigger the reloading if someone uses import
Just some ideas :D
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
Found that
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.
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
its also what most javascript developers use nowadays due to the fact that it uses native js stuff like esm
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)
nm, found something here: https://github.com/shadow-cljs/quickstart-browser
also npx shadow-cljs browser-repl, it has a builtin server at http://localhost:8000, you don't need a build config.
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
This @fs isn't that just a library path. So an alias for a path.
the vite server routes that to the right file
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
well, in this case @fs/ is just a special route of the vite dev server:
curl Oh but look isnt that the full path on disc after that @fs?
yes
it is just a convention of vite, a prefix: https://github.com/search?q=repo%3Avitejs%2Fvite%20%40fs&type=code
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
Perhaps I need to write this stuff as a vite squint-repl plugin or so
That looks like the exact thing that you are looking for imho. At least the docu seems legit
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
well, I don't want to include vite stuff directly in squint
Ok than its clear to have it as vite plugin :D
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?
no polling, similar setup as scittle nREPL
a server runs and listens on a websocket. the browser connects to the websocket. server also runs an nREPL client, editor connects to that
so the server is a kind of proxy between nREPL and the websocket
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
and/or perhaps I could just write that plugin myself
another interesting library from the jotai author https://github.com/pmndrs/valtio
looks like this one lets you do reactive stuff with watches 🙂
looks a bit like re-frame subscriptions?
yeah exactly