Skip to content

Commit adfb593

Browse files
huntiefacebook-github-bot
authored andcommitted
Add getPackageForModule function to resolution context
Summary: Exposes an additional accessor on this API in order for `package.json`-based resolution to be relocated into metro-resolver. Changelog: **[Feature]** Add `getPackageForModule` accessor to custom resolver context Reviewed By: jacdebug Differential Revision: D42740236 fbshipit-source-id: 5753b427cd50417a657a59bcd06224aebd7b3fb9
1 parent 11a4681 commit adfb593

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

docs/Resolution.md

+4
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ The ordered list of fields in `package.json` that should be read to resolve a pa
168168

169169
Given the path to a `package.json` file, returns the parsed file contents.
170170

171+
#### `getPackageForModule: (modulePath: string) => ?PackageInfo`
172+
173+
Given a module path that may exist under an npm package, locates and returns the package root path and parsed `package.json` contents.
174+
171175
#### `resolveHasteModule: string => ?string`
172176

173177
Resolves a Haste module name to an absolute path. Returns `null` if no such module exists.

packages/metro-resolver/src/__tests__/utils.js

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export function createResolutionContext(
3333
extraNodeModules: null,
3434
getPackage: (packageJsonPath: string) =>
3535
JSON.parse(fileMap[packageJsonPath]),
36+
getPackageForModule: () => null,
3637
isAssetFile: () => false,
3738
mainFields: ['browser', 'main'],
3839
nodeModulesPaths: [],

packages/metro-resolver/src/types.js

+11
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ export type PackageJson = $ReadOnly<{
5050
...
5151
}>;
5252

53+
export type PackageInfo = $ReadOnly<{
54+
packageJson: PackageJson,
55+
rootPath: string,
56+
}>;
57+
5358
/**
5459
* Check existence of a single file.
5560
*/
@@ -91,6 +96,12 @@ export type FileOrDirContext = $ReadOnly<{
9196
* Get the parsed contents of the specified `package.json` file.
9297
*/
9398
getPackage: (packageJsonPath: string) => ?PackageJson,
99+
100+
/**
101+
* Get the package information and parsed `package.json` file for for a given
102+
* module path, if it is contained within an npm package.
103+
*/
104+
getPackageForModule: (modulePath: string) => ?PackageInfo,
94105
}>;
95106

96107
export type HasteContext = $ReadOnly<{

packages/metro/src/node-haste/DependencyGraph/ModuleResolution.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import type {
2020
ResolveAsset,
2121
} from 'metro-resolver';
2222
import type {ResolverInputOptions} from '../../shared/types.flow';
23-
import type {PackageJson} from 'metro-resolver/src/types';
23+
import type {PackageInfo, PackageJson} from 'metro-resolver/src/types';
2424

2525
const {codeFrameColumns} = require('@babel/code-frame');
2626
const fs = require('fs');
@@ -220,6 +220,7 @@ class ModuleResolver<TPackage: Packageish> {
220220
resolveHastePackage: (name: string) =>
221221
this._options.getHastePackagePath(name, platform),
222222
getPackage: this._getPackage,
223+
getPackageForModule: this._getPackageForModule,
223224
},
224225
moduleName,
225226
platform,
@@ -282,6 +283,24 @@ class ModuleResolver<TPackage: Packageish> {
282283
return null;
283284
};
284285

286+
_getPackageForModule = (modulePath: string): ?PackageInfo => {
287+
let pkg;
288+
289+
try {
290+
pkg = this._options.moduleCache.getPackageOf(modulePath);
291+
} catch (e) {
292+
// Do nothing. The standard module cache does not trigger any error, but
293+
// the ModuleGraph one does, if the module does not exist.
294+
}
295+
296+
return pkg != null
297+
? {
298+
rootPath: path.dirname(pkg.path),
299+
packageJson: pkg.read(),
300+
}
301+
: null;
302+
};
303+
285304
/**
286305
* TODO: Return Resolution instead of coercing to BundlerResolution here
287306
*/

0 commit comments

Comments
 (0)