1
- import { createRequire } from 'node:module' ;
1
+ import { fileURLToPath } from 'node:url' ;
2
+ import { createRequire , Module } from 'node:module' ;
3
+ import { existsSync } from 'node:fs' ;
4
+ import { addPath as addNodeModulePath } from 'app-module-path' ;
5
+ import { dirname , resolve as pathResolve , join as pathJoin } from 'node:path' ;
6
+
7
+ let initialized = false ;
8
+
9
+ const thisDir = pathResolve (
10
+ import . meta. url
11
+ ? dirname ( fileURLToPath ( import . meta. url ) )
12
+ : typeof __dirname !== 'undefined'
13
+ ? __dirname
14
+ : process . cwd ( ) ,
15
+ ) ;
2
16
3
17
export const dynamicRequire = {
4
- resolve ( path : string , meta : ImportMeta ) : string {
5
- if ( meta . url === undefined && typeof require !== undefined ) {
6
- // import.meta not available, maybe running a ts file with tsx? use default cjs require
7
- return require . resolve ( path ) ;
18
+ initialize ( ) {
19
+ if ( initialized ) {
20
+ return ;
21
+ }
22
+ initialized = true ;
23
+
24
+ // Register node_modules paths
25
+
26
+ let currentDir = thisDir ;
27
+ while ( currentDir ) {
28
+ if ( currentDir . endsWith ( 'node_modules' ) ) {
29
+ tryAddNodeModulePath ( currentDir ) ;
30
+ break ;
31
+ }
32
+ tryAddNodeModulePath ( pathJoin ( currentDir , 'node_modules' ) ) ;
33
+ let parentDir = dirname ( currentDir ) ;
34
+ if ( parentDir === currentDir ) {
35
+ break ;
36
+ }
37
+ currentDir = parentDir ;
38
+ }
39
+
40
+ // Add typescript support by loading tsx
41
+ try {
42
+ dynamicRequire . require ( 'tsx/cjs' , import . meta) ;
43
+ } catch {
44
+ // ignore error if tsx could not be loaded
45
+ }
46
+
47
+ // Register .cjs and .cts extensions
48
+
49
+ const exts = ( Module as any ) . _extensions ;
50
+ if ( exts && ! ( '.cjs' in exts ) ) {
51
+ exts [ '.cjs' ] = exts [ '.js' ] ;
52
+ }
53
+ if ( exts && ! ( '.cts' in exts ) && '.ts' in exts ) {
54
+ exts [ '.cts' ] = exts [ '.ts' ] ;
8
55
}
9
- return createRequire ( meta . url ) . resolve ( path ) ;
56
+ } ,
57
+
58
+ resolve ( path : string , meta : ImportMeta ) : string {
59
+ dynamicRequire . initialize ( ) ;
60
+ return createRequire ( meta . url || pathResolve ( thisDir , 'index.js' ) ) . resolve ( path ) ;
10
61
} ,
11
62
12
63
require < T = unknown > ( path : string , meta : ImportMeta ) : T {
13
- if ( meta . url === undefined && typeof require !== undefined ) {
14
- // import.meta not available, maybe running a ts file with tsx? use default cjs require
15
- return require ( path ) ;
16
- }
17
- return createRequire ( meta . url ) ( path ) ;
64
+ dynamicRequire . initialize ( ) ;
65
+ return createRequire ( meta . url || pathResolve ( thisDir , 'index.js' ) ) ( path ) ;
18
66
} ,
19
67
20
68
/** Like require, but supports modules with a default export transpiled to cjs */
@@ -32,3 +80,13 @@ export const dynamicRequire = {
32
80
return required ;
33
81
} ,
34
82
} ;
83
+
84
+ function tryAddNodeModulePath ( nodeModulesPath : string ) {
85
+ try {
86
+ if ( existsSync ( nodeModulesPath ) ) {
87
+ addNodeModulePath ( nodeModulesPath ) ;
88
+ }
89
+ } catch {
90
+ // ignore error
91
+ }
92
+ }
0 commit comments