squint-cljs / vite-plugin-squint

A plugin to compile .cljs source code with squint using vite
1 stars 3 forks source link

Proper HMR Support #5

Closed martinklepsch closed 4 months ago

martinklepsch commented 5 months ago

I don't fully grasp the details yet but I found this code from a haxe vite plugin. Could be that's all thats needed:

https://github.com/ciscoheat/vite-plugin-haxe/blob/2cb147281285ca533b1d21da625718538db5d7a5/src/index.ts#L42-L54

    handleHotUpdate(event) {
      let files: string[];
      if (event.file.endsWith(".hx")) files = Array.from(hxmlFiles.keys());
      else if (!event.file.endsWith(".hxml")) files = [event.file];
      else return;

      files.forEach((hxml) => {
        const module = event.server.moduleGraph.getModuleById(hxml);
        if (module) {
          event.server.reloadModule(module);
        }
      });
    },
brandonstubbs commented 5 months ago

Yeah I think so too. Just need to understand the servers module graph and how these cljs modules that are transformed get registered there. Is this event.file similar to the id that we use in the transformation, is that the id that is used.

martinklepsch commented 5 months ago

Somebody also said this in Discord

I wonder if you might want something more like this then. https://rollupjs.org/plugin-development/#resolveid. Essentially, resolve it to a .js extension id with a query string. Load the correct extension when you see that query string, and transform when you see that query string.

I think that would allow other js transformers to also do their thing, without you having to call other transformers explicitly.

Edit

You need the enforce: 'pre' option set on the plugin for resolveId to work.

brandonstubbs commented 5 months ago

@martinklepsch now that I went through the learning curve that comment makes so much more sense than it did the first time I read it. The vite dev mode also really helped make things clear what was going on within the black box (turned this on in the dev project)

This is resolved in https://github.com/squint-cljs/vite-plugin-squint/pull/13

borkdude commented 4 months ago

Can this be closed since #13 is merged? cc @martinklepsch