-
Notifications
You must be signed in to change notification settings - Fork 637
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
Package Exports not working #1128
Comments
Reproducible example |
Hi! I think i discovered the issue and I have a proposal for a solution but I need your help! The error is easily reproducible with the examples from the repo I posted above in the issue. The problem I found is in the Whenever you have a module which is using the "new" exports way of exposing its content, like this in "exports": {
"./native": {
"types": "./lib/native/index.d.ts",
"import": "./lib/native/index.mjs",
"require": "./lib/native/index.cjs"
},
"./web": {
"types": "./lib/web/index.d.ts",
"import": "./lib/web/index.mjs",
"require": "./lib/web/index.cjs"
},
"./package.json": "./package.json"
}, and then you are consuming this module like this: the My Solution My function: function isExportedSource(context, moduleName) {
if (context.unstable_enablePackageExports) {
let packageName = moduleName;
let packageJsonPath = context.resolveHastePackage(packageName);
while (packageJsonPath == null && packageName && packageName !== ".") {
packageName = _path.default.dirname(packageName);
packageJsonPath = context.resolveHastePackage(packageName);
}
if (packageJsonPath == null) {
return failedFor();
}
if (packageJsonPath) {
const pkg = context.getPackageForModule(packageJsonPath);
const exportsField = pkg?.packageJson.exports;
const packageName = pkg?.packageJson.name;
const exportName = moduleName.replace(packageName, '');
return Object.keys(exportsField).some((item) => item.includes(exportName));
}
}
return false;
} and should be used in the condition which wraps the call for resolving const isExportedSrc = isExportedSource(context, normalizePath(realModuleName));
if (context.allowHaste && !isDirectImport && !isExportedSrc) {
const normalizedName = normalizePath(realModuleName);
const result = resolveHasteName(context, normalizedName, platform);
if (result.type === "resolved") {
return result.resolution;
}
} When I have this in place, all modules all resolved correctly and my app builds without issues :) Any thoughts? Should I create a proposal PR for you guys? PLEASE let me know :) :) Thank you very much! |
One additional important note... If in the example above, I remove |
@Bekaxp I wanted to verify your fix: Where do I put / call the |
Hi @kraenhansen, thanks for trying it out. Once you install all the dependencies of the repro above, you should have in the node_modules a package named I did some more testings with my function and is not perfect because actually some modules should be considered as For now I solved the problem with exporting our packages in a different way (package.json example): {
"react-native": "./lib/native/index.mjs",
"exports": {
".": {
"types": "./lib/native/index.d.ts",
},
"./native": {
"types": "./lib/native/index.d.ts",
"import": "./lib/native/index.mjs",
"require": "./lib/native/index.cjs"
},
"./web": {
"types": "./lib/web/index.d.ts",
"import": "./lib/web/index.mjs",
"require": "./lib/web/index.cjs"
},
"./package.json": "./package.json"
},
} Of course then we need to import it from Thank you again for trying it out! |
I agree. As a React Native library developer, I'm experiencing the same issue. I verified that simply disabling the It definitely looks like this code is thoroughly tested by packages/metro-resolver/src/tests/package-exports-test.js. I've found |
I think I understand the issue better now. This happens only when the package being imported is in the haste map, because it's in the list of watchFolders. I've added a failing test to main...kraenhansen:metro:fixing-1128 showing how this is broken and I'll try to see if I can contribute a fix for it too 🙏 |
Well, that wasn't too difficult: #1136. |
@kraenhansen OMG!!! You did it! It is fixed! It works 😃😃😃. I will patch it for our projects but I guess the next version will already have your fix in place. Should I close the issue or leave it open until your PR is merged in? Thank you very much for this! |
Please leave it open 🙂 I expect it to close as the PR merge 🙏 |
…ame` (#1136) Summary: This fixes #1128 by calling the `resolvePackage` instead of `resolveModulePath` in `resolveHasteName`. Only `resolvePackage` has the code to resolve package "exports" and it calls `resolveModulePath` as a fallback. Changelog: [Experimental] When enabled, the `"exports"` field is now considered for Haste packages (which could include local monorepo packages) Pull Request resolved: #1136 Test Plan: I've added a failing test which passed as the fix got implemented. Reviewed By: huntie Differential Revision: D51346769 Pulled By: robhogan fbshipit-source-id: 8a003d5b147b73d344365db7cff8187ff946013d
…ame` (#1136) Summary: This fixes #1128 by calling the `resolvePackage` instead of `resolveModulePath` in `resolveHasteName`. Only `resolvePackage` has the code to resolve package "exports" and it calls `resolveModulePath` as a fallback. Changelog: [Experimental] When enabled, the `"exports"` field is now considered for Haste packages (which could include local monorepo packages) Pull Request resolved: #1136 Test Plan: I've added a failing test which passed as the fix got implemented. Reviewed By: huntie Differential Revision: D51346769 Pulled By: robhogan fbshipit-source-id: 8a003d5b147b73d344365db7cff8187ff946013d
…ame` (#1136) Summary: This fixes #1128 by calling the `resolvePackage` instead of `resolveModulePath` in `resolveHasteName`. Only `resolvePackage` has the code to resolve package "exports" and it calls `resolveModulePath` as a fallback. Changelog: [Experimental] When enabled, the `"exports"` field is now considered for Haste packages (which could include local monorepo packages) Pull Request resolved: #1136 Test Plan: I've added a failing test which passed as the fix got implemented. Reviewed By: huntie Differential Revision: D51346769 Pulled By: robhogan fbshipit-source-id: 8a003d5b147b73d344365db7cff8187ff946013d
Do you want to request a feature or report a bug?
It could be a BUG or maybe I just need some help with the configuration.
What is the current behavior?
exports
in thepackage.json
unstable_enablePackageExports
enabledyarn start
ornpm start
it explodes with an error:If the current behavior is a bug, please provide the steps to reproduce and a minimal repository on GitHub that we can
yarn install
andyarn test
.What is the expected behavior?
I would expect that the packages are resolved correctly and the app starts without issues.
Please provide your exact Metro configuration and mention your Metro, node, yarn/npm version and operating system.
My metro config looks like this:
I am running it on a MacOS Sonoma 14.0 (Apple M1 Pro)
Thank you very much for any tips and tricks to make it work :)
The text was updated successfully, but these errors were encountered: