|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow strict-local |
| 8 | + * @format |
| 9 | + * @oncall react_native |
| 10 | + */ |
| 11 | + |
| 12 | +import type {PackageForModule, ResolutionContext} from '../types'; |
| 13 | + |
| 14 | +import * as Resolver from '../index'; |
| 15 | +import {createPackageAccessors, createResolutionContext} from './utils'; |
| 16 | + |
| 17 | +// The resolver looks up package scopes through `getPackageForModule`. These |
| 18 | +// tests pin down which paths it asks about for each kind of specifier, so |
| 19 | +// that changes to the number of lookups per resolution are deliberate. Metro |
| 20 | +// memoizes the answer per path, so a repeated path is cheap and a new one is |
| 21 | +// not: in particular, the directory of a file is looked up once for all of |
| 22 | +// its extension candidates. |
| 23 | +describe('package scope lookups', () => { |
| 24 | + const fileMap = { |
| 25 | + '/root/package.json': JSON.stringify({name: 'root'}), |
| 26 | + '/root/src/main.js': '', |
| 27 | + '/root/src/foo.js': '', |
| 28 | + '/root/src/lib/index.js': '', |
| 29 | + '/root/node_modules/pkg/package.json': JSON.stringify({ |
| 30 | + name: 'pkg', |
| 31 | + main: 'lib/index', |
| 32 | + browser: {'./lib/redirected.js': './lib/other.js'}, |
| 33 | + }), |
| 34 | + '/root/node_modules/pkg/lib/index.js': '', |
| 35 | + '/root/node_modules/pkg/lib/sub.js': '', |
| 36 | + '/root/node_modules/pkg/lib/other.js': '', |
| 37 | + '/root/node_modules/nopkg/index.js': '', |
| 38 | + }; |
| 39 | + |
| 40 | + let getPackageForModule: JestMockFn<[string], ?PackageForModule>; |
| 41 | + let context: ResolutionContext; |
| 42 | + |
| 43 | + function useFileMap(files: typeof fileMap) { |
| 44 | + const accessors = createPackageAccessors(files); |
| 45 | + getPackageForModule = jest.fn(accessors.getPackageForModule); |
| 46 | + context = { |
| 47 | + ...createResolutionContext(files), |
| 48 | + ...accessors, |
| 49 | + getPackageForModule, |
| 50 | + originModulePath: '/root/src/main.js', |
| 51 | + }; |
| 52 | + } |
| 53 | + |
| 54 | + beforeEach(() => { |
| 55 | + useFileMap(fileMap); |
| 56 | + }); |
| 57 | + |
| 58 | + const lookedUpPaths = () => |
| 59 | + getPackageForModule.mock.calls.map(([modulePath]) => modulePath); |
| 60 | + |
| 61 | + test('relative file, found after trying several extensions', () => { |
| 62 | + expect(Resolver.resolve(context, './foo', 'ios')).toEqual({ |
| 63 | + type: 'sourceFile', |
| 64 | + filePath: '/root/src/foo.js', |
| 65 | + }); |
| 66 | + expect(lookedUpPaths()).toEqual(['/root/src/foo', '/root/src']); |
| 67 | + }); |
| 68 | + |
| 69 | + test('relative file with an explicit extension', () => { |
| 70 | + expect(Resolver.resolve(context, './foo.js', 'ios')).toEqual({ |
| 71 | + type: 'sourceFile', |
| 72 | + filePath: '/root/src/foo.js', |
| 73 | + }); |
| 74 | + expect(lookedUpPaths()).toEqual(['/root/src/foo.js', '/root/src']); |
| 75 | + }); |
| 76 | + |
| 77 | + test('relative directory, resolved to its index', () => { |
| 78 | + expect(Resolver.resolve(context, './lib', 'ios')).toEqual({ |
| 79 | + type: 'sourceFile', |
| 80 | + filePath: '/root/src/lib/index.js', |
| 81 | + }); |
| 82 | + expect(lookedUpPaths()).toEqual([ |
| 83 | + '/root/src/lib', |
| 84 | + '/root/src', |
| 85 | + '/root/src/lib', |
| 86 | + ]); |
| 87 | + }); |
| 88 | + |
| 89 | + test('package entry point', () => { |
| 90 | + expect(Resolver.resolve(context, 'pkg', 'ios')).toEqual({ |
| 91 | + type: 'sourceFile', |
| 92 | + filePath: '/root/node_modules/pkg/lib/index.js', |
| 93 | + }); |
| 94 | + // The origin, the package directory, and the directory of the entry |
| 95 | + // point. Files directly under node_modules have no scope, so none is |
| 96 | + // looked up for the `pkg` file candidates. |
| 97 | + expect(lookedUpPaths()).toEqual([ |
| 98 | + '/root/src/main.js', |
| 99 | + '/root/node_modules/pkg', |
| 100 | + '/root/node_modules/pkg', |
| 101 | + '/root/node_modules/pkg/lib', |
| 102 | + ]); |
| 103 | + }); |
| 104 | + |
| 105 | + test('package subpath', () => { |
| 106 | + expect(Resolver.resolve(context, 'pkg/lib/sub', 'ios')).toEqual({ |
| 107 | + type: 'sourceFile', |
| 108 | + filePath: '/root/node_modules/pkg/lib/sub.js', |
| 109 | + }); |
| 110 | + expect(lookedUpPaths()).toEqual([ |
| 111 | + '/root/src/main.js', |
| 112 | + '/root/node_modules/pkg/lib/sub', |
| 113 | + '/root/node_modules/pkg/lib/sub', |
| 114 | + '/root/node_modules/pkg/lib', |
| 115 | + ]); |
| 116 | + }); |
| 117 | + |
| 118 | + test('package subpath redirected by the "browser" field', () => { |
| 119 | + expect(Resolver.resolve(context, 'pkg/lib/redirected', 'ios')).toEqual({ |
| 120 | + type: 'sourceFile', |
| 121 | + filePath: '/root/node_modules/pkg/lib/other.js', |
| 122 | + }); |
| 123 | + // The redirected path may be redirected again by its own scope |
| 124 | + expect(lookedUpPaths()).toEqual([ |
| 125 | + '/root/src/main.js', |
| 126 | + '/root/node_modules/pkg/lib/redirected', |
| 127 | + '/root/node_modules/pkg/lib/other.js', |
| 128 | + '/root/node_modules/pkg/lib', |
| 129 | + ]); |
| 130 | + }); |
| 131 | + |
| 132 | + test('module under node_modules without a package.json', () => { |
| 133 | + expect(Resolver.resolve(context, 'nopkg', 'ios')).toEqual({ |
| 134 | + type: 'sourceFile', |
| 135 | + filePath: '/root/node_modules/nopkg/index.js', |
| 136 | + }); |
| 137 | + expect(lookedUpPaths()).toEqual([ |
| 138 | + '/root/src/main.js', |
| 139 | + '/root/node_modules/nopkg', |
| 140 | + '/root/node_modules/nopkg', |
| 141 | + '/root/node_modules/nopkg', |
| 142 | + ]); |
| 143 | + }); |
| 144 | + |
| 145 | + test('package subpath with package exports', () => { |
| 146 | + useFileMap({ |
| 147 | + ...fileMap, |
| 148 | + '/root/node_modules/pkg/package.json': JSON.stringify({ |
| 149 | + name: 'pkg', |
| 150 | + exports: {'./sub': './lib/sub.js'}, |
| 151 | + }), |
| 152 | + }); |
| 153 | + context = {...context, unstable_enablePackageExports: true}; |
| 154 | + expect(Resolver.resolve(context, 'pkg/sub', 'ios')).toEqual({ |
| 155 | + type: 'sourceFile', |
| 156 | + filePath: '/root/node_modules/pkg/lib/sub.js', |
| 157 | + }); |
| 158 | + expect(lookedUpPaths()).toEqual([ |
| 159 | + '/root/src/main.js', |
| 160 | + '/root/node_modules/pkg/sub', |
| 161 | + '/root/node_modules/pkg/sub', |
| 162 | + ]); |
| 163 | + }); |
| 164 | +}); |
0 commit comments