Repository navigation
Conversation
| if (svelteRequest) { | ||
| const { modules } = ctx; | ||
| const svelteModules = []; | ||
| const svelteJsModules = []; |
There was a problem hiding this comment.
I'm not sure if svelteJsModules is a good variable name or not.
There was a problem hiding this comment.
its ok. technically could be a little confusing with .svelte.js files but they are not processed by the hot-update plugin at all so its fine.
an alternative would be to simply sort svelteModules instead of throwing them into 2 different buckets first. the number of modules in here is super low, usually its exactly 2, so perf really isn't an issue
There was a problem hiding this comment.
an alternative would be to simply sort svelteModules
I feel that the following code is a bit hard to read.
svelteModules.sort((a, b) => {
const numA = SVELTE_VIRTUAL_STYLE_ID_REGEX.test(a.id || '') ? 2 : 1;
const numB = SVELTE_VIRTUAL_STYLE_ID_REGEX.test(b.id || '') ? 2 : 1;
return numA - numB
})There was a problem hiding this comment.
you don't need a custom comparator in this case, the css module id is constructed with a suffix, so svelteModules.sort() ensures that Foo.svelte?... comes after Foo.svelte.
edit: do'h, you need to map by id
svelteModules.sort((a,b)=> a.id > b.id ? 1 : a.id===b.id ? 0 : -1)
|
this is really strange. Due to the fact that the virutal style module is imported by the js module iit should always come afterwards in |
|
regarding ssr tests. the |
I created a repo for easier debugging of this issue: https://github.com/issueset/vite-plugin-svelte-hmr-issue.git There are three files under Notice that for |
I submitted another PR to implement this behavior #1258 Based on my test, it works fine for all three cases shown in my previous comment |
|
closing in favor of #1258 |
During the development of withastro/astro#14821, I noticed that sometimes HMR doesn't work. After some debugging, here is the reason in my opinion:
<style>tag in a.sveltefile changes,hotUpdate.handleris called.ctx.modulescontains two modules:Counter.svelte?svelte&type=style&lang.cssandCounter.svelte. Notice the order here.await transformRequest(mod.url)is called first forCounter.svelte?svelte&type=style&lang.css.loadCompiledCss'sload.handlermethod is called, and a cached CSS is returned.Counter.svelte?svelte&type=style&lang.cssis from the previous cache, the moduleCounter.svelte?svelte&type=style&lang.cssis marked as not affected.await transformRequest(mod.url)is called forCounter.svelte.transform.handlermethod is called. In this step, the.sveltefile is actually handled by the Svelte compiler, and the cached CSS is updated..sveltefile doesn't change, the moduleCounter.svelteis marked as not affected too.This PR fixes the issue by processing
Counter.sveltefirst, which triggers the Svelte compiler and updates the cached CSS. Only after that, it processesCounter.svelte?svelte&type=style&lang.css.I don't know how to test this patch in this pull request, though. While running hmr.spec.ts, the
ctx.modulesare in the order [HmrTest.svelte,HmrTest.svelte?svelte&type=style&lang.css]. This order won't cause the HMR issue I described above. I suspect that the different order ofctx.moduleshas something to do with the fact thatastrois an SSR framework.