@@ -36,6 +36,53 @@ const { defaultGetFormat } = require('../dist-raw/node-esm-default-get-format');
36
36
// from node, build our implementation of the *new* API on top of it, and implement the *old*
37
37
// hooks API as a shim to the *new* API.
38
38
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
+
39
86
/** @internal */
40
87
export function registerAndCreateEsmHooks ( opts ?: RegisterOptions ) {
41
88
// Automatically performs registration just like `-r ts-node/register`
@@ -62,12 +109,7 @@ export function createEsmHooks(tsNodeService: Service) {
62
109
versionGteLt ( process . versions . node , '12.999.999' , '13.0.0' ) ;
63
110
64
111
// 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
71
113
? { resolve, load, getFormat : undefined , transformSource : undefined }
72
114
: { resolve, getFormat, transformSource, load : undefined } ;
73
115
return hooksAPI ;
@@ -117,9 +159,12 @@ export function createEsmHooks(tsNodeService: Service) {
117
159
// `load` from new loader hook API (See description at the top of this file)
118
160
async function load (
119
161
url : string ,
120
- context : { format : Format | null | undefined } ,
162
+ context : { format : NodeLoaderHooksFormat | null | undefined } ,
121
163
defaultLoad : typeof load
122
- ) : Promise < { format : Format ; source : string | Buffer | undefined } > {
164
+ ) : Promise < {
165
+ format : NodeLoaderHooksFormat ;
166
+ source : string | Buffer | undefined ;
167
+ } > {
123
168
// If we get a format hint from resolve() on the context then use it
124
169
// otherwise call the old getFormat() hook using node's old built-in defaultGetFormat() that ships with ts-node
125
170
const format =
@@ -160,12 +205,11 @@ export function createEsmHooks(tsNodeService: Service) {
160
205
return { format, source } ;
161
206
}
162
207
163
- type Format = 'builtin' | 'commonjs' | 'dynamic' | 'json' | 'module' | 'wasm' ;
164
208
async function getFormat (
165
209
url : string ,
166
210
context : { } ,
167
211
defaultGetFormat : typeof getFormat
168
- ) : Promise < { format : Format } > {
212
+ ) : Promise < { format : NodeLoaderHooksFormat } > {
169
213
const defer = ( overrideUrl : string = url ) =>
170
214
defaultGetFormat ( overrideUrl , context , defaultGetFormat ) ;
171
215
@@ -185,7 +229,7 @@ export function createEsmHooks(tsNodeService: Service) {
185
229
186
230
// If file has .ts, .tsx, or .jsx extension, then ask node how it would treat this file if it were .js
187
231
const ext = extname ( nativePath ) ;
188
- let nodeSays : { format : Format } ;
232
+ let nodeSays : { format : NodeLoaderHooksFormat } ;
189
233
if ( ext !== '.js' && ! tsNodeService . ignored ( nativePath ) ) {
190
234
nodeSays = await defer ( formatUrl ( pathToFileURL ( nativePath + '.js' ) ) ) ;
191
235
} else {
@@ -210,7 +254,7 @@ export function createEsmHooks(tsNodeService: Service) {
210
254
211
255
async function transformSource (
212
256
source : string | Buffer ,
213
- context : { url : string ; format : Format } ,
257
+ context : { url : string ; format : NodeLoaderHooksFormat } ,
214
258
defaultTransformSource : typeof transformSource
215
259
) : Promise < { source : string | Buffer } > {
216
260
if ( source === null || source === undefined ) {
0 commit comments