@@ -36,6 +36,39 @@ const { defaultGetFormat } = require('../dist-raw/node-esm-default-get-format');
3636// from node, build our implementation of the *new* API on top of it, and implement the *old*
3737// hooks API as a shim to the *new* API.
3838
39+ export interface NodeHooksAPI1 {
40+ resolve (
41+ specifier : string ,
42+ context : { parentURL : string } ,
43+ defaultResolve : NodeHooksAPI1 [ 'resolve' ]
44+ ) : Promise < { url : string } > ;
45+ getFormat (
46+ url : string ,
47+ context : { } ,
48+ defaultGetFormat : NodeHooksAPI1 [ 'getFormat' ]
49+ ) : Promise < { format : Format } > ;
50+ transformSource (
51+ source : string | Buffer ,
52+ context : { url : string ; format : Format } ,
53+ defaultTransformSource : NodeHooksAPI1 [ 'transformSource' ]
54+ ) : Promise < { source : string | Buffer } > ;
55+ }
56+
57+ export interface NodeHooksAPI2 {
58+ resolve (
59+ specifier : string ,
60+ context : { parentURL : string } ,
61+ defaultResolve : NodeHooksAPI2 [ 'resolve' ]
62+ ) : Promise < { url : string } > ;
63+ load (
64+ url : string ,
65+ context : { format : Format | null | undefined } ,
66+ defaultLoad : NodeHooksAPI2 [ 'load' ]
67+ ) : Promise < { format : Format ; source : string | Buffer | undefined } > ;
68+ }
69+
70+ type Format = 'builtin' | 'commonjs' | 'dynamic' | 'json' | 'module' | 'wasm' ;
71+
3972/** @internal */
4073export function registerAndCreateEsmHooks ( opts ?: RegisterOptions ) {
4174 // Automatically performs registration just like `-r ts-node/register`
@@ -62,12 +95,7 @@ export function createEsmHooks(tsNodeService: Service) {
6295 versionGteLt ( process . versions . node , '12.999.999' , '13.0.0' ) ;
6396
6497 // Explicit return type to avoid TS's non-ideal inferred type
65- const hooksAPI : {
66- resolve : typeof resolve ;
67- getFormat : typeof getFormat | undefined ;
68- transformSource : typeof transformSource | undefined ;
69- load : typeof load | undefined ;
70- } = newHooksAPI
98+ const hooksAPI : NodeHooksAPI1 | NodeHooksAPI2 = newHooksAPI
7199 ? { resolve, load, getFormat : undefined , transformSource : undefined }
72100 : { resolve, getFormat, transformSource, load : undefined } ;
73101 return hooksAPI ;
@@ -160,7 +188,6 @@ export function createEsmHooks(tsNodeService: Service) {
160188 return { format, source } ;
161189 }
162190
163- type Format = 'builtin' | 'commonjs' | 'dynamic' | 'json' | 'module' | 'wasm' ;
164191 async function getFormat (
165192 url : string ,
166193 context : { } ,
0 commit comments