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

fix(metro-resolver): package exports restricted paths #1239

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion packages/metro-resolver/src/PackageExportsResolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,16 @@ function reduceConditionalExport(
let reducedValue = subpathValue;

while (reducedValue != null && typeof reducedValue !== 'string') {
let match: typeof subpathValue | 'no-match' = 'no-match';
let match: typeof subpathValue | 'no-match';

// when conditions are present and default is not specified
// the default condition is implicitly set to null, to allow
// for restricting access to unexported internals of a package.
if ('default' in reducedValue) {
match = 'no-match';
} else {
match = null;
}

for (const conditionName in reducedValue) {
if (conditionNames.has(conditionName)) {
Expand Down
12 changes: 7 additions & 5 deletions packages/metro-resolver/src/__tests__/package-exports-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -539,10 +539,6 @@ describe('with package exports resolution enabled', () => {
'test-pkg/features/foo.js.js',
'/root/node_modules/test-pkg/src/features/foo.js.js',
],
[
'test-pkg/features/bar/Bar.js',
'/root/node_modules/test-pkg/src/features/bar/Bar.js',
],
]) {
expect(Resolver.resolve(baseContext, importSpecifier, null)).toEqual({
type: 'sourceFile',
Expand All @@ -558,7 +554,13 @@ describe('with package exports resolution enabled', () => {
).toThrowError();
});

test('should use most specific pattern base', () => {
test('should use the most specific pattern base - implicit default condition', () => {
expect(() =>
Resolver.resolve(baseContext, 'test-pkg/features/bar/Bar.js', null),
).toThrowError();
});

test('should use most specific pattern base - custom condition', () => {
const context = {
...baseContext,
unstable_conditionNames: ['react-native'],
Expand Down