Skip to content

Commit 722f8ff

Browse files
authored
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 f7409c8) * 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 1211f97) * fix: remove extraneous checks for viteConfig.server
1 parent 1bf186b commit 722f8ff

File tree

4 files changed

+47
-26
lines changed

4 files changed

+47
-26
lines changed

.changeset/rare-turkeys-mate.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': patch
3+
---
4+
5+
fix: ensure vite config is only resolved once during lazy init of vitePreprocess

.changeset/strong-cherries-know.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': patch
3+
---
4+
5+
fix: disable hmr when vite config server.hmr is false

packages/vite-plugin-svelte/src/preprocess.js

+24-22
Original file line numberDiff line numberDiff line change
@@ -61,29 +61,16 @@ function viteScript() {
6161
* @returns {{ style: import('svelte/compiler').Preprocessor }}
6262
*/
6363
function viteStyle(config = {}) {
64-
/** @type {CssTransform} */
65-
let transform;
64+
/** @type {Promise<CssTransform> | CssTransform} */
65+
let cssTransform;
6666
/** @type {import('svelte/compiler').Preprocessor} */
6767
const style = async ({ attributes, content, filename = '' }) => {
6868
const ext = attributes.lang ? `.${attributes.lang}` : '.css';
6969
if (attributes.lang && !isCSSRequest(ext)) return;
70-
if (!transform) {
71-
/** @type {import('vite').ResolvedConfig} */
72-
let resolvedConfig;
73-
// @ts-expect-error special prop added if running in v-p-s
74-
if (style.__resolvedConfig) {
75-
// @ts-expect-error
76-
resolvedConfig = style.__resolvedConfig;
77-
} else if (isResolvedConfig(config)) {
78-
resolvedConfig = config;
79-
} else {
80-
resolvedConfig = await resolveConfig(
81-
config,
82-
process.env.NODE_ENV === 'production' ? 'build' : 'serve'
83-
);
84-
}
85-
transform = getCssTransformFn(resolvedConfig);
70+
if (!cssTransform) {
71+
cssTransform = createCssTransform(style, config).then((t) => (cssTransform = t));
8672
}
73+
const transform = await cssTransform;
8774
const suffix = `${lang_sep}${ext}`;
8875
const moduleId = `${filename}${suffix}`;
8976
const { code, map, deps } = await transform(content, moduleId);
@@ -102,12 +89,27 @@ function viteStyle(config = {}) {
10289
}
10390

10491
/**
105-
* @param {import('vite').ResolvedConfig} config
106-
* @returns {CssTransform}
92+
* @param {import('svelte/compiler').Preprocessor} style
93+
* @param {import('vite').ResolvedConfig | import('vite').InlineConfig} config
94+
* @returns {Promise<CssTransform>}
10795
*/
108-
function getCssTransformFn(config) {
96+
async function createCssTransform(style, config) {
97+
/** @type {import('vite').ResolvedConfig} */
98+
let resolvedConfig;
99+
// @ts-expect-error special prop added if running in v-p-s
100+
if (style.__resolvedConfig) {
101+
// @ts-expect-error
102+
resolvedConfig = style.__resolvedConfig;
103+
} else if (isResolvedConfig(config)) {
104+
resolvedConfig = config;
105+
} else {
106+
resolvedConfig = await resolveConfig(
107+
config,
108+
process.env.NODE_ENV === 'production' ? 'build' : 'serve'
109+
);
110+
}
109111
return async (code, filename) => {
110-
return preprocessCSS(code, filename, config);
112+
return preprocessCSS(code, filename, resolvedConfig);
111113
};
112114
}
113115

packages/vite-plugin-svelte/src/utils/options.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,15 @@ export function resolveOptions(preResolveOptions, viteConfig, cache) {
199199
dev: !viteConfig.isProduction
200200
}
201201
};
202+
const hot =
203+
!viteConfig.isProduction && !preResolveOptions.isBuild && viteConfig.server.hmr !== false;
202204
if (isSvelte5) {
203205
if (isSvelte5WithHMRSupport) {
204206
// @ts-expect-error svelte4 does not have hmr option
205-
defaultOptions.compilerOptions.hmr = !viteConfig.isProduction;
207+
defaultOptions.compilerOptions.hmr = hot;
206208
}
207209
} else {
208-
defaultOptions.hot = viteConfig.isProduction
210+
defaultOptions.hot = !hot
209211
? false
210212
: {
211213
injectCss: css === 'injected',
@@ -224,7 +226,7 @@ export function resolveOptions(preResolveOptions, viteConfig, cache) {
224226
removeIgnoredOptions(merged);
225227
handleDeprecatedOptions(merged);
226228
addExtraPreprocessors(merged, viteConfig);
227-
enforceOptionsForHmr(merged);
229+
enforceOptionsForHmr(merged, viteConfig);
228230
enforceOptionsForProduction(merged);
229231
// mergeConfigs would mangle functions on the stats class, so do this afterwards
230232
if (log.debug.enabled && isDebugNamespaceEnabled('stats')) {
@@ -235,8 +237,15 @@ export function resolveOptions(preResolveOptions, viteConfig, cache) {
235237

236238
/**
237239
* @param {import('../types/options.d.ts').ResolvedOptions} options
240+
* @param {import('vite').ResolvedConfig} viteConfig
238241
*/
239-
function enforceOptionsForHmr(options) {
242+
function enforceOptionsForHmr(options, viteConfig) {
243+
if (options.hot && viteConfig.server.hmr === false) {
244+
log.warn(
245+
'vite config server.hmr is false but hot is true. Forcing hot to false as it would not work.'
246+
);
247+
options.hot = false;
248+
}
240249
if (isSvelte5) {
241250
if (options.hot && isSvelte5WithHMRSupport) {
242251
log.warn(

0 commit comments

Comments
 (0)