Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/metro-resolver/src/__tests__/package-scope-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,28 @@ describe('package scope lookups', () => {
]);
});

test('relative file, when a candidate is a package root', () => {
useFileMap({
...fileMap,
// A package whose "browser" field applies to its own directory
[p('/root/src/bar.js/package.json')]: JSON.stringify({
name: 'bar.js',
browser: {'./': './replacement.js'},
}),
[p('/root/src/bar.js/replacement.js')]: '',
});
expect(Resolver.resolve(context, './bar', 'ios')).toEqual({
type: 'sourceFile',
filePath: p('/root/src/bar.js/replacement.js'),
});
// The directory candidate is looked up as a module of its own
expect(lookedUpPaths()).toEqual([
p('/root/src/bar'),
filesIn('/root/src'),
p('/root/src/bar.js'),
]);
});

test('throws if the scope is never that of the stand-in file', () => {
getPackageForModule = jest.fn(() => ({
rootPath: p('/root'),
Expand Down
36 changes: 22 additions & 14 deletions packages/metro-resolver/src/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -839,21 +839,29 @@ function resolveSourceFileForExt(
extension: string,
): SourceFileResolution {
const filePath = `${context.filePathPrefix}${extension}`;
const {candidatePackageScope} = context;
const redirectedPath =
// Any redirections for the bare path have already happened
extension !== '' && candidatePackageScope != null
? redirectPackageSubpath(
context,
filePath,
candidatePackageScope,
candidatePackageScope.packageRelativePath + extension,
)
: filePath;
if (redirectedPath === false) {
return {type: 'empty'};
let lookupResult = context.fileSystemLookup(filePath);
// Any redirections for the bare path have already happened
if (extension !== '') {
const {candidatePackageScope} = context;
const redirectedPath =
lookupResult.exists && lookupResult.type === 'd'
? // A directory may be a package root, i.e. its own package scope
redirectModulePath(context, filePath)
: candidatePackageScope != null
? redirectPackageSubpath(
context,
filePath,
candidatePackageScope,
candidatePackageScope.packageRelativePath + extension,
)
: filePath;
if (redirectedPath === false) {
return {type: 'empty'};
}
if (redirectedPath !== filePath) {
lookupResult = context.fileSystemLookup(redirectedPath);
}
}
const lookupResult = context.fileSystemLookup(redirectedPath);
if (lookupResult.exists && lookupResult.type === 'f') {
return lookupResult.realPath;
}
Expand Down
Loading