Enable Virtual Module Resolution
Overview
To implement a custom resolution system that allows resolving virtual modules and handles cases where files have not been created yet, you can extend the Metro resolver with a custom resolveRequest function. This function will provide a VirtualResolution for modules that don’t exist physically but are dynamically generated or virtualized.
resolveRequest?: CustomResolver;
export type CustomResolver = (
context: CustomResolutionContext,
moduleName: string,
platform: string | null,
) => Resolution;
export type Resolution = FileResolution | VirtualResolution
| Readonly<{type: 'empty'}>;
export type FileResolution = AssetResolution | SourceFileResolution;
export type AssetResolution = Readonly<{
type: 'assetFiles';
filePaths: AssetFileResolution;
}>;
export type SourceFileResolution = Readonly<{
type: 'sourceFile';
filePath: string;
}>;
export type VirtualResolution = Readonly<{
type: 'virtual';
content: string;
}>;
On this note - if the Resolver is also a union with Promise<Resolution>, then the function can be async. This came up for me with going through an ESM resolution method that was async, and thus a dead end.
Shouldn't the resolution be a union of ArrayBuffer | ArrayView | Blob | String?
The context resolver in AWS Lambda has a similar method, but it supports async. Overall the virtual with yarn pnp is picked up -- and the resolver as is can use the sync resolver, but it would be nice if it supported async.
As I recall from the yarn pnp path - there is an Sha -1 hook somewhere down the line