Skip to content

fix: disable hmr when explicitly disabled in vite config #913

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

Merged
merged 3 commits into from
May 24, 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/strong-cherries-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/vite-plugin-svelte': patch
---

fix: disable hmr when vite config server.hmr is false
2 changes: 0 additions & 2 deletions packages/e2e-tests/kit-node/svelte.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import node from '@sveltejs/adapter-node';

/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
adapter: node()
}
};

export default config;
8 changes: 1 addition & 7 deletions packages/e2e-tests/vite-ssr-esm/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 15 additions & 4 deletions packages/vite-plugin-svelte/src/utils/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
};

Expand All @@ -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')) {
Expand All @@ -228,15 +232,22 @@ 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'
);
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;
}
}

/**
Expand Down Expand Up @@ -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 || {});
Expand Down