summaryrefslogtreecommitdiff
path: root/packages/integrations/svelte/src/index.ts
blob: f5a3dd945c3dab01cdfe5c10dac68dac821064c9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import type { AstroIntegration, AstroRenderer } from 'astro';
import { svelte } from '@sveltejs/vite-plugin-svelte';
import preprocess from 'svelte-preprocess';

function getRenderer(): AstroRenderer {
	return {
		name: '@astrojs/svelte',
		clientEntrypoint: '@astrojs/svelte/client.js',
		serverEntrypoint: '@astrojs/svelte/server.js',
	};
}

function getViteConfiguration(isDev: boolean) {
	return {
		optimizeDeps: {
			include: ['@astrojs/svelte/client.js', 'svelte', 'svelte/internal'],
			exclude: ['@astrojs/svelte/server.js'],
		},
		plugins: [
			svelte({
				emitCss: true,
				compilerOptions: { dev: isDev, hydratable: true },
				preprocess: [
					preprocess({
						less: true,
						sass: { renderSync: true },
						scss: { renderSync: true },
						stylus: true,
						typescript: true,
					}),
				],
			}),
		],
	};
}

export default function (): AstroIntegration {
	return {
		name: '@astrojs/svelte',
		hooks: {
			// Anything that gets returned here is merged into Astro Config
			'astro:config:setup': ({ command, updateConfig, addRenderer }) => {
				addRenderer(getRenderer());
				updateConfig({ vite: getViteConfiguration(command === 'dev') });
			},
		},
	};
}