From 5c35de715c503a2164acd96ed38160673e2d5655 Mon Sep 17 00:00:00 2001 From: dominikg Date: Tue, 21 May 2024 11:43:56 +0200 Subject: [PATCH 1/3] feat: disable hmr when running in vitest by default --- .changeset/strong-cherries-know.md | 5 +++++ packages/e2e-tests/hmr/vite.config.js | 2 +- packages/e2e-tests/kit-node/svelte.config.js | 5 +++-- packages/e2e-tests/svelte-preprocess/vite.config.js | 2 +- packages/e2e-tests/ts-type-import/vite.config.js | 4 ++-- packages/e2e-tests/vite-ssr-esm/vite.config.js | 2 +- packages/vite-plugin-svelte/src/types/options.d.ts | 1 + packages/vite-plugin-svelte/src/utils/options.js | 5 +++-- 8 files changed, 17 insertions(+), 9 deletions(-) create mode 100644 .changeset/strong-cherries-know.md diff --git a/.changeset/strong-cherries-know.md b/.changeset/strong-cherries-know.md new file mode 100644 index 000000000..3b5729fdf --- /dev/null +++ b/.changeset/strong-cherries-know.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/vite-plugin-svelte': patch +--- + +feat: disable hmr by default when running inside vitest diff --git a/packages/e2e-tests/hmr/vite.config.js b/packages/e2e-tests/hmr/vite.config.js index 0d7a032f2..12d9d52ec 100644 --- a/packages/e2e-tests/hmr/vite.config.js +++ b/packages/e2e-tests/hmr/vite.config.js @@ -4,7 +4,7 @@ import { transformValidation } from 'e2e-test-dep-vite-plugins'; export default defineConfig(({ command, mode }) => { return { - plugins: [transformValidation(), svelte()], + plugins: [transformValidation(), svelte({ compilerOptions: { hmr: command === 'serve' } })], build: { minify: false, target: 'esnext', diff --git a/packages/e2e-tests/kit-node/svelte.config.js b/packages/e2e-tests/kit-node/svelte.config.js index 8c700e24a..22b32cef5 100644 --- a/packages/e2e-tests/kit-node/svelte.config.js +++ b/packages/e2e-tests/kit-node/svelte.config.js @@ -1,10 +1,11 @@ import node from '@sveltejs/adapter-node'; - /** @type {import('@sveltejs/kit').Config} */ const config = { kit: { adapter: node() + }, + compilerOptions: { + hmr: true // for some reason process.env.NODE_ENV==='development' doesn't work here } }; - export default config; diff --git a/packages/e2e-tests/svelte-preprocess/vite.config.js b/packages/e2e-tests/svelte-preprocess/vite.config.js index 20e9a8e57..79ca3edae 100644 --- a/packages/e2e-tests/svelte-preprocess/vite.config.js +++ b/packages/e2e-tests/svelte-preprocess/vite.config.js @@ -3,7 +3,7 @@ import { defineConfig } from 'vite'; export default defineConfig(({ command, mode }) => { return { - plugins: [svelte()], + plugins: [svelte({ compilerOptions: { hmr: command === 'serve' } })], build: { // make build faster by skipping transforms and minification target: 'esnext', diff --git a/packages/e2e-tests/ts-type-import/vite.config.js b/packages/e2e-tests/ts-type-import/vite.config.js index ed4763cbd..7e6e277c0 100644 --- a/packages/e2e-tests/ts-type-import/vite.config.js +++ b/packages/e2e-tests/ts-type-import/vite.config.js @@ -1,9 +1,9 @@ import { svelte } from '@sveltejs/vite-plugin-svelte'; import { defineConfig } from 'vite'; -export default defineConfig(() => { +export default defineConfig(({ command }) => { return { - plugins: [svelte()], + plugins: [svelte({ compilerOptions: { hmr: command === 'serve' } })], build: { // make build faster by skipping transforms and minification target: 'esnext', diff --git a/packages/e2e-tests/vite-ssr-esm/vite.config.js b/packages/e2e-tests/vite-ssr-esm/vite.config.js index b588f3674..ee5a8b9f1 100644 --- a/packages/e2e-tests/vite-ssr-esm/vite.config.js +++ b/packages/e2e-tests/vite-ssr-esm/vite.config.js @@ -6,7 +6,7 @@ export default defineConfig(({ command, mode }) => { plugins: [ svelte({ compilerOptions: { - hydratable: true /* required for clientside hydration */ + hmr: command === 'serve' } }) ], diff --git a/packages/vite-plugin-svelte/src/types/options.d.ts b/packages/vite-plugin-svelte/src/types/options.d.ts index 5b4ca4274..9895c774b 100644 --- a/packages/vite-plugin-svelte/src/types/options.d.ts +++ b/packages/vite-plugin-svelte/src/types/options.d.ts @@ -12,6 +12,7 @@ export interface PreResolvedOptions extends Options { isBuild: boolean; isServe: boolean; isDebug: boolean; + isVitest: boolean; } export interface ResolvedOptions extends PreResolvedOptions { diff --git a/packages/vite-plugin-svelte/src/utils/options.js b/packages/vite-plugin-svelte/src/utils/options.js index a4a6fd565..da6509324 100644 --- a/packages/vite-plugin-svelte/src/utils/options.js +++ b/packages/vite-plugin-svelte/src/utils/options.js @@ -155,7 +155,8 @@ export async function preResolveOptions(inlineOptions, viteUserConfig, viteEnv) root: viteConfigWithResolvedRoot.root, isBuild, isServe: viteEnv.command === 'serve', - isDebug: process.env.DEBUG != null + isDebug: process.env.DEBUG != null, + isVitest: !!process.env.VITEST }; const merged = /** @type {import('../types/options.d.ts').PreResolvedOptions} */ ( @@ -201,7 +202,7 @@ export function resolveOptions(preResolveOptions, viteConfig, cache) { compilerOptions: { css, dev: !viteConfig.isProduction, - hmr: !viteConfig.isProduction && !preResolveOptions.isBuild + hmr: !viteConfig.isProduction && !preResolveOptions.isBuild && !preResolveOptions.isVitest } }; From 7e8b43f094f8f5538416c65e6f14948822f35263 Mon Sep 17 00:00:00 2001 From: dominikg Date: Fri, 24 May 2024 10:44:53 +0200 Subject: [PATCH 2/3] refactor: use vite server.hmr config instead that is set by vitest --- packages/e2e-tests/hmr/vite.config.js | 2 +- packages/e2e-tests/kit-node/svelte.config.js | 3 --- packages/e2e-tests/svelte-preprocess/vite.config.js | 2 +- packages/e2e-tests/ts-type-import/vite.config.js | 4 ++-- packages/e2e-tests/vite-ssr-esm/vite.config.js | 8 +------- packages/vite-plugin-svelte/src/types/options.d.ts | 1 - packages/vite-plugin-svelte/src/utils/options.js | 9 ++++++--- 7 files changed, 11 insertions(+), 18 deletions(-) diff --git a/packages/e2e-tests/hmr/vite.config.js b/packages/e2e-tests/hmr/vite.config.js index 12d9d52ec..0d7a032f2 100644 --- a/packages/e2e-tests/hmr/vite.config.js +++ b/packages/e2e-tests/hmr/vite.config.js @@ -4,7 +4,7 @@ import { transformValidation } from 'e2e-test-dep-vite-plugins'; export default defineConfig(({ command, mode }) => { return { - plugins: [transformValidation(), svelte({ compilerOptions: { hmr: command === 'serve' } })], + plugins: [transformValidation(), svelte()], build: { minify: false, target: 'esnext', diff --git a/packages/e2e-tests/kit-node/svelte.config.js b/packages/e2e-tests/kit-node/svelte.config.js index 22b32cef5..17c5095f0 100644 --- a/packages/e2e-tests/kit-node/svelte.config.js +++ b/packages/e2e-tests/kit-node/svelte.config.js @@ -3,9 +3,6 @@ import node from '@sveltejs/adapter-node'; const config = { kit: { adapter: node() - }, - compilerOptions: { - hmr: true // for some reason process.env.NODE_ENV==='development' doesn't work here } }; export default config; diff --git a/packages/e2e-tests/svelte-preprocess/vite.config.js b/packages/e2e-tests/svelte-preprocess/vite.config.js index 79ca3edae..20e9a8e57 100644 --- a/packages/e2e-tests/svelte-preprocess/vite.config.js +++ b/packages/e2e-tests/svelte-preprocess/vite.config.js @@ -3,7 +3,7 @@ import { defineConfig } from 'vite'; export default defineConfig(({ command, mode }) => { return { - plugins: [svelte({ compilerOptions: { hmr: command === 'serve' } })], + plugins: [svelte()], build: { // make build faster by skipping transforms and minification target: 'esnext', diff --git a/packages/e2e-tests/ts-type-import/vite.config.js b/packages/e2e-tests/ts-type-import/vite.config.js index 7e6e277c0..ed4763cbd 100644 --- a/packages/e2e-tests/ts-type-import/vite.config.js +++ b/packages/e2e-tests/ts-type-import/vite.config.js @@ -1,9 +1,9 @@ import { svelte } from '@sveltejs/vite-plugin-svelte'; import { defineConfig } from 'vite'; -export default defineConfig(({ command }) => { +export default defineConfig(() => { return { - plugins: [svelte({ compilerOptions: { hmr: command === 'serve' } })], + plugins: [svelte()], build: { // make build faster by skipping transforms and minification target: 'esnext', diff --git a/packages/e2e-tests/vite-ssr-esm/vite.config.js b/packages/e2e-tests/vite-ssr-esm/vite.config.js index ee5a8b9f1..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: { - hmr: command === 'serve' - } - }) - ], + plugins: [svelte()], build: { target: 'esnext', minify: false, diff --git a/packages/vite-plugin-svelte/src/types/options.d.ts b/packages/vite-plugin-svelte/src/types/options.d.ts index 9895c774b..5b4ca4274 100644 --- a/packages/vite-plugin-svelte/src/types/options.d.ts +++ b/packages/vite-plugin-svelte/src/types/options.d.ts @@ -12,7 +12,6 @@ export interface PreResolvedOptions extends Options { isBuild: boolean; isServe: boolean; isDebug: boolean; - isVitest: boolean; } export interface ResolvedOptions extends PreResolvedOptions { diff --git a/packages/vite-plugin-svelte/src/utils/options.js b/packages/vite-plugin-svelte/src/utils/options.js index da6509324..3942d71dc 100644 --- a/packages/vite-plugin-svelte/src/utils/options.js +++ b/packages/vite-plugin-svelte/src/utils/options.js @@ -155,8 +155,7 @@ export async function preResolveOptions(inlineOptions, viteUserConfig, viteEnv) root: viteConfigWithResolvedRoot.root, isBuild, isServe: viteEnv.command === 'serve', - isDebug: process.env.DEBUG != null, - isVitest: !!process.env.VITEST + isDebug: process.env.DEBUG != null }; const merged = /** @type {import('../types/options.d.ts').PreResolvedOptions} */ ( @@ -202,7 +201,11 @@ export function resolveOptions(preResolveOptions, viteConfig, cache) { compilerOptions: { css, dev: !viteConfig.isProduction, - hmr: !viteConfig.isProduction && !preResolveOptions.isBuild && !preResolveOptions.isVitest + hmr: + !viteConfig.isProduction && + !preResolveOptions.isBuild && + viteConfig.server && + viteConfig.server.hmr !== false } }; From 9bca8612a0159a6098407b8f45f214262b987de6 Mon Sep 17 00:00:00 2001 From: dominikg Date: Fri, 24 May 2024 11:31:22 +0200 Subject: [PATCH 3/3] fix: enforce hmr false, update changeset --- .changeset/strong-cherries-know.md | 2 +- packages/vite-plugin-svelte/src/utils/options.js | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/.changeset/strong-cherries-know.md b/.changeset/strong-cherries-know.md index 3b5729fdf..b276f0832 100644 --- a/.changeset/strong-cherries-know.md +++ b/.changeset/strong-cherries-know.md @@ -2,4 +2,4 @@ '@sveltejs/vite-plugin-svelte': patch --- -feat: disable hmr by default when running inside vitest +fix: disable hmr when vite config server.hmr is false diff --git a/packages/vite-plugin-svelte/src/utils/options.js b/packages/vite-plugin-svelte/src/utils/options.js index 3942d71dc..8afa38372 100644 --- a/packages/vite-plugin-svelte/src/utils/options.js +++ b/packages/vite-plugin-svelte/src/utils/options.js @@ -221,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')) { @@ -232,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' @@ -241,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; + } } /** @@ -268,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 || {});