Skip to content

Commit

Permalink
fix: disable hmr when explicitly disabled in vite config (#913)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
dominikg authored May 24, 2024
1 parent 1211f97 commit f7409c8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
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

0 comments on commit f7409c8

Please sign in to comment.