From 1211f97ffd85df1dbc3fac68265eb0b88ea61bdd Mon Sep 17 00:00:00 2001 From: Dominik G Date: Fri, 24 May 2024 09:16:54 +0200 Subject: [PATCH] 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 --- .changeset/rare-turkeys-mate.md | 5 ++ packages/vite-plugin-svelte/src/preprocess.js | 46 ++++++++++--------- 2 files changed, 29 insertions(+), 22 deletions(-) create mode 100644 .changeset/rare-turkeys-mate.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/packages/vite-plugin-svelte/src/preprocess.js b/packages/vite-plugin-svelte/src/preprocess.js index d15f7aa3f..fd282f554 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); }; }