Skip to content
Merged
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
6 changes: 6 additions & 0 deletions packages/metro-resolver/src/PackageExportsResolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ function normalizeExportsField(
return new Map([['.', exportsField]]);
}

if (typeof exportsField !== 'object') {
throw createConfigError(
'The "exports" field must be a string, an array or an object.',
);
}

const cachedValue = _normalizedExportsFields.get(exportsField);
if (cachedValue) {
return cachedValue;
Expand Down
32 changes: 32 additions & 0 deletions packages/metro-resolver/src/__tests__/package-exports-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,38 @@ describe('with package exports resolution enabled', () => {
);
});

test.each([
['false', false],
['true', true],
['a number', 1],
])(
'[nonstrict] should fall back to "main" field resolution when "exports" is %s',
(_, exportsField) => {
const logWarning = jest.fn();
const context = {
...baseContext,
...createPackageAccessors({
[p('/root/node_modules/test-pkg/package.json')]: {
main: 'index-main.js',
exports: exportsField,
},
}),
unstable_logWarning: logWarning,
};

expect(Resolver.resolve(context, 'test-pkg', null)).toEqual({
type: 'sourceFile',
filePath: p('/root/node_modules/test-pkg/index-main.js'),
});
expect(logWarning).toHaveBeenCalledTimes(1);
expect(logWarning.mock.calls[0][0]).toBe(
`The package ${p('/root/node_modules/test-pkg')} contains an invalid package.json configuration. Consider raising this issue with the package maintainer(s).
` +
'Reason: The "exports" field must be a string, an array or an object. Falling back to file-based resolution.',
);
},
);

describe('should resolve "exports" target directly', () => {
const context = {
...baseContext,
Expand Down
Loading