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

Add exports entry point to support node's module resolution. #9452

Open
otaviosoares opened this issue Nov 16, 2023 · 5 comments
Open

Add exports entry point to support node's module resolution. #9452

otaviosoares opened this issue Nov 16, 2023 · 5 comments

Comments

@otaviosoares
Copy link

React admin provides cjs and esm bundles. However, it relies on main and module in the package.json file to decide which version to use. It will work with most bundlers but fails when using node's module resolution as it doesn't support the module field. I was hit with this problem when trying to test a component using vitest.

Example:

function ComponentA() {
   return <SimpleForm><ComponentB /></SimpleForm>
}

function ComponentB() {
   const { watch } = useFormContext()
   return ...
}

SimpleForm is loaded using cjs and uses FormProvider (cjs) from react-hook-form.
ComponentB uses useFormContext from react-hook-form esm bundle and context will be null.

Describe the solution you'd like
Add exports to package.json specifying the entry points for import and require.

Additional context
vitest-dev/vitest#4233
https://nodejs.org/api/packages.html#package-entry-points

@djhi
Copy link
Contributor

djhi commented Nov 16, 2023

I successfully tried https://github.com/isaacs/tshy/ on another project. Might be worth investigating

@otaviosoares
Copy link
Author

hey @djhi thanks for your suggestion. I don't see how tshy could help in this case, though. react-admin already supports esm and cjs, the only missing piece is the exports field. Am I missing something?

@djhi
Copy link
Contributor

djhi commented Nov 17, 2023

It does what we currently do but better and would handle the exports for us

@otaviosoares
Copy link
Author

otaviosoares commented Nov 17, 2023

I see, I thought you meant for it to be fixed on userland, my mistake.

I took a look at tshy but I'm not really sure how to tackle it. It seems very opinionated where it forces us to use moduleResolution: 'nodenext'. This won't allow it to build bc it requires to add file extensions to imports.

Please, correct me if I'm wrong, but since we already have esm and cjs builds, manually adding exports to package.json seems to be a simpler fix.

@djhi
Copy link
Contributor

djhi commented Nov 18, 2023

Indeed, that's probably what we'll do for now.

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

No branches or pull requests

3 participants