diff options
author | 2024-11-14 15:31:51 +0000 | |
---|---|---|
committer | 2024-11-14 23:31:51 +0800 | |
commit | 9fc2ab8cc848739a21bfa3f754e9bec4926dc034 (patch) | |
tree | a184ada6711296569a064c01defd2fa6a74f63c5 /packages/integrations/svelte/src | |
parent | bdc0890061533466da19660ff83a331a3136f6c4 (diff) | |
download | astro-9fc2ab8cc848739a21bfa3f754e9bec4926dc034.tar.gz astro-9fc2ab8cc848739a21bfa3f754e9bec4926dc034.tar.zst astro-9fc2ab8cc848739a21bfa3f754e9bec4926dc034.zip |
Update to svelte 5 (#12364)
Co-authored-by: bluwy <bjornlu.dev@gmail.com>
Diffstat (limited to 'packages/integrations/svelte/src')
-rw-r--r-- | packages/integrations/svelte/src/index.ts | 99 |
1 files changed, 12 insertions, 87 deletions
diff --git a/packages/integrations/svelte/src/index.ts b/packages/integrations/svelte/src/index.ts index b0db3505c..0db02aff3 100644 --- a/packages/integrations/svelte/src/index.ts +++ b/packages/integrations/svelte/src/index.ts @@ -1,111 +1,36 @@ -import { fileURLToPath } from 'node:url'; import type { Options } from '@sveltejs/vite-plugin-svelte'; import { svelte, vitePreprocess } from '@sveltejs/vite-plugin-svelte'; import type { AstroIntegration, AstroRenderer, ContainerRenderer } from 'astro'; -import { VERSION } from 'svelte/compiler'; -import type { UserConfig } from 'vite'; - -const isSvelte5 = Number.parseInt(VERSION.split('.').at(0)!) >= 5; function getRenderer(): AstroRenderer { return { name: '@astrojs/svelte', - clientEntrypoint: isSvelte5 ? '@astrojs/svelte/client-v5.js' : '@astrojs/svelte/client.js', - serverEntrypoint: isSvelte5 ? '@astrojs/svelte/server-v5.js' : '@astrojs/svelte/server.js', + clientEntrypoint: '@astrojs/svelte/client.js', + serverEntrypoint: '@astrojs/svelte/server.js', }; } export function getContainerRenderer(): ContainerRenderer { return { name: '@astrojs/svelte', - serverEntrypoint: isSvelte5 ? '@astrojs/svelte/server-v5.js' : '@astrojs/svelte/server.js', - }; -} - -async function svelteConfigHasPreprocess(root: URL) { - const svelteConfigFiles = ['./svelte.config.js', './svelte.config.cjs', './svelte.config.mjs']; - for (const file of svelteConfigFiles) { - const filePath = fileURLToPath(new URL(file, root)); - try { - // Suppress warnings by vite: "The above dynamic import cannot be analyzed by Vite." - const config = (await import(/* @vite-ignore */ filePath)).default; - return !!config.preprocess; - } catch {} - } -} - -type ViteConfigurationArgs = { - isDev: boolean; - options?: Options | OptionsCallback; - root: URL; -}; - -async function getViteConfiguration({ - options, - isDev, - root, -}: ViteConfigurationArgs): Promise<UserConfig> { - const defaultOptions: Partial<Options> = { - emitCss: true, - compilerOptions: { dev: isDev }, - }; - - // `hydratable` does not need to be set in Svelte 5 as it's always hydratable by default - if (!isSvelte5) { - // @ts-ignore ignore Partial type above - defaultOptions.compilerOptions.hydratable = true; - } - - // Disable hot mode during the build - if (!isDev) { - defaultOptions.hot = false; - } - - let resolvedOptions: Partial<Options>; - - if (!options) { - resolvedOptions = defaultOptions; - } else if (typeof options === 'function') { - resolvedOptions = options(defaultOptions); - } else { - resolvedOptions = { - ...options, - ...defaultOptions, - compilerOptions: { - ...options.compilerOptions, - // Always use dev and hydratable from defaults - ...defaultOptions.compilerOptions, - }, - }; - } - - if (!resolvedOptions.preprocess && !(await svelteConfigHasPreprocess(root))) { - resolvedOptions.preprocess = vitePreprocess(); - } - - return { - optimizeDeps: { - include: [isSvelte5 ? '@astrojs/svelte/client-v5.js' : '@astrojs/svelte/client.js'], - exclude: [isSvelte5 ? '@astrojs/svelte/server-v5.js' : '@astrojs/svelte/server.js'], - }, - plugins: [svelte(resolvedOptions)], + serverEntrypoint: '@astrojs/svelte/server.js', }; } -type OptionsCallback = (defaultOptions: Options) => Options; -export default function (options?: Options | OptionsCallback): AstroIntegration { +export default function svelteIntegration(options?: Options): AstroIntegration { return { name: '@astrojs/svelte', hooks: { - // Anything that gets returned here is merged into Astro Config - 'astro:config:setup': async ({ command, updateConfig, addRenderer, config }) => { + 'astro:config:setup': async ({ updateConfig, addRenderer }) => { addRenderer(getRenderer()); updateConfig({ - vite: await getViteConfiguration({ - options, - isDev: command === 'dev', - root: config.root, - }), + vite: { + optimizeDeps: { + include: ['@astrojs/svelte/client.js'], + exclude: ['@astrojs/svelte/server.js'], + }, + plugins: [svelte(options)], + }, }); }, }, |