Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions packages/vite-plugin-svelte/src/plugins/hot-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,29 @@ export function hotUpdate(api) {
const svelteRequest = idParser(ctx.file, false, ctx.timestamp);
if (svelteRequest) {
const { modules } = ctx;
const svelteModules = [];
const svelteJsModules = [];

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if svelteJsModules is a good variable name or not.

@ghost ghost Jan 7, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
})

@ghost ghost Jan 7, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

const svelteCssModules = [];
const nonSvelteModules = [];
for (const mod of modules) {
if (transformResultCache.has(mod.id)) {
svelteModules.push(mod);
if (mod.id && SVELTE_VIRTUAL_STYLE_ID_REGEX.test(mod.id)) {
svelteCssModules.push(mod);
} else {
svelteJsModules.push(mod);
}
} else {
nonSvelteModules.push(mod);
}
}

// Always process Svelte components first, then styles. This
// ensures that when we call
// `this.environment.transformRequest(mod.url)` later for
// style modules in this function, the Svelte components
// have already been processed and the cached styles are up
// to date.
const svelteModules = [...svelteJsModules, ...svelteCssModules];

if (svelteModules.length === 0) {
return; // nothing to do for us
}
Expand Down