react-spectrum icon indicating copy to clipboard operation
react-spectrum copied to clipboard

@react-aria/toast causing Dynamic require of "react" is not supported

Open kysley opened this issue 3 years ago โ€ข 3 comments

๐Ÿ› Bug Report

When instantiating a new ToastQueue, or using the useToastState hook, I get a runtime error Dynamic require of "react" is not supported.

๐Ÿค” Expected Behavior

There should be no runtime error when using Toasts.

๐Ÿ˜ฏ Current Behavior

image

๐Ÿ’ Possible Solution

I tried to externalize react, which initially removed the Dynamic require error, but it led to having 2 versions of react in the DOM.

๐Ÿ”ฆ Context

We are trying to make use of @react-aria/toast to handle Toasts for our design system.

I feel as if this could be related to using esbuild/tsup? Here is our config if that may alude to anything:

export default defineConfig({
	outDir: "dist",
	format: ["esm", "cjs"],
	dts: true,
	platform: "browser",
	external: ["react"],
	clean: true,
	splitting: true,
	treeshake: true,
	entry: ["src/**/*.ts"],
	esbuildPlugins: [svgrPlugin(), dynamicImport()],
});

๐Ÿ’ป Code Sample

This is enough to cause the runtime error.

const toastQueue = new ToastQueue<ReactNode>({
	maxVisibleToasts: 5,
});

Another way is:

const state = useToastState<ReactNode>({
    maxVisibleToasts: 5,
});

๐ŸŒ Your Environment

Software Version(s)
react-aria 3.26.0
react-stately 3.24.0
Browser Firefox Dev 116
Operating System Windows 10
@react-aria/toast 3.0.0-beta.2

๐Ÿงข Your Company/Team

Lightspeed HQ

๐Ÿ•ท Tracking Issue (optional)

n/a

kysley avatar Jul 26 '23 14:07 kysley

I don't think anything in our code is doing a dynamic require, but maybe related to the external: ["react"] in your config? Not really familiar with esbuild though.

devongovett avatar Jul 29 '23 02:07 devongovett

Appreciate you getting back! I haven't had a chance to debug further :( Marking react as external here means that esbuild assumes the module is available at runtime. So react doesn't need to be bundled with our package. This is why without external, our tests resulted in 2 versions of React in the dom.

I'd be happy to try any ideas, otherwise I think its safe to call this a build tooling issue and you can close ๐Ÿ™‚

kysley avatar Aug 01 '23 00:08 kysley

Appreciate you getting back! I haven't had a chance to debug further :( Marking react as external here means that esbuild assumes the module is available at runtime. So react doesn't need to be bundled with our package. This is why without external, our tests resulted in 2 versions of React in the dom.

I'd be happy to try any ideas, otherwise I think its safe to call this a build tooling issue and you can close ๐Ÿ™‚

@kysley Did you ever figure out a solution for this? It seems like these components are meant for building component libraries, but they do not seem to be compatible with bundling tools like esbuild or tsup. I am finding it very difficult to build a reusable component.

steveg152 avatar Jun 07 '24 13:06 steveg152