From 58296db87780928a7db3dbff19c002832c6df795 Mon Sep 17 00:00:00 2001 From: Lenz Weber-Tronic Date: Wed, 23 Aug 2023 05:59:29 +0200 Subject: [PATCH] add an extra entrypoint for React Server Components --- package.json | 1 + src/index-rsc.ts | 34 ++++++++++++++++++++++++++++++++++ tsup.config.ts | 10 ++++++++++ 3 files changed, 45 insertions(+) create mode 100644 src/index-rsc.ts diff --git a/package.json b/package.json index 5ebdad14d..b36063a0f 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "./package.json": "./package.json", ".": { "types": "./dist/react-redux.d.ts", + "react-server": "./dist/rsc.mjs", "import": "./dist/react-redux.mjs", "default": "./dist/cjs/index.js" }, diff --git a/src/index-rsc.ts b/src/index-rsc.ts new file mode 100644 index 000000000..625bff76e --- /dev/null +++ b/src/index-rsc.ts @@ -0,0 +1,34 @@ +import type * as normal from './index' +import type * as rsc from './index-rsc' + +// checks to make sure we didn't forgot to replicate any exports + +// eslint-disable-next-line @typescript-eslint/no-unused-vars +const _check: typeof normal = {} as typeof rsc +// eslint-disable-next-line @typescript-eslint/no-unused-vars +const _check2: typeof rsc = {} as typeof normal + +// ------------------------------------------------------------------------------------- + +function throwNotSupportedError( + // eslint-disable-next-line @typescript-eslint/no-unused-vars + ...args: any[] +): any { + throw new Error( + 'This function is not supported in React Server Components. Please only use this export in a Client Component.' + ) +} + +export { + throwNotSupportedError as batch, + throwNotSupportedError as Provider, + throwNotSupportedError as connect, + throwNotSupportedError as useSelector, + throwNotSupportedError as useDispatch, + throwNotSupportedError as useStore, + throwNotSupportedError as createDispatchHook, + throwNotSupportedError as createSelectorHook, + throwNotSupportedError as createStoreHook, +} +export const ReactReduxContext = {} as any +export { default as shallowEqual } from './utils/shallowEqual' diff --git a/tsup.config.ts b/tsup.config.ts index 67a675251..81a61b1c2 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -34,6 +34,16 @@ export default defineConfig((options) => { dts: true, clean: true, }, + // ESM for RSC + { + ...commonOptions, + entry: { + rsc: 'src/index-rsc.ts', + }, + format: ['esm'], + outExtension: () => ({ js: '.mjs' }), + dts: false, + }, // Support Webpack 4 by pointing `"module"` to a file with a `.js` extension { ...commonOptions,