Skip to content

Commit f7409c8

Browse files
authored
fix: disable hmr when explicitly disabled in vite config (#913)
* feat: disable hmr when running in vitest by default * refactor: use vite server.hmr config instead that is set by vitest * fix: enforce hmr false, update changeset
1 parent 1211f97 commit f7409c8

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

.changeset/strong-cherries-know.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: disable hmr when vite config server.hmr is false
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import node from '@sveltejs/adapter-node';
2-
32
/** @type {import('@sveltejs/kit').Config} */
43
const config = {
54
kit: {
65
adapter: node()
76
}
87
};
9-
108
export default config;

packages/e2e-tests/vite-ssr-esm/vite.config.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@ import { svelte } from '@sveltejs/vite-plugin-svelte';
33

44
export default defineConfig(({ command, mode }) => {
55
return {
6-
plugins: [
7-
svelte({
8-
compilerOptions: {
9-
hydratable: true /* required for clientside hydration */
10-
}
11-
})
12-
],
6+
plugins: [svelte()],
137
build: {
148
target: 'esnext',
159
minify: false,

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

+15-4
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,11 @@ export function resolveOptions(preResolveOptions, viteConfig, cache) {
201201
compilerOptions: {
202202
css,
203203
dev: !viteConfig.isProduction,
204-
hmr: !viteConfig.isProduction && !preResolveOptions.isBuild
204+
hmr:
205+
!viteConfig.isProduction &&
206+
!preResolveOptions.isBuild &&
207+
viteConfig.server &&
208+
viteConfig.server.hmr !== false
205209
}
206210
};
207211

@@ -217,7 +221,7 @@ export function resolveOptions(preResolveOptions, viteConfig, cache) {
217221
removeIgnoredOptions(merged);
218222
handleDeprecatedOptions(merged);
219223
addExtraPreprocessors(merged, viteConfig);
220-
enforceOptionsForHmr(merged);
224+
enforceOptionsForHmr(merged, viteConfig);
221225
enforceOptionsForProduction(merged);
222226
// mergeConfigs would mangle functions on the stats class, so do this afterwards
223227
if (log.debug.enabled && isDebugNamespaceEnabled('stats')) {
@@ -228,15 +232,22 @@ export function resolveOptions(preResolveOptions, viteConfig, cache) {
228232

229233
/**
230234
* @param {import('../types/options.d.ts').ResolvedOptions} options
235+
* @param {import('vite').ResolvedConfig} viteConfig
231236
*/
232-
function enforceOptionsForHmr(options) {
237+
function enforceOptionsForHmr(options, viteConfig) {
233238
if (options.hot) {
234239
log.warn(
235240
'svelte 5 has hmr integrated in core. Please remove the vitePlugin.hot option and use compilerOptions.hmr instead'
236241
);
237242
delete options.hot;
238243
options.compilerOptions.hmr = true;
239244
}
245+
if (options.compilerOptions.hmr && viteConfig.server?.hmr === false) {
246+
log.warn(
247+
'vite config server.hmr is false but compilerOptions.hmr is true. Forcing compilerOptions.hmr to false as it would not work.'
248+
);
249+
options.compilerOptions.hmr = false;
250+
}
240251
}
241252

242253
/**
@@ -264,7 +275,7 @@ function enforceOptionsForProduction(options) {
264275
*/
265276
function removeIgnoredOptions(options) {
266277
const ignoredCompilerOptions = ['generate', 'format', 'filename'];
267-
if (options.hot && options.emitCss) {
278+
if (options.compilerOptions.hmr && options.emitCss) {
268279
ignoredCompilerOptions.push('cssHash');
269280
}
270281
const passedCompilerOptions = Object.keys(options.compilerOptions || {});

0 commit comments

Comments
 (0)