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

File extension pattern matching in React Native 0.62.4 for mjs cjs main and module files #46

Closed
HowardLin114 opened this issue Jun 25, 2021 · 4 comments

Comments

@HowardLin114
Copy link

@bhousel Thank you very much for the help. Just one small thing left before package is fully ready for RN again.

Yes browser export was causing issues, and I get the actual root issue now aside from browser export.

It's the file extension pattern matching.

This is v4.1.0 package.json, it conforms to pattern

"main": "./dist/country-coder.js",
"module": "./dist/country-coder.module.js",

it conforms to this pattern that React-Native wants, notice the .js extension (.native|.ios.js|.native.js|.js|.ios.json|.native.json|.json|.ios.ts|.native.ts|.ts|.ios.tsx|.native.tsx|.tsx|.ios.svg|.native.svg|.svg), without this Xcode will not build.

However in v5.0.3, it becomes

"main": "./dist/country-coder.cjs",
"module": "./dist/country-coder.mjs",

If I just added a .js to both of these files (RN uses module but both pattern matching was needed)

"main": "./dist/country-coder.cjs.js",
"module": "./dist/country-coder.mjs.js",

This works perfectly. All features are now back online. It's silly, but the pattern matching is strict.

@bhousel
Copy link
Collaborator

bhousel commented Jun 25, 2021

This is a known issue with metro: facebook/metro#535

It's going to be more of an issue going forward as maintainers upgrade their JavaScript projects to ESM. The meaning of the .js file extension is ambiguous in Node - it depends on whether the package maintainer has specified "type": "module" (by default, js files are treated as ESM) or "type": "commonjs" (by default, js files are treated as cjs).

So whichever a maintainer picks, we need those .cjs or .mjs extensions to support the other way of doing things, at least for a while.

@bhousel
Copy link
Collaborator

bhousel commented Jun 25, 2021

Also I browsed the commits linked to that metro issue, it looks like you might be adjust the metro config as a workaround:
voidrender/demo-react-native-emotion@d868225

@HowardLin114
Copy link
Author

Amazing explanation and thank you for the action item. The facebook metro team should really permanently fix this from their side so people can upgrade long term.

I confirm it is now working as expected this link you posted, and it works beautifully.

Have a great day! Love the package, and keep up the amazing work!

@bhousel
Copy link
Collaborator

bhousel commented Jun 25, 2021

Have a great day! Love the package, and keep up the amazing work!

Thank you! I'm happy you are finding it useful!

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

2 participants