metro icon indicating copy to clipboard operation
metro copied to clipboard

Enable Virtual Module Resolution

Open oktaysenkan opened this issue 1 year ago • 1 comments

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;
}>;

oktaysenkan avatar Dec 29 '24 22:12 oktaysenkan

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

Downchuck avatar Apr 02 '25 02:04 Downchuck