@@ -36,6 +36,53 @@ 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 NodeLoaderHooksAPI1 {
40+ resolve : NodeLoaderHooksAPI1 . ResolveHook ;
41+ getFormat : NodeLoaderHooksAPI1 . GetFormatHook ;
42+ transformSource : NodeLoaderHooksAPI1 . TransformSourceHook ;
43+ }
44+ export namespace NodeLoaderHooksAPI1 {
45+ export type ResolveHook = NodeLoaderHooksAPI2 . ResolveHook ;
46+ export type GetFormatHook = (
47+ url : string ,
48+ context : { } ,
49+ defaultGetFormat : GetFormatHook
50+ ) => Promise < { format : NodeLoaderHooksFormat } > ;
51+ export type TransformSourceHook = (
52+ source : string | Buffer ,
53+ context : { url : string ; format : NodeLoaderHooksFormat } ,
54+ defaultTransformSource : NodeLoaderHooksAPI1 . TransformSourceHook
55+ ) => Promise < { source : string | Buffer } > ;
56+ }
57+
58+ export interface NodeLoaderHooksAPI2 {
59+ resolve : NodeLoaderHooksAPI2 . ResolveHook ;
60+ load : NodeLoaderHooksAPI2 . LoadHook ;
61+ }
62+ export namespace NodeLoaderHooksAPI2 {
63+ export type ResolveHook = (
64+ specifier : string ,
65+ context : { parentURL : string } ,
66+ defaultResolve : ResolveHook
67+ ) => Promise < { url : string } > ;
68+ export type LoadHook = (
69+ url : string ,
70+ context : { format : NodeLoaderHooksFormat | null | undefined } ,
71+ defaultLoad : NodeLoaderHooksAPI2 [ 'load' ]
72+ ) => Promise < {
73+ format : NodeLoaderHooksFormat ;
74+ source : string | Buffer | undefined ;
75+ } > ;
76+ }
77+
78+ export type NodeLoaderHooksFormat =
79+ | 'builtin'
80+ | 'commonjs'
81+ | 'dynamic'
82+ | 'json'
83+ | 'module'
84+ | 'wasm' ;
85+
3986/** @internal */
4087export function registerAndCreateEsmHooks ( opts ?: RegisterOptions ) {
4188 // Automatically performs registration just like `-r ts-node/register`
@@ -62,12 +109,7 @@ export function createEsmHooks(tsNodeService: Service) {
62109 versionGteLt ( process . versions . node , '12.999.999' , '13.0.0' ) ;
63110
64111 // 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
112+ const hooksAPI : NodeLoaderHooksAPI1 | NodeLoaderHooksAPI2 = newHooksAPI
71113 ? { resolve, load, getFormat : undefined , transformSource : undefined }
72114 : { resolve, getFormat, transformSource, load : undefined } ;
73115 return hooksAPI ;
@@ -117,9 +159,12 @@ export function createEsmHooks(tsNodeService: Service) {
117159 // `load` from new loader hook API (See description at the top of this file)
118160 async function load (
119161 url : string ,
120- context : { format : Format | null | undefined } ,
162+ context : { format : NodeLoaderHooksFormat | null | undefined } ,
121163 defaultLoad : typeof load
122- ) : Promise < { format : Format ; source : string | Buffer | undefined } > {
164+ ) : Promise < {
165+ format : NodeLoaderHooksFormat ;
166+ source : string | Buffer | undefined ;
167+ } > {
123168 // If we get a format hint from resolve() on the context then use it
124169 // otherwise call the old getFormat() hook using node's old built-in defaultGetFormat() that ships with ts-node
125170 const format =
@@ -160,12 +205,11 @@ export function createEsmHooks(tsNodeService: Service) {
160205 return { format, source } ;
161206 }
162207
163- type Format = 'builtin' | 'commonjs' | 'dynamic' | 'json' | 'module' | 'wasm' ;
164208 async function getFormat (
165209 url : string ,
166210 context : { } ,
167211 defaultGetFormat : typeof getFormat
168- ) : Promise < { format : Format } > {
212+ ) : Promise < { format : NodeLoaderHooksFormat } > {
169213 const defer = ( overrideUrl : string = url ) =>
170214 defaultGetFormat ( overrideUrl , context , defaultGetFormat ) ;
171215
@@ -185,7 +229,7 @@ export function createEsmHooks(tsNodeService: Service) {
185229
186230 // If file has .ts, .tsx, or .jsx extension, then ask node how it would treat this file if it were .js
187231 const ext = extname ( nativePath ) ;
188- let nodeSays : { format : Format } ;
232+ let nodeSays : { format : NodeLoaderHooksFormat } ;
189233 if ( ext !== '.js' && ! tsNodeService . ignored ( nativePath ) ) {
190234 nodeSays = await defer ( formatUrl ( pathToFileURL ( nativePath + '.js' ) ) ) ;
191235 } else {
@@ -210,7 +254,7 @@ export function createEsmHooks(tsNodeService: Service) {
210254
211255 async function transformSource (
212256 source : string | Buffer ,
213- context : { url : string ; format : Format } ,
257+ context : { url : string ; format : NodeLoaderHooksFormat } ,
214258 defaultTransformSource : typeof transformSource
215259 ) : Promise < { source : string | Buffer } > {
216260 if ( source === null || source === undefined ) {
0 commit comments