Skip to content

Commit cd25c2b

Browse files
huntiefacebook-github-bot
authored andcommitted
Update exports resolution to ignore absolute and relative imports
Summary: Changelog: **[Experimental]** Fix bug where `"exports"` field would be used on relative imports within a package Reviewed By: motiz88 Differential Revision: D43665370 fbshipit-source-id: dfed117b0f6d6e8bed1469cb2ece0f241cf079e7
1 parent 7e92227 commit cd25c2b

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

packages/metro-resolver/src/__tests__/package-exports-test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ describe('with package exports resolution enabled', () => {
243243
'.': './index.js',
244244
'./foo.js': './lib/foo.js',
245245
'./baz': './node_modules/baz/index.js',
246+
'./metadata.json': './metadata.min.json',
246247
},
247248
}),
248249
'/root/node_modules/test-pkg/index.js': '',
@@ -253,6 +254,8 @@ describe('with package exports resolution enabled', () => {
253254
'/root/node_modules/test-pkg/lib/foo.ios.js': '',
254255
'/root/node_modules/test-pkg/private/bar.js': '',
255256
'/root/node_modules/test-pkg/node_modules/baz/index.js': '',
257+
'/root/node_modules/test-pkg/metadata.json': '',
258+
'/root/node_modules/test-pkg/metadata.min.json': '',
256259
}),
257260
originModulePath: '/root/src/main.js',
258261
unstable_enablePackageExports: true,
@@ -305,6 +308,31 @@ describe('with package exports resolution enabled', () => {
305308
`);
306309
});
307310

311+
test('should not use "exports" for internal relative imports within a package', () => {
312+
const context = {
313+
...baseContext,
314+
originModulePath: '/root/node_modules/test-pkg/lib/foo.js',
315+
};
316+
317+
expect(Resolver.resolve(context, '../metadata.json', null)).toEqual({
318+
type: 'sourceFile',
319+
filePath: '/root/node_modules/test-pkg/metadata.json',
320+
});
321+
});
322+
323+
test('should not use "exports" for an absolute import path', () => {
324+
expect(
325+
Resolver.resolve(
326+
baseContext,
327+
'/root/node_modules/test-pkg/metadata.json',
328+
null,
329+
),
330+
).toEqual({
331+
type: 'sourceFile',
332+
filePath: '/root/node_modules/test-pkg/metadata.json',
333+
});
334+
});
335+
308336
describe('should resolve "exports" target directly', () => {
309337
test('without expanding `sourceExts`', () => {
310338
expect(Resolver.resolve(baseContext, 'test-pkg/foo.js', null)).toEqual({
@@ -374,13 +402,15 @@ describe('with package exports resolution enabled', () => {
374402
name: 'test-pkg',
375403
main: 'index.js',
376404
exports: {
405+
'.': './src/index.js',
377406
'./features/*.js': './src/features/*.js',
378407
'./features/bar/*.js': {
379408
'react-native': null,
380409
},
381410
'./assets/*': './assets/*',
382411
},
383412
}),
413+
'/root/node_modules/test-pkg/src/index.js': '',
384414
'/root/node_modules/test-pkg/src/features/foo.js': '',
385415
'/root/node_modules/test-pkg/src/features/foo.js.js': '',
386416
'/root/node_modules/test-pkg/src/features/bar/Bar.js': '',

packages/metro-resolver/src/resolve.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function resolve(
5050
}
5151

5252
if (isRelativeImport(moduleName) || path.isAbsolute(moduleName)) {
53-
const result = resolvePackage(context, moduleName, platform);
53+
const result = resolveModulePath(context, moduleName, platform);
5454
if (result.type === 'failed') {
5555
throw new FailedToResolvePathError(result.candidates);
5656
}
@@ -78,7 +78,7 @@ function resolve(
7878
originModulePath.indexOf(path.sep, fromModuleParentIdx),
7979
);
8080
const absPath = path.join(originModuleDir, realModuleName);
81-
const result = resolvePackage(context, absPath, platform);
81+
const result = resolveModulePath(context, absPath, platform);
8282
if (result.type === 'failed') {
8383
throw new FailedToResolvePathError(result.candidates);
8484
}
@@ -240,10 +240,11 @@ class MissingFileInHastePackageError extends Error {
240240
}
241241

242242
/**
243-
* In the NodeJS-style module resolution scheme we want to check potential
244-
* paths both as directories and as files. For example, `/foo/bar` may resolve
245-
* to `/foo/bar.js` (preferred), but it might also be `/foo/bar/index.js`, or
246-
* even a package directory.
243+
* Resolve a package entry point or subpath target.
244+
*
245+
* This should be used when resolving a bare import specifier prefixed with the
246+
* package name. Use `resolveModulePath` instead to scope to legacy "browser"
247+
* spec behaviour, which is also applicable to relative and absolute imports.
247248
*/
248249
function resolvePackage(
249250
context: ResolutionContext,

0 commit comments

Comments
 (0)