Skip to content

Forward whole context to ESM hooks #1572

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions src/esm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,38 @@ export interface NodeLoaderHooksAPI2 {
export namespace NodeLoaderHooksAPI2 {
export type ResolveHook = (
specifier: string,
context: { parentURL: string },
context: ResolveContext,
defaultResolve: ResolveHook
) => Promise<{ url: string }>;
export type LoadHook = (
url: string,
context: { format: NodeLoaderHooksFormat | null | undefined },
context: LoadContext,
defaultLoad: NodeLoaderHooksAPI2['load']
) => Promise<{
format: NodeLoaderHooksFormat;
source: string | Buffer | undefined;
}>;
}

// Context passed to the `load()` Hook.
//
// https://nodejs.org/dist/latest-v17.x/docs/api/esm.html#loadurl-context-defaultload
interface LoadContext {
format: NodeLoaderHooksFormat | null | undefined
// This field is only present on Node v17
importAssertions?: object
}

// Context passed to the `resolve()` hook.
//
// https://nodejs.org/dist/latest-v17.x/docs/api/esm.html#resolvespecifier-context-defaultresolve
interface ResolveContext {
conditions: string[],
parentURL: string | undefined,
// This field is only present on Node v17
importAssertions?: object
}

export type NodeLoaderHooksFormat =
| 'builtin'
| 'commonjs'
Expand Down Expand Up @@ -122,7 +141,7 @@ export function createEsmHooks(tsNodeService: Service) {

async function resolve(
specifier: string,
context: { parentURL: string },
context: ResolveContext,
defaultResolve: typeof resolve
): Promise<{ url: string }> {
const defer = async () => {
Expand Down Expand Up @@ -159,7 +178,7 @@ export function createEsmHooks(tsNodeService: Service) {
// `load` from new loader hook API (See description at the top of this file)
async function load(
url: string,
context: { format: NodeLoaderHooksFormat | null | undefined },
context: LoadContext,
defaultLoad: typeof load
): Promise<{
format: NodeLoaderHooksFormat;
Expand All @@ -176,7 +195,7 @@ export function createEsmHooks(tsNodeService: Service) {
// Call the new defaultLoad() to get the source
const { source: rawSource } = await defaultLoad(
url,
{ format },
{ ...context, format },
defaultLoad
);

Expand Down