Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: align css hash in prebundling and disable hmr #950

Merged
merged 4 commits into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
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
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