2023-03-10 16:19:57 +01:00
|
|
|
import type { AstroIntegration } from 'astro';
|
2022-03-31 09:51:29 -07:00
|
|
|
import { version as ReactVersion } from 'react-dom';
|
2023-08-11 10:05:02 -04:00
|
|
|
import react, {type Options as ViteReactPluginOptions} from '@vitejs/plugin-react';
|
|
|
|
import { appendForwardSlash } from '@astrojs/internal-helpers/path';
|
|
|
|
|
|
|
|
|
|
|
|
const FAST_REFRESH_PREAMBLE = react.preambleCode;
|
2022-03-18 15:35:45 -07:00
|
|
|
|
|
|
|
function getRenderer() {
|
|
|
|
return {
|
|
|
|
name: '@astrojs/react',
|
2022-03-31 09:51:29 -07:00
|
|
|
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',
|
2022-03-18 15:35:45 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-08-11 10:05:02 -04:00
|
|
|
function getViteConfiguration({include, exclude}: Options = {}) {
|
2022-03-18 15:35:45 -07:00
|
|
|
return {
|
|
|
|
optimizeDeps: {
|
2022-03-31 09:51:29 -07:00
|
|
|
include: [
|
|
|
|
ReactVersion.startsWith('18.')
|
|
|
|
? '@astrojs/react/client.js'
|
|
|
|
: '@astrojs/react/client-v17.js',
|
|
|
|
'react',
|
|
|
|
'react/jsx-runtime',
|
|
|
|
'react/jsx-dev-runtime',
|
|
|
|
'react-dom',
|
2022-03-31 17:02:05 +00:00
|
|
|
],
|
|
|
|
exclude: [
|
|
|
|
ReactVersion.startsWith('18.')
|
|
|
|
? '@astrojs/react/server.js'
|
|
|
|
: '@astrojs/react/server-v17.js',
|
2022-04-02 14:15:41 -06:00
|
|
|
],
|
2022-03-18 15:35:45 -07:00
|
|
|
},
|
2023-08-11 10:05:02 -04:00
|
|
|
plugins: [react({include, exclude})],
|
2022-03-18 15:35:45 -07:00
|
|
|
resolve: {
|
2023-08-11 10:05:02 -04:00
|
|
|
dedupe: ['react', 'react-dom', 'react-dom/server'],
|
2022-03-18 15:35:45 -07:00
|
|
|
},
|
|
|
|
ssr: {
|
2022-03-31 09:51:29 -07:00
|
|
|
external: ReactVersion.startsWith('18.')
|
|
|
|
? ['react-dom/server', 'react-dom/client']
|
|
|
|
: ['react-dom/server.js', 'react-dom/client.js'],
|
2022-10-07 15:54:25 -04:00
|
|
|
noExternal: [
|
|
|
|
// These are all needed to get mui to work.
|
2022-10-07 19:56:04 +00:00
|
|
|
'@mui/material',
|
|
|
|
'@mui/base',
|
|
|
|
'@babel/runtime',
|
2023-05-01 09:31:44 -04:00
|
|
|
'redoc',
|
2023-03-28 15:35:21 -04:00
|
|
|
'use-immer',
|
2022-10-07 19:56:04 +00:00
|
|
|
],
|
2022-03-18 15:35:45 -07:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-08-11 10:05:02 -04:00
|
|
|
export type Options =Pick<ViteReactPluginOptions, 'include' | 'exclude'>;
|
|
|
|
export default function ({include, exclude}: Pick<ViteReactPluginOptions, 'include' | 'exclude'> = {}): AstroIntegration {
|
2022-03-18 15:35:45 -07:00
|
|
|
return {
|
|
|
|
name: '@astrojs/react',
|
|
|
|
hooks: {
|
2023-08-11 10:05:02 -04:00
|
|
|
'astro:config:setup': ({ config, command, addRenderer, updateConfig, injectScript }) => {
|
2022-03-18 15:35:45 -07:00
|
|
|
addRenderer(getRenderer());
|
2023-08-11 10:05:02 -04:00
|
|
|
updateConfig({ vite: getViteConfiguration({include, exclude}) });
|
|
|
|
if (command === 'dev') {
|
|
|
|
const preamble = FAST_REFRESH_PREAMBLE.replace(`__BASE__`, appendForwardSlash(config.base))
|
|
|
|
injectScript('before-hydration', preamble);
|
|
|
|
}
|
2022-03-18 15:35:45 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|