Skip to content

Commit f21146f

Browse files
committed
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 (cherry picked from commit f7409c8)
1 parent 1bf186b commit f21146f

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
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

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

+16-4
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,18 @@ export function resolveOptions(preResolveOptions, viteConfig, cache) {
199199
dev: !viteConfig.isProduction
200200
}
201201
};
202+
const hot =
203+
!viteConfig.isProduction &&
204+
!preResolveOptions.isBuild &&
205+
viteConfig.server &&
206+
viteConfig.server.hmr !== false;
202207
if (isSvelte5) {
203208
if (isSvelte5WithHMRSupport) {
204209
// @ts-expect-error svelte4 does not have hmr option
205-
defaultOptions.compilerOptions.hmr = !viteConfig.isProduction;
210+
defaultOptions.compilerOptions.hmr = hot;
206211
}
207212
} else {
208-
defaultOptions.hot = viteConfig.isProduction
213+
defaultOptions.hot = !hot
209214
? false
210215
: {
211216
injectCss: css === 'injected',
@@ -224,7 +229,7 @@ export function resolveOptions(preResolveOptions, viteConfig, cache) {
224229
removeIgnoredOptions(merged);
225230
handleDeprecatedOptions(merged);
226231
addExtraPreprocessors(merged, viteConfig);
227-
enforceOptionsForHmr(merged);
232+
enforceOptionsForHmr(merged, viteConfig);
228233
enforceOptionsForProduction(merged);
229234
// mergeConfigs would mangle functions on the stats class, so do this afterwards
230235
if (log.debug.enabled && isDebugNamespaceEnabled('stats')) {
@@ -235,8 +240,15 @@ export function resolveOptions(preResolveOptions, viteConfig, cache) {
235240

236241
/**
237242
* @param {import('../types/options.d.ts').ResolvedOptions} options
243+
* @param {import('vite').ResolvedConfig} viteConfig
238244
*/
239-
function enforceOptionsForHmr(options) {
245+
function enforceOptionsForHmr(options, viteConfig) {
246+
if (options.hot && viteConfig.server?.hmr === false) {
247+
log.warn(
248+
'vite config server.hmr is false but hot is true. Forcing hot to false as it would not work.'
249+
);
250+
options.hot = false;
251+
}
240252
if (isSvelte5) {
241253
if (options.hot && isSvelte5WithHMRSupport) {
242254
log.warn(

0 commit comments

Comments
 (0)