diff --git a/.changeset/strong-cherries-know.md b/.changeset/strong-cherries-know.md new file mode 100644 index 000000000..b276f0832 --- /dev/null +++ b/.changeset/strong-cherries-know.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/vite-plugin-svelte': patch +--- + +fix: disable hmr when vite config server.hmr is false diff --git a/packages/e2e-tests/kit-node/svelte.config.js b/packages/e2e-tests/kit-node/svelte.config.js index 8c700e24a..17c5095f0 100644 --- a/packages/e2e-tests/kit-node/svelte.config.js +++ b/packages/e2e-tests/kit-node/svelte.config.js @@ -1,10 +1,8 @@ import node from '@sveltejs/adapter-node'; - /** @type {import('@sveltejs/kit').Config} */ const config = { kit: { adapter: node() } }; - export default config; diff --git a/packages/e2e-tests/vite-ssr-esm/vite.config.js b/packages/e2e-tests/vite-ssr-esm/vite.config.js index b588f3674..63c8aadaa 100644 --- a/packages/e2e-tests/vite-ssr-esm/vite.config.js +++ b/packages/e2e-tests/vite-ssr-esm/vite.config.js @@ -3,13 +3,7 @@ import { svelte } from '@sveltejs/vite-plugin-svelte'; export default defineConfig(({ command, mode }) => { return { - plugins: [ - svelte({ - compilerOptions: { - hydratable: true /* required for clientside hydration */ - } - }) - ], + plugins: [svelte()], build: { target: 'esnext', minify: false, diff --git a/packages/vite-plugin-svelte/src/utils/options.js b/packages/vite-plugin-svelte/src/utils/options.js index a4a6fd565..8afa38372 100644 --- a/packages/vite-plugin-svelte/src/utils/options.js +++ b/packages/vite-plugin-svelte/src/utils/options.js @@ -201,7 +201,11 @@ export function resolveOptions(preResolveOptions, viteConfig, cache) { compilerOptions: { css, dev: !viteConfig.isProduction, - hmr: !viteConfig.isProduction && !preResolveOptions.isBuild + hmr: + !viteConfig.isProduction && + !preResolveOptions.isBuild && + viteConfig.server && + viteConfig.server.hmr !== false } }; @@ -217,7 +221,7 @@ export function resolveOptions(preResolveOptions, viteConfig, cache) { removeIgnoredOptions(merged); handleDeprecatedOptions(merged); addExtraPreprocessors(merged, viteConfig); - enforceOptionsForHmr(merged); + enforceOptionsForHmr(merged, viteConfig); enforceOptionsForProduction(merged); // mergeConfigs would mangle functions on the stats class, so do this afterwards if (log.debug.enabled && isDebugNamespaceEnabled('stats')) { @@ -228,8 +232,9 @@ export function resolveOptions(preResolveOptions, viteConfig, cache) { /** * @param {import('../types/options.d.ts').ResolvedOptions} options + * @param {import('vite').ResolvedConfig} viteConfig */ -function enforceOptionsForHmr(options) { +function enforceOptionsForHmr(options, viteConfig) { if (options.hot) { log.warn( 'svelte 5 has hmr integrated in core. Please remove the vitePlugin.hot option and use compilerOptions.hmr instead' @@ -237,6 +242,12 @@ function enforceOptionsForHmr(options) { delete options.hot; options.compilerOptions.hmr = true; } + if (options.compilerOptions.hmr && viteConfig.server?.hmr === false) { + log.warn( + 'vite config server.hmr is false but compilerOptions.hmr is true. Forcing compilerOptions.hmr to false as it would not work.' + ); + options.compilerOptions.hmr = false; + } } /** @@ -264,7 +275,7 @@ function enforceOptionsForProduction(options) { */ function removeIgnoredOptions(options) { const ignoredCompilerOptions = ['generate', 'format', 'filename']; - if (options.hot && options.emitCss) { + if (options.compilerOptions.hmr && options.emitCss) { ignoredCompilerOptions.push('cssHash'); } const passedCompilerOptions = Object.keys(options.compilerOptions || {});