@@ -54,20 +54,27 @@ node --import tsx/esm ./file.ts
54
54
node --loader tsx/esm ./file.ts
55
55
```
56
56
57
- ### Hooks API
58
- > Previously known as _ Loaders_ ([ renamed in Node.js v21] ( https://github.com/nodejs/loaders/issues/95 ) )
57
+ ### Registration & Unregistration
58
+ ``` js
59
+ import { register } from ' tsx/esm/api'
59
60
60
- You can use the [ Hooks API] ( https://nodejs.org/api/module.html#customization-hooks ) to load TypeScript files with ` tsx/esm ` :
61
+ // register tsx enhancement
62
+ const unregister = register ()
61
63
62
- ``` js
63
- import { register } from ' node:module'
64
+ // Unregister when needed
65
+ unregister ()
66
+ ```
64
67
65
- register (' tsx/esm' , {
66
- parentURL: import .meta.url,
67
- data: true
68
- })
68
+ #### Tracking loaded files
69
+ Detect files that get loaded with the ` onImport ` hook:
69
70
70
- const loaded = await import (' ./hello.ts' )
71
+ ``` ts
72
+ register ({
73
+ onImport : (file : string ) => {
74
+ console .log (file )
75
+ // file:///foo.ts
76
+ }
77
+ })
71
78
```
72
79
73
80
## Only CommonJS enhancement
@@ -82,21 +89,15 @@ Pass _tsx_ into the `--require` flag:
82
89
node --require tsx/cjs ./file.ts
83
90
```
84
91
85
- ### Node.js API
86
-
87
- #### Globally patching ` require `
88
-
89
- ##### Enabling TSX Enhancement
90
-
91
- Add the following line at the top of your entry file:
92
+ This is the equivalent of adding the following at the top of your entry file, which you can also do:
92
93
93
94
``` js
94
95
require (' tsx/cjs' )
95
96
```
96
97
97
- ##### Manual Registration & Unregistration
98
+ ### Registration & Unregistration
98
99
99
- To manually register and unregister the TypeScript enhancement:
100
+ To manually register and unregister the tsx enhancement:
100
101
101
102
``` js
102
103
const tsx = require (' tsx/cjs/api' )
@@ -108,13 +109,60 @@ const unregister = tsx.register()
108
109
unregister ()
109
110
```
110
111
111
- ## ` tsx.require() `
112
+ ## Enhanced ` import() ` & ` require() `
113
+
114
+ tsx exports enhanced ` import() ` or ` require() ` functions, allowing you to load TypeScript/ESM files without affecting the runtime environment.
115
+
116
+ ### ` tsImport() `
117
+
118
+ The ` import() ` function enhanced to support TypeScript. Because it's the native ` import() ` , it supports [ top-level await] ( https://v8.dev/features/top-level-await ) .
119
+
120
+ ::: warning Caveat
121
+ ` require() ` calls in the loaded files are not enhanced.
122
+ :::
123
+
124
+ #### ESM usage
125
+
126
+ Note, the current file path must be passed in as the second argument to resolve the import context.
127
+
128
+ ``` js
129
+ import { tsImport } from ' tsx/esm/api'
130
+
131
+ const loaded = await tsImport (' ./file.ts' , import .meta.url)
132
+ ` ` `
133
+
134
+ #### CommonJS usage
135
+
136
+ ` ` ` js
137
+ const { tsImport } = require (' tsx/esm/api' )
138
+
139
+ const loaded = await tsImport (' ./file.ts' , __filename )
140
+ ` ` `
141
+
142
+ #### Tracking loaded files
143
+ Detect files that get loaded with the ` onImport` hook:
112
144
113
- For loading a TypeScript file without affecting the environment, ` tsx ` exports a custom ` require(id, loadFromPath) ` function.
145
+ ` ` ` ts
146
+ tsImport (' ./file.ts' , {
147
+ parentURL: import .meta.url,
148
+ onImport : (file : string ) => {
149
+ console .log (file)
150
+ // file:///foo.ts
151
+ }
152
+ })
153
+ ` ` `
154
+
155
+ ### ` tsx .require ()`
156
+
157
+ The ` require ()` function enhanced to support TypeScript and ESM.
158
+
159
+ ::: warning Caveat
160
+ ` import ()` & asynchronous ` require ()` calls in the loaded files are not enhanced.
161
+ :::
114
162
115
- Note, the current file path must be passed in as the second argument so it knows how to resolve relative paths.
163
+ #### CommonJS usage
116
164
117
- ### CommonJS usage
165
+ Note, the current file path must be passed in as the second argument to resolve the import context.
118
166
119
167
` ` ` js
120
168
const tsx = require (' tsx/cjs/api' )
@@ -123,7 +171,7 @@ const loaded = tsx.require('./file.ts', __filename)
123
171
const filepath = tsx .require .resolve (' ./file.ts' , __filename )
124
172
` ` `
125
173
126
- ### ESM usage
174
+ #### ESM usage
127
175
128
176
` ` ` js
129
177
import { require } from ' tsx/cjs/api'
@@ -132,7 +180,7 @@ const loaded = require('./file.ts', import.meta.url)
132
180
const filepath = require .resolve (' ./file.ts' , import .meta.url)
133
181
` ` `
134
182
135
- ### Module graph inspection
183
+ #### Tracking loaded files
136
184
137
185
Because the CommonJS API tracks loaded modules in ` require .cache ` , you can use it to identify loaded files for dependency tracking. This can be useful when implementing a watcher.
138
186
0 commit comments