Skip to content

Commit

Permalink
chore: backport fixes (#917)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
dominikg committed May 29, 2024
1 parent 1bf186b commit 722f8ff
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/rare-turkeys-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/vite-plugin-svelte': patch
---

fix: ensure vite config is only resolved once during lazy init of vitePreprocess
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
46 changes: 24 additions & 22 deletions packages/vite-plugin-svelte/src/preprocess.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,16 @@ function viteScript() {
* @returns {{ style: import('svelte/compiler').Preprocessor }}
*/
function viteStyle(config = {}) {
/** @type {CssTransform} */
let transform;
/** @type {Promise<CssTransform> | 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);
Expand All @@ -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<CssTransform>}
*/
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);
};
}

Expand Down
17 changes: 13 additions & 4 deletions packages/vite-plugin-svelte/src/utils/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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')) {
Expand All @@ -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(
Expand Down

0 comments on commit 722f8ff

Please sign in to comment.