Skip to content

Commit

Permalink
Use resolvePackage instead of resolveModulePath in `resolveHasteN…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
kraenhansen authored and robhogan committed Jan 9, 2024
1 parent e6562f3 commit 04ed99a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
18 changes: 18 additions & 0 deletions packages/metro-resolver/src/__tests__/package-exports-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,24 @@ describe('with package exports resolution enabled', () => {
expect(logWarning).not.toHaveBeenCalled();
});
});

describe('haste package', () => {
test('should resolve subpath in "exports"', () => {
const context = {
...baseContext,
resolveHastePackage(name: string) {
if (name === 'test-pkg') {
return '/root/node_modules/test-pkg/package.json';
}
return null;
},
};
expect(Resolver.resolve(context, 'test-pkg/foo.js', null)).toEqual({
type: 'sourceFile',
filePath: '/root/node_modules/test-pkg/lib/foo.js',
});
});
});
});

describe('subpath patterns', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/metro-resolver/src/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ function resolveHasteName(
const packageDirPath = path.dirname(packageJsonPath);
const pathInModule = moduleName.substring(packageName.length + 1);
const potentialModulePath = path.join(packageDirPath, pathInModule);
const result = resolveModulePath(context, potentialModulePath, platform);
const result = resolvePackage(context, potentialModulePath, platform);
if (result.type === 'resolved') {
return result;
}
Expand Down

0 comments on commit 04ed99a

Please sign in to comment.