1
1
import path from 'path' ;
2
- import { pathToFileURL , fileURLToPath } from 'url' ;
2
+ import { pathToFileURL } from 'url' ;
3
3
import type {
4
- ResolveFnOutput , ResolveHookContext , LoadHook , GlobalPreloadHook , InitializeHook ,
4
+ ResolveFnOutput , ResolveHookContext ,
5
5
} from 'module' ;
6
- import type { TransformOptions } from 'esbuild' ;
7
- import { transform } from '../utils/transform/index.js' ;
8
- import { transformDynamicImport } from '../utils/transform/transform-dynamic-import.js' ;
9
- import { resolveTsPath } from '../utils/resolve-ts-path.js' ;
10
- import { installSourceMapSupport } from '../source-map.js' ;
11
- import { isFeatureSupported , importAttributes } from '../utils/node-features.js' ;
12
- import { parent } from '../utils/ipc/client.js' ;
13
- import type { NodeError } from '../types.js' ;
14
- import { isRelativePathPattern } from '../utils/is-relative-path-pattern.js' ;
6
+ import { resolveTsPath } from '../../utils/resolve-ts-path.js' ;
7
+ import type { NodeError } from '../../types.js' ;
8
+ import { isRelativePathPattern } from '../../utils/is-relative-path-pattern.js' ;
15
9
import {
16
10
tsconfigPathsMatcher ,
17
- fileMatcher ,
18
11
tsExtensionsPattern ,
19
- isJsonPattern ,
20
12
getFormatFromFileUrl ,
21
13
fileProtocol ,
22
14
allowJs ,
23
15
type MaybePromise ,
24
16
} from './utils.js' ;
25
17
26
- const applySourceMap = installSourceMapSupport ( ) ;
27
-
28
18
const isDirectoryPattern = / \/ (?: $ | \? ) / ;
29
19
30
20
type NextResolve = (
@@ -39,21 +29,6 @@ type resolve = (
39
29
recursiveCall ?: boolean ,
40
30
) => MaybePromise < ResolveFnOutput > ;
41
31
42
- export const initialize : InitializeHook = async ( data ) => {
43
- if ( ! data ) {
44
- throw new Error ( 'tsx must be loaded with --import instead of --loader\nThe --loader flag was deprecated in Node v20.6.0 and v18.19.0' ) ;
45
- }
46
- } ;
47
-
48
- /**
49
- * Technically globalPreload is deprecated so it should be in loaders-deprecated
50
- * but it shares a closure with the new load hook
51
- */
52
- export const globalPreload : GlobalPreloadHook = ( ) => `
53
- const require = getBuiltin('module').createRequire("${ import . meta. url } ");
54
- require('../source-map.cjs').installSourceMapSupport();
55
- ` ;
56
-
57
32
const resolveExplicitPath = async (
58
33
defaultResolve : NextResolve ,
59
34
specifier : string ,
@@ -218,72 +193,3 @@ export const resolve: resolve = async (
218
193
throw error ;
219
194
}
220
195
} ;
221
-
222
- const contextAttributesProperty = (
223
- isFeatureSupported ( importAttributes )
224
- ? 'importAttributes'
225
- : 'importAssertions'
226
- ) ;
227
-
228
- export const load : LoadHook = async (
229
- url ,
230
- context ,
231
- defaultLoad ,
232
- ) => {
233
- /*
234
- Filter out node:*
235
- Maybe only handle files that start with file://
236
- */
237
- if ( parent . send ) {
238
- parent . send ( {
239
- type : 'dependency' ,
240
- path : url ,
241
- } ) ;
242
- }
243
-
244
- if ( isJsonPattern . test ( url ) ) {
245
- if ( ! context [ contextAttributesProperty ] ) {
246
- context [ contextAttributesProperty ] = { } ;
247
- }
248
-
249
- context [ contextAttributesProperty ] ! . type = 'json' ;
250
- }
251
-
252
- const loaded = await defaultLoad ( url , context ) ;
253
-
254
- // CommonJS and Internal modules (e.g. node:*)
255
- if ( ! loaded . source ) {
256
- return loaded ;
257
- }
258
-
259
- const filePath = url . startsWith ( 'file://' ) ? fileURLToPath ( url ) : url ;
260
- const code = loaded . source . toString ( ) ;
261
-
262
- if (
263
- // Support named imports in JSON modules
264
- loaded . format === 'json'
265
- || tsExtensionsPattern . test ( url )
266
- ) {
267
- const transformed = await transform (
268
- code ,
269
- filePath ,
270
- {
271
- tsconfigRaw : fileMatcher ?.( filePath ) as TransformOptions [ 'tsconfigRaw' ] ,
272
- } ,
273
- ) ;
274
-
275
- return {
276
- format : 'module' ,
277
- source : applySourceMap ( transformed ) ,
278
- } ;
279
- }
280
-
281
- if ( loaded . format === 'module' ) {
282
- const dynamicImportTransformed = transformDynamicImport ( filePath , code ) ;
283
- if ( dynamicImportTransformed ) {
284
- loaded . source = applySourceMap ( dynamicImportTransformed ) ;
285
- }
286
- }
287
-
288
- return loaded ;
289
- } ;
0 commit comments