Skip to content

Commit

Permalink
fix: align css hash in prebundling and disable hmr (#950)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
dominikg authored Jul 27, 2024
1 parent c1e2a80 commit 4fbc960
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/healthy-years-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/vite-plugin-svelte': patch
---

fix(dev): compile with hmr: false for prebundled deps as hmr does not work with that
5 changes: 5 additions & 0 deletions .changeset/twenty-walls-watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/vite-plugin-svelte': patch
---

fix(dev): make sure custom cssHash is applied consistently even for prebundled components to avoid hash mismatches during hydration
12 changes: 11 additions & 1 deletion packages/vite-plugin-svelte/src/utils/esbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { readFileSync } from 'node:fs';
import * as svelte from 'svelte/compiler';
import { log } from './log.js';
import { toESBuildError } from './error.js';
import { safeBase64Hash } from './hash.js';
import { normalize } from './id.js';

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

/**
* @param {import('../types/options.d.ts').ResolvedOptions} options
* @param {{ filename: string; code: string }} input
* @param {{ filename: string, code: string }} input
* @param {import('../types/vite-plugin-svelte-stats.d.ts').StatCollection} [statsCollection]
* @returns {Promise<string>}
*/
Expand All @@ -68,6 +70,14 @@ async function compileSvelte(options, { filename, code }, statsCollection) {
generate: 'client'
};

if (compileOptions.hmr) {
if (options.emitCss) {
const hash = `s-${safeBase64Hash(normalize(filename, options.root))}`;
compileOptions.cssHash = () => hash;
}
compileOptions.hmr = false;
}

let preprocessed;

if (options.preprocess) {
Expand Down
2 changes: 1 addition & 1 deletion packages/vite-plugin-svelte/src/utils/id.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ function parseRequestQuery(rawQuery) {
* @param {string} normalizedRoot
* @returns {string}
*/
function normalize(filename, normalizedRoot) {
export function normalize(filename, normalizedRoot) {
return stripRoot(normalizePath(filename), normalizedRoot);
}

Expand Down

0 comments on commit 4fbc960

Please sign in to comment.