Skip to content

Commit 1211f97

Browse files
authored
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
1 parent 8bd5a00 commit 1211f97

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
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

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

0 commit comments

Comments
 (0)