1
1
'use strict' ;
2
2
3
- const path = require ( 'path' ) ;
4
- const { getURLFromFilePath, URL } = require ( 'internal/url' ) ;
5
3
const errors = require ( 'internal/errors' ) ;
6
-
7
4
const ModuleMap = require ( 'internal/loader/ModuleMap' ) ;
8
5
const ModuleJob = require ( 'internal/loader/ModuleJob' ) ;
9
6
const defaultResolve = require ( 'internal/loader/DefaultResolve' ) ;
10
7
const createDynamicModule = require ( 'internal/loader/CreateDynamicModule' ) ;
11
8
const translators = require ( 'internal/loader/Translators' ) ;
12
- const { setImportModuleDynamicallyCallback } = internalBinding ( 'module_wrap' ) ;
9
+
13
10
const FunctionBind = Function . call . bind ( Function . prototype . bind ) ;
14
11
15
12
const debug = require ( 'util' ) . debuglog ( 'esm' ) ;
16
13
17
- // Returns a file URL for the current working directory.
18
- function getURLStringForCwd ( ) {
19
- try {
20
- return getURLFromFilePath ( `${ process . cwd ( ) } /` ) . href ;
21
- } catch ( e ) {
22
- e . stack ;
23
- // If the current working directory no longer exists.
24
- if ( e . code === 'ENOENT' ) {
25
- return undefined ;
26
- }
27
- throw e ;
28
- }
29
- }
30
-
31
- function normalizeReferrerURL ( referrer ) {
32
- if ( typeof referrer === 'string' && path . isAbsolute ( referrer ) ) {
33
- return getURLFromFilePath ( referrer ) . href ;
34
- }
35
- return new URL ( referrer ) . href ;
36
- }
37
-
38
14
/* A Loader instance is used as the main entry point for loading ES modules.
39
15
* Currently, this is a singleton -- there is only one used for loading
40
16
* the main module and everything in its dependency graph. */
41
17
class Loader {
42
- constructor ( base = getURLStringForCwd ( ) ) {
43
- if ( typeof base !== 'string' )
44
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'base' , 'string' ) ;
45
-
46
- this . base = base ;
47
- this . isMain = true ;
48
-
18
+ constructor ( ) {
49
19
// methods which translate input code or other information
50
20
// into es modules
51
21
this . translators = translators ;
@@ -71,8 +41,9 @@ class Loader {
71
41
this . _dynamicInstantiate = undefined ;
72
42
}
73
43
74
- async resolve ( specifier , parentURL = this . base ) {
75
- if ( typeof parentURL !== 'string' )
44
+ async resolve ( specifier , parentURL ) {
45
+ const isMain = parentURL === undefined ;
46
+ if ( ! isMain && typeof parentURL !== 'string' )
76
47
throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'parentURL' , 'string' ) ;
77
48
78
49
const { url, format } =
@@ -93,7 +64,7 @@ class Loader {
93
64
return { url, format } ;
94
65
}
95
66
96
- async import ( specifier , parent = this . base ) {
67
+ async import ( specifier , parent ) {
97
68
const job = await this . getModuleJob ( specifier , parent ) ;
98
69
const module = await job . run ( ) ;
99
70
return module . namespace ( ) ;
@@ -107,7 +78,7 @@ class Loader {
107
78
this . _dynamicInstantiate = FunctionBind ( dynamicInstantiate , null ) ;
108
79
}
109
80
110
- async getModuleJob ( specifier , parentURL = this . base ) {
81
+ async getModuleJob ( specifier , parentURL ) {
111
82
const { url, format } = await this . resolve ( specifier , parentURL ) ;
112
83
let job = this . moduleMap . get ( url ) ;
113
84
if ( job !== undefined )
@@ -134,24 +105,16 @@ class Loader {
134
105
}
135
106
136
107
let inspectBrk = false ;
137
- if ( this . isMain ) {
138
- if ( process . _breakFirstLine ) {
139
- delete process . _breakFirstLine ;
140
- inspectBrk = true ;
141
- }
142
- this . isMain = false ;
108
+ if ( process . _breakFirstLine ) {
109
+ delete process . _breakFirstLine ;
110
+ inspectBrk = true ;
143
111
}
144
112
job = new ModuleJob ( this , url , loaderInstance , inspectBrk ) ;
145
113
this . moduleMap . set ( url , job ) ;
146
114
return job ;
147
115
}
148
-
149
- static registerImportDynamicallyCallback ( loader ) {
150
- setImportModuleDynamicallyCallback ( async ( referrer , specifier ) => {
151
- return loader . import ( specifier , normalizeReferrerURL ( referrer ) ) ;
152
- } ) ;
153
- }
154
116
}
155
117
156
118
Object . setPrototypeOf ( Loader . prototype , null ) ;
119
+
157
120
module . exports = Loader ;
0 commit comments