Skip to content

Commit

Permalink
Fix docs for esm hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
cspotcode committed Oct 22, 2021
1 parent cd6d208 commit 4b957a1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
37 changes: 21 additions & 16 deletions src/esm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,34 +37,39 @@ const { defaultGetFormat } = require('../dist-raw/node-esm-default-get-format');
// hooks API as a shim to the *new* API.

export interface NodeLoaderHooksAPI1 {
resolve(
specifier: string,
context: { parentURL: string },
defaultResolve: NodeLoaderHooksAPI1['resolve']
): Promise<{ url: string }>;
getFormat(
resolve: NodeLoaderHooksAPI1.ResolveHook;
getFormat: NodeLoaderHooksAPI1.GetFormatHook;
transformSource: NodeLoaderHooksAPI1.TransformSourceHook;
}
export namespace NodeLoaderHooksAPI1 {
export type ResolveHook = NodeLoaderHooksAPI2.ResolveHook;
export type GetFormatHook = (
url: string,
context: {},
defaultGetFormat: NodeLoaderHooksAPI1['getFormat']
): Promise<{ format: NodeLoaderHooksFormat }>;
transformSource(
defaultGetFormat: GetFormatHook
) => Promise<{ format: NodeLoaderHooksFormat }>;
export type TransformSourceHook = (
source: string | Buffer,
context: { url: string; format: NodeLoaderHooksFormat },
defaultTransformSource: NodeLoaderHooksAPI1['transformSource']
): Promise<{ source: string | Buffer }>;
defaultTransformSource: NodeLoaderHooksAPI1.TransformSourceHook
) => Promise<{ source: string | Buffer }>;
}

export interface NodeLoaderHooksAPI2 {
resolve(
resolve: NodeLoaderHooksAPI2.ResolveHook;
load: NodeLoaderHooksAPI2.LoadHook;
}
export namespace NodeLoaderHooksAPI2 {
export type ResolveHook = (
specifier: string,
context: { parentURL: string },
defaultResolve: NodeLoaderHooksAPI2['resolve']
): Promise<{ url: string }>;
load(
defaultResolve: ResolveHook
) => Promise<{ url: string }>;
export type LoadHook = (
url: string,
context: { format: NodeLoaderHooksFormat | null | undefined },
defaultLoad: NodeLoaderHooksAPI2['load']
): Promise<{ format: NodeLoaderHooksFormat; source: string | Buffer | undefined }>;
) => Promise<{ format: NodeLoaderHooksFormat; source: string | Buffer | undefined }>;
}

export type NodeLoaderHooksFormat = 'builtin' | 'commonjs' | 'dynamic' | 'json' | 'module' | 'wasm';
Expand Down
12 changes: 11 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1488,6 +1488,16 @@ function getTokenAtPosition(
}
}

/**
* Create an implementation of node's ESM loader hooks.
*
* This may be useful if you
* want to wrap or compose the loader hooks to add additional functionality or
* combine with another loader.
*
* Node changed the hooks API, so there are two possible APIs. This function
* detects your node version and returns the appropriate API.
*/
export const createEsmHooks: typeof createEsmHooksFn = (
tsNodeService: Service
) => require('./esm').createEsmHooks(tsNodeService);
) => (require('./esm') as typeof import('./esm')).createEsmHooks(tsNodeService);

0 comments on commit 4b957a1

Please sign in to comment.