Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"useId".toString() import error when using rollup to package components and then use webpack with react 17 #2796

Open
deepjoy opened this issue Mar 23, 2024 · 1 comment

Comments

@deepjoy
Copy link
Contributor

deepjoy commented Mar 23, 2024

Bug report

Current Behavior

When a component library using radix-ui/primitives is packaged with

  • vite@^5.1.5
  • esbuild@^0.19.3
  • react@^17.0.2

const useReactId = (React as any)['useId'.toString()] || (() => undefined);
from the codebase is generated as

const V1 = React.useId || (() ...

when this library is then consumed by an App using

  • webpack@5.89.0
  • react@17.0.2

it trigger webpack/webpack#14814

Expected behavior

Using react@17 at both component and application level should not result in compilation errors when using webpack in the final step.

Reproducible example

N/A

Suggested solution

Since

const useReactId = (React as any)['useId'.toString()] || (() => undefined);
was introduced to avoid exactly this scenario I would suggest updating this to a more esoteric code snippet such as

const useReactId = (React as any)[' useId '.trim().toString()] || (() => undefined);

I have verified that this currently works with the esbuild and webpack versions listed above by using the rollup-replace plugin with this configuration

  [
    replace({
      values: {
          "'useId'.toString()": "' useId '.trim().toString()",
      },
      delimiters: [ "", ""],
   })
 ]

Additional context

N/A

Your environment

Software Name(s) Version
Radix Package(s) @radix-ui/react-id 1.0.1
React n/a 17.0.2
Browser any
Assistive tech webpack @5
Node n/a 18.19.0
npm/yarn pnpm 8.13.1
Operating System OSX
@deepjoy
Copy link
Contributor Author

deepjoy commented Mar 23, 2024

I found a similar bug report on mui

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant