From 722f8ff0b0e1fca9481e997a64c59ab60b574719 Mon Sep 17 00:00:00 2001 From: Dominik G Date: Wed, 29 May 2024 21:26:03 +0200 Subject: [PATCH] chore: backport fixes (#917) * 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 f7409c8d55fba965e2671f767d00c93e4d1200b4) * fix: ensure vite config is only resolved once in lazy init of vitePreprocess (#912) * fix: ensure vite config is only resolved once * fix: add back inlined function to please ts (cherry picked from commit 1211f97ffd85df1dbc3fac68265eb0b88ea61bdd) * fix: remove extraneous checks for viteConfig.server --- .changeset/rare-turkeys-mate.md | 5 ++ .changeset/strong-cherries-know.md | 5 ++ packages/vite-plugin-svelte/src/preprocess.js | 46 ++++++++++--------- .../vite-plugin-svelte/src/utils/options.js | 17 +++++-- 4 files changed, 47 insertions(+), 26 deletions(-) create mode 100644 .changeset/rare-turkeys-mate.md create mode 100644 .changeset/strong-cherries-know.md diff --git a/.changeset/rare-turkeys-mate.md b/.changeset/rare-turkeys-mate.md new file mode 100644 index 000000000..c7b2fc748 --- /dev/null +++ b/.changeset/rare-turkeys-mate.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/vite-plugin-svelte': patch +--- + +fix: ensure vite config is only resolved once during lazy init of vitePreprocess 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/vite-plugin-svelte/src/preprocess.js b/packages/vite-plugin-svelte/src/preprocess.js index 1753af5b3..522e1b6ab 100644 --- a/packages/vite-plugin-svelte/src/preprocess.js +++ b/packages/vite-plugin-svelte/src/preprocess.js @@ -61,29 +61,16 @@ function viteScript() { * @returns {{ style: import('svelte/compiler').Preprocessor }} */ function viteStyle(config = {}) { - /** @type {CssTransform} */ - let transform; + /** @type {Promise | CssTransform} */ + let cssTransform; /** @type {import('svelte/compiler').Preprocessor} */ const style = async ({ attributes, content, filename = '' }) => { const ext = attributes.lang ? `.${attributes.lang}` : '.css'; if (attributes.lang && !isCSSRequest(ext)) return; - if (!transform) { - /** @type {import('vite').ResolvedConfig} */ - let resolvedConfig; - // @ts-expect-error special prop added if running in v-p-s - if (style.__resolvedConfig) { - // @ts-expect-error - resolvedConfig = style.__resolvedConfig; - } else if (isResolvedConfig(config)) { - resolvedConfig = config; - } else { - resolvedConfig = await resolveConfig( - config, - process.env.NODE_ENV === 'production' ? 'build' : 'serve' - ); - } - transform = getCssTransformFn(resolvedConfig); + if (!cssTransform) { + cssTransform = createCssTransform(style, config).then((t) => (cssTransform = t)); } + const transform = await cssTransform; const suffix = `${lang_sep}${ext}`; const moduleId = `${filename}${suffix}`; const { code, map, deps } = await transform(content, moduleId); @@ -102,12 +89,27 @@ function viteStyle(config = {}) { } /** - * @param {import('vite').ResolvedConfig} config - * @returns {CssTransform} + * @param {import('svelte/compiler').Preprocessor} style + * @param {import('vite').ResolvedConfig | import('vite').InlineConfig} config + * @returns {Promise} */ -function getCssTransformFn(config) { +async function createCssTransform(style, config) { + /** @type {import('vite').ResolvedConfig} */ + let resolvedConfig; + // @ts-expect-error special prop added if running in v-p-s + if (style.__resolvedConfig) { + // @ts-expect-error + resolvedConfig = style.__resolvedConfig; + } else if (isResolvedConfig(config)) { + resolvedConfig = config; + } else { + resolvedConfig = await resolveConfig( + config, + process.env.NODE_ENV === 'production' ? 'build' : 'serve' + ); + } return async (code, filename) => { - return preprocessCSS(code, filename, config); + return preprocessCSS(code, filename, resolvedConfig); }; } diff --git a/packages/vite-plugin-svelte/src/utils/options.js b/packages/vite-plugin-svelte/src/utils/options.js index 2b035f2d1..3b8766611 100644 --- a/packages/vite-plugin-svelte/src/utils/options.js +++ b/packages/vite-plugin-svelte/src/utils/options.js @@ -199,13 +199,15 @@ export function resolveOptions(preResolveOptions, viteConfig, cache) { dev: !viteConfig.isProduction } }; + const hot = + !viteConfig.isProduction && !preResolveOptions.isBuild && viteConfig.server.hmr !== false; if (isSvelte5) { if (isSvelte5WithHMRSupport) { // @ts-expect-error svelte4 does not have hmr option - defaultOptions.compilerOptions.hmr = !viteConfig.isProduction; + defaultOptions.compilerOptions.hmr = hot; } } else { - defaultOptions.hot = viteConfig.isProduction + defaultOptions.hot = !hot ? false : { injectCss: css === 'injected', @@ -224,7 +226,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')) { @@ -235,8 +237,15 @@ 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 && viteConfig.server.hmr === false) { + log.warn( + 'vite config server.hmr is false but hot is true. Forcing hot to false as it would not work.' + ); + options.hot = false; + } if (isSvelte5) { if (options.hot && isSvelte5WithHMRSupport) { log.warn(