Skip to content

Commit 4fbc960

Browse files
authored
fix: align css hash in prebundling and disable hmr (#950)
* fix(dev): avoid css hash mismatch by applying custom css hash even for prebundling * fix(optimize): skip hmr compile for prebundled * fix: jsdoc * refactor: simplify normalize use in esbuild plugin
1 parent c1e2a80 commit 4fbc960

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

.changeset/healthy-years-battle.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': patch
3+
---
4+
5+
fix(dev): compile with hmr: false for prebundled deps as hmr does not work with that

.changeset/twenty-walls-watch.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': patch
3+
---
4+
5+
fix(dev): make sure custom cssHash is applied consistently even for prebundled components to avoid hash mismatches during hydration

packages/vite-plugin-svelte/src/utils/esbuild.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { readFileSync } from 'node:fs';
22
import * as svelte from 'svelte/compiler';
33
import { log } from './log.js';
44
import { toESBuildError } from './error.js';
5+
import { safeBase64Hash } from './hash.js';
6+
import { normalize } from './id.js';
57

68
/**
79
* @typedef {NonNullable<import('vite').DepOptimizationOptions['esbuildOptions']>} EsbuildOptions
@@ -49,7 +51,7 @@ export function esbuildSveltePlugin(options) {
4951

5052
/**
5153
* @param {import('../types/options.d.ts').ResolvedOptions} options
52-
* @param {{ filename: string; code: string }} input
54+
* @param {{ filename: string, code: string }} input
5355
* @param {import('../types/vite-plugin-svelte-stats.d.ts').StatCollection} [statsCollection]
5456
* @returns {Promise<string>}
5557
*/
@@ -68,6 +70,14 @@ async function compileSvelte(options, { filename, code }, statsCollection) {
6870
generate: 'client'
6971
};
7072

73+
if (compileOptions.hmr) {
74+
if (options.emitCss) {
75+
const hash = `s-${safeBase64Hash(normalize(filename, options.root))}`;
76+
compileOptions.cssHash = () => hash;
77+
}
78+
compileOptions.hmr = false;
79+
}
80+
7181
let preprocessed;
7282

7383
if (options.preprocess) {

packages/vite-plugin-svelte/src/utils/id.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ function parseRequestQuery(rawQuery) {
133133
* @param {string} normalizedRoot
134134
* @returns {string}
135135
*/
136-
function normalize(filename, normalizedRoot) {
136+
export function normalize(filename, normalizedRoot) {
137137
return stripRoot(normalizePath(filename), normalizedRoot);
138138
}
139139

0 commit comments

Comments
 (0)