summaryrefslogtreecommitdiff
path: root/packages/integrations/preact/src/index.ts
blob: 4f4b0ee7924e5d465a37177501f3f51defe1990d (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import type { AstroIntegration, AstroRenderer, ViteUserConfig } from 'astro';

function getRenderer(development: boolean): AstroRenderer {
	return {
		name: '@astrojs/preact',
		clientEntrypoint: development ? '@astrojs/preact/client-dev.js' : '@astrojs/preact/client.js',
		serverEntrypoint: '@astrojs/preact/server.js',
		jsxImportSource: 'preact',
		jsxTransformOptions: async () => {
			const {
				default: { default: jsx },
				// @ts-expect-error types not found
			} = await import('@babel/plugin-transform-react-jsx');
			return {
				plugins: [jsx({}, { runtime: 'automatic', importSource: 'preact' })],
			};
		},
	};
}

function getCompatRenderer(development: boolean): AstroRenderer {
	return {
		name: '@astrojs/preact',
		clientEntrypoint: development ? '@astrojs/preact/client-dev.js' : '@astrojs/preact/client.js',
		serverEntrypoint: '@astrojs/preact/server.js',
		jsxImportSource: 'react',
		jsxTransformOptions: async () => {
			const {
				default: { default: jsx },
				// @ts-expect-error types not found
			} = await import('@babel/plugin-transform-react-jsx');
			return {
				plugins: [
					jsx({}, { runtime: 'automatic', importSource: 'preact/compat' }),
					[
						'babel-plugin-module-resolver',
						{
							alias: {
								react: 'preact/compat',
								'react-dom/test-utils': 'preact/test-utils',
								'react-dom': 'preact/compat',
								'react/jsx-runtime': 'preact/jsx-runtime',
							},
						},
					],
				],
			};
		},
	};
}

function getViteConfiguration(compat?: boolean): ViteUserConfig {
	const viteConfig: ViteUserConfig = {
		optimizeDeps: {
			include: ['@astrojs/preact/client.js', 'preact', 'preact/jsx-runtime'],
			exclude: ['@astrojs/preact/server.js'],
		},
	};

	if (compat) {
		viteConfig.optimizeDeps!.include!.push(
			'preact/compat',
			'preact/test-utils',
			'preact/compat/jsx-runtime'
		);
		viteConfig.resolve = {
			alias: [
				{ find: 'react', replacement: 'preact/compat' },
				{ find: 'react-dom/test-utils', replacement: 'preact/test-utils' },
				{ find: 'react-dom', replacement: 'preact/compat' },
				{ find: 'react/jsx-runtime', replacement: 'preact/jsx-runtime' },
			],
			dedupe: ['preact/compat', 'preact'],
		};
		// noExternal React entrypoints to be bundled, resolved, and aliased by Vite
		viteConfig.ssr = {
			noExternal: ['react', 'react-dom', 'react-dom/test-utils', 'react/jsx-runtime'],
		};
	}

	return viteConfig;
}

export default function ({ compat }: { compat?: boolean } = {}): AstroIntegration {
	return {
		name: '@astrojs/preact',
		hooks: {
			'astro:config:setup': ({ addRenderer, updateConfig, command }) => {
				const development = command === 'dev';
				if (compat) addRenderer(getCompatRenderer(development));
				addRenderer(getRenderer(development));
				updateConfig({
					vite: getViteConfiguration(compat),
				});
			},
		},
	};
}