Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: backport fixes #917

Merged
merged 3 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
20 changes: 16 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,18 @@ export function resolveOptions(preResolveOptions, viteConfig, cache) {
dev: !viteConfig.isProduction
}
};
const hot =
!viteConfig.isProduction &&
!preResolveOptions.isBuild &&
viteConfig.server &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something I just noticed but not a big deal to change. It looks like viteConfig.server is always truthy, but this line implies otherwise, which could be confusing because we don't want to disable HMR if the server is not configured (HMR would've been enabled by default in dev)

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 +229,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 +240,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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ?. seem unnecessary too.

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