summaryrefslogtreecommitdiff
path: root/packages/integrations/react/src/index.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/integrations/react/src/index.ts')
-rw-r--r--packages/integrations/react/src/index.ts44
1 files changed, 16 insertions, 28 deletions
diff --git a/packages/integrations/react/src/index.ts b/packages/integrations/react/src/index.ts
index d7906fe4a..da008a670 100644
--- a/packages/integrations/react/src/index.ts
+++ b/packages/integrations/react/src/index.ts
@@ -1,5 +1,10 @@
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';
+
+
+const FAST_REFRESH_PREAMBLE = react.preambleCode;
function getRenderer() {
return {
@@ -10,33 +15,10 @@ function getRenderer() {
serverEntrypoint: ReactVersion.startsWith('18.')
? '@astrojs/react/server.js'
: '@astrojs/react/server-v17.js',
- jsxImportSource: 'react',
- jsxTransformOptions: async () => {
- // @ts-expect-error types not found
- const babelPluginTransformReactJsxModule = await import('@babel/plugin-transform-react-jsx');
- const jsx =
- babelPluginTransformReactJsxModule?.default?.default ??
- babelPluginTransformReactJsxModule?.default;
- return {
- plugins: [
- jsx(
- {},
- {
- runtime: 'automatic',
- // 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',
- }
- ),
- ],
- };
- },
};
}
-function getViteConfiguration() {
+function getViteConfiguration({include, exclude}: Options = {}) {
return {
optimizeDeps: {
include: [
@@ -54,8 +36,9 @@ function getViteConfiguration() {
: '@astrojs/react/server-v17.js',
],
},
+ plugins: [react({include, exclude})],
resolve: {
- dedupe: ['react', 'react-dom'],
+ dedupe: ['react', 'react-dom', 'react-dom/server'],
},
ssr: {
external: ReactVersion.startsWith('18.')
@@ -73,13 +56,18 @@ function getViteConfiguration() {
};
}
-export default function (): AstroIntegration {
+export type Options =Pick<ViteReactPluginOptions, 'include' | 'exclude'>;
+export default function ({include, exclude}: Pick<ViteReactPluginOptions, 'include' | 'exclude'> = {}): AstroIntegration {
return {
name: '@astrojs/react',
hooks: {
- 'astro:config:setup': ({ addRenderer, updateConfig }) => {
+ 'astro:config:setup': ({ config, command, addRenderer, updateConfig, injectScript }) => {
addRenderer(getRenderer());
- updateConfig({ vite: getViteConfiguration() });
+ updateConfig({ vite: getViteConfiguration({include, exclude}) });
+ if (command === 'dev') {
+ const preamble = FAST_REFRESH_PREAMBLE.replace(`__BASE__`, appendForwardSlash(config.base))
+ injectScript('before-hydration', preamble);
+ }
},
},
};