summaryrefslogtreecommitdiff
path: root/packages/integrations/react/src/index.ts
blob: a318bb4c23f0defc66e2d747b10bf5e19d92f0c8 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import type { AstroIntegration } from 'astro';
import { version as ReactVersion } from 'react-dom';
import react, { type Options as ViteReactPluginOptions } from '@vitejs/plugin-react';
import { appendForwardSlash } from '@astrojs/internal-helpers/path';
import type * as vite from 'vite';

const FAST_REFRESH_PREAMBLE = react.preambleCode;




function getRenderer() {
	return {
		name: '@astrojs/react',
		clientEntrypoint: ReactVersion.startsWith('18.')
			? '@astrojs/react/client.js'
			: '@astrojs/react/client-v17.js',
		serverEntrypoint: ReactVersion.startsWith('18.')
			? '@astrojs/react/server.js'
			: '@astrojs/react/server-v17.js',
	};
}

function optionsPlugin(experimentalReactChildren: boolean): vite.Plugin {
	const virtualModule = 'astro:react:opts';
	const virtualModuleId = '\0' + virtualModule;
	return {
		name: '@astrojs/react:opts',
		resolveId(id) {
			if (id === virtualModule) {
				return virtualModuleId;
			}
		},
		load(id) {
			if (id === virtualModuleId) {
				return {
					code: `export default {
						experimentalReactChildren: ${JSON.stringify(experimentalReactChildren)}
					}`,
				};
			}
		},
	};
}

function getViteConfiguration(experimentalReactChildren: boolean, { include, exclude }: Options = {}) {
	return {
		optimizeDeps: {
			include: [
				ReactVersion.startsWith('18.')
					? '@astrojs/react/client.js'
					: '@astrojs/react/client-v17.js',
				'react',
				'react/jsx-runtime',
				'react/jsx-dev-runtime',
				'react-dom',
			],
			exclude: [
				ReactVersion.startsWith('18.')
					? '@astrojs/react/server.js'
					: '@astrojs/react/server-v17.js',
			],
		},
		plugins: [
			react({ include, exclude }),
			optionsPlugin(experimentalReactChildren)
		],
		resolve: {
			dedupe: ['react', 'react-dom', 'react-dom/server'],
		},
		ssr: {
			external: ReactVersion.startsWith('18.')
				? ['react-dom/server', 'react-dom/client']
				: ['react-dom/server.js', 'react-dom/client.js'],
			noExternal: [
				// These are all needed to get mui to work.
				'@mui/material',
				'@mui/base',
				'@babel/runtime',
				'redoc',
				'use-immer',
			],
		},
	};
}

export type ReactIntegrationOptions = Pick<ViteReactPluginOptions, 'include' | 'exclude'> & {
	experimentalReactChildren: boolean;
};
export default function ({
	include,
	exclude,
	experimentalReactChildren
}: ReactIntegrationOptions = {
	experimentalReactChildren: false
}): AstroIntegration {
	return {
		name: '@astrojs/react',
		hooks: {
			'astro:config:setup': ({ config, command, addRenderer, updateConfig, injectScript }) => {
				addRenderer(getRenderer());
				updateConfig({ vite: getViteConfiguration(experimentalReactChildren, { include, exclude }) });
				if (command === 'dev') {
					const preamble = FAST_REFRESH_PREAMBLE.replace(
						`__BASE__`,
						appendForwardSlash(config.base)
					);
					injectScript('before-hydration', preamble);
				}
			},
		},
	};
}