2022-03-18 22:35:45 +00:00
|
|
|
import { AstroIntegration } from 'astro';
|
2022-03-31 16:51:29 +00:00
|
|
|
import { version as ReactVersion } from 'react-dom';
|
2022-03-18 22:35:45 +00:00
|
|
|
|
|
|
|
function getRenderer() {
|
|
|
|
return {
|
|
|
|
name: '@astrojs/react',
|
2022-03-31 16:51:29 +00: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 22:35:45 +00:00
|
|
|
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',
|
2022-03-31 16:51:29 +00:00
|
|
|
// This option tells the JSX transform how to construct the "*/jsx-runtime" import.
|
|
|
|
// In React v17, we had to shim this due to an export map issue in React.
|
|
|
|
// In React v18, this issue was fixed and we can import "react/jsx-runtime" directly.
|
|
|
|
// See `./jsx-runtime.js` for more details.
|
|
|
|
importSource: ReactVersion.startsWith('18.') ? 'react' : '@astrojs/react',
|
2022-03-18 22:35:45 +00:00
|
|
|
}
|
|
|
|
),
|
|
|
|
],
|
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function getViteConfiguration() {
|
|
|
|
return {
|
|
|
|
optimizeDeps: {
|
2022-03-31 16:51:29 +00: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:01:05 +00:00
|
|
|
exclude: [ReactVersion.startsWith('18.') ? '@astrojs/react/server.js' : '@astrojs/react/server-v17.js'],
|
2022-03-18 22:35:45 +00:00
|
|
|
},
|
|
|
|
resolve: {
|
|
|
|
dedupe: ['react', 'react-dom'],
|
|
|
|
},
|
|
|
|
ssr: {
|
2022-03-31 16:51:29 +00:00
|
|
|
external: ReactVersion.startsWith('18.') ? ['react-dom/server', 'react-dom/client'] : ['react-dom/server.js', 'react-dom/client.js'],
|
2022-03-18 22:35:45 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function (): AstroIntegration {
|
|
|
|
return {
|
|
|
|
name: '@astrojs/react',
|
|
|
|
hooks: {
|
|
|
|
'astro:config:setup': ({ addRenderer, updateConfig }) => {
|
|
|
|
addRenderer(getRenderer());
|
|
|
|
updateConfig({ vite: getViteConfiguration() });
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|