summaryrefslogtreecommitdiff
path: root/packages/integrations/react/src
diff options
context:
space:
mode:
authorGravatar Matthew Phillips <matthew@skypack.dev> 2024-05-07 13:13:03 -0400
committerGravatar GitHub <noreply@github.com> 2024-05-07 13:13:03 -0400
commitd47baa466aaeedde9c79ed5375d0be34762ac8b6 (patch)
treebc12edbce6cde5150ec4d654dcee1e20c8a95513 /packages/integrations/react/src
parent4b693c0476f04a6956208a5df42c52a70a559fda (diff)
downloadastro-d47baa466aaeedde9c79ed5375d0be34762ac8b6.tar.gz
astro-d47baa466aaeedde9c79ed5375d0be34762ac8b6.tar.zst
astro-d47baa466aaeedde9c79ed5375d0be34762ac8b6.zip
Support React 19 (#10942)
* Support React 19 * Fix lint * Update .changeset/short-phones-breathe.md * fix: update types peer dep --------- Co-authored-by: bholmesdev <hey@bholmes.dev>
Diffstat (limited to 'packages/integrations/react/src')
-rw-r--r--packages/integrations/react/src/index.ts67
1 files changed, 48 insertions, 19 deletions
diff --git a/packages/integrations/react/src/index.ts b/packages/integrations/react/src/index.ts
index e0149e8e7..62803e788 100644
--- a/packages/integrations/react/src/index.ts
+++ b/packages/integrations/react/src/index.ts
@@ -12,15 +12,44 @@ export type ReactIntegrationOptions = Pick<
const FAST_REFRESH_PREAMBLE = react.preambleCode;
-function getRenderer() {
+const versionsConfig = {
+ 17: {
+ server: '@astrojs/react/server-v17.js',
+ client: '@astrojs/react/client-v17.js',
+ externals: ['react-dom/server.js', 'react-dom/client.js'],
+ },
+ 18: {
+ server: '@astrojs/react/server.js',
+ client: '@astrojs/react/client.js',
+ externals: ['react-dom/server', 'react-dom/client']
+ },
+ 19: {
+ server: '@astrojs/react/server.js',
+ client: '@astrojs/react/client.js',
+ externals: ['react-dom/server', 'react-dom/client']
+ }
+};
+
+type SupportedReactVersion = keyof (typeof versionsConfig);
+type ReactVersionConfig = (typeof versionsConfig)[SupportedReactVersion];
+
+function getReactMajorVersion(): number {
+ const matches = /\d+\./.exec(ReactVersion);
+ if(!matches) {
+ return NaN;
+ }
+ return Number(matches[0]);
+}
+
+function isUnsupportedVersion(majorVersion: number) {
+ return majorVersion < 17 || majorVersion > 19 || Number.isNaN(majorVersion);
+}
+
+function getRenderer(reactConfig: ReactVersionConfig) {
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',
+ clientEntrypoint: reactConfig.client,
+ serverEntrypoint: reactConfig.server,
};
}
@@ -51,22 +80,18 @@ function getViteConfiguration({
exclude,
babel,
experimentalReactChildren,
-}: ReactIntegrationOptions = {}) {
+}: ReactIntegrationOptions = {}, reactConfig: ReactVersionConfig) {
return {
optimizeDeps: {
include: [
- ReactVersion.startsWith('18.')
- ? '@astrojs/react/client.js'
- : '@astrojs/react/client-v17.js',
+ reactConfig.client,
'react',
'react/jsx-runtime',
'react/jsx-dev-runtime',
'react-dom',
],
exclude: [
- ReactVersion.startsWith('18.')
- ? '@astrojs/react/server.js'
- : '@astrojs/react/server-v17.js',
+ reactConfig.server,
],
},
plugins: [react({ include, exclude, babel }), optionsPlugin(!!experimentalReactChildren)],
@@ -74,9 +99,7 @@ function getViteConfiguration({
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'],
+ external: reactConfig.externals,
noExternal: [
// These are all needed to get mui to work.
'@mui/material',
@@ -95,13 +118,19 @@ export default function ({
babel,
experimentalReactChildren,
}: ReactIntegrationOptions = {}): AstroIntegration {
+ const majorVersion = getReactMajorVersion();
+ if(isUnsupportedVersion(majorVersion)) {
+ throw new Error(`Unsupported React version: ${majorVersion}.`);
+ }
+ const versionConfig = versionsConfig[majorVersion as SupportedReactVersion];
+
return {
name: '@astrojs/react',
hooks: {
'astro:config:setup': ({ command, addRenderer, updateConfig, injectScript }) => {
- addRenderer(getRenderer());
+ addRenderer(getRenderer(versionConfig));
updateConfig({
- vite: getViteConfiguration({ include, exclude, babel, experimentalReactChildren }),
+ vite: getViteConfiguration({ include, exclude, babel, experimentalReactChildren }, versionConfig),
});
if (command === 'dev') {
const preamble = FAST_REFRESH_PREAMBLE.replace(`__BASE__`, '/');
Jérôme Benoit <jerome.benoit@sap.com> Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org> 2023-10-16Fix use before define bug in sqliteGravatar Ashcon Partovi 2-5/+5 Fixes #6481 2023-10-16fix(jest): fix toStrictEqual on same URLs (#6528)Gravatar João Alisson 2-13/+16 Fixes #6492 2023-10-16Fix `toHaveBeenCalled` having wrong error signatureGravatar Ashcon Partovi 1-2/+2 Fixes #6527 2023-10-16Fix formattingGravatar Ashcon Partovi 1-2/+1 2023-10-16Add `reusePort` to `Bun.serve` typesGravatar Ashcon Partovi 1-0/+9 2023-10-16Fix `request.url` having incorrect portGravatar Ashcon Partovi 4-1/+92 Fixes #6443 2023-10-16Remove uWebSockets header from Bun.serve responsesGravatar Ashcon Partovi 1-6/+6 2023-10-16Rename some testsGravatar Ashcon Partovi 3-0/+0 2023-10-16Fix #6467Gravatar Ashcon Partovi 2-3/+10 2023-10-16Update InternalModuleRegistryConstants.hGravatar Dylan Conway 1-3/+3 2023-10-16Development -> Contributing (#6538)Gravatar Colin McDonnell 2-1/+1 Co-authored-by: Colin McDonnell <colin@KennyM1.local> 2023-10-14fix(net/tls) fix pg hang on end + hanging on query (#6487)Gravatar Ciro Spaciari 3-8/+36 * fix pg hang on end + hanging on query * remove dummy function * fix node-stream * add test * fix test * return error in test * fix test use once instead of on * fix OOM * generated * 💅 * 💅 2023-10-13fix installing dependencies that match workspace versions (#6494)Gravatar Dylan Conway 4-2/+64 * check if dependency matches workspace version * test * Update lockfile.zig * set resolution to workspace package id 2023-10-13fix lockfile struct padding (#6495)Gravatar Dylan Conway 3-3/+18 * integrity padding * error message for bytes at end of struct