11'use strict' ;
22
3- const path = require ( 'path' ) ;
4- const { getURLFromFilePath, URL } = require ( 'internal/url' ) ;
53const errors = require ( 'internal/errors' ) ;
6-
74const ModuleMap = require ( 'internal/loader/ModuleMap' ) ;
85const ModuleJob = require ( 'internal/loader/ModuleJob' ) ;
96const defaultResolve = require ( 'internal/loader/DefaultResolve' ) ;
107const createDynamicModule = require ( 'internal/loader/CreateDynamicModule' ) ;
118const translators = require ( 'internal/loader/Translators' ) ;
12- const { setImportModuleDynamicallyCallback } = internalBinding ( 'module_wrap' ) ;
9+
1310const FunctionBind = Function . call . bind ( Function . prototype . bind ) ;
1411
1512const debug = require ( 'util' ) . debuglog ( 'esm' ) ;
1613
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-
3814/* A Loader instance is used as the main entry point for loading ES modules.
3915 * Currently, this is a singleton -- there is only one used for loading
4016 * the main module and everything in its dependency graph. */
4117class 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 ( ) {
4919 // methods which translate input code or other information
5020 // into es modules
5121 this . translators = translators ;
@@ -71,8 +41,9 @@ class Loader {
7141 this . _dynamicInstantiate = undefined ;
7242 }
7343
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' )
7647 throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'parentURL' , 'string' ) ;
7748
7849 const { url, format } =
@@ -93,7 +64,7 @@ class Loader {
9364 return { url, format } ;
9465 }
9566
96- async import ( specifier , parent = this . base ) {
67+ async import ( specifier , parent ) {
9768 const job = await this . getModuleJob ( specifier , parent ) ;
9869 const module = await job . run ( ) ;
9970 return module . namespace ( ) ;
@@ -107,7 +78,7 @@ class Loader {
10778 this . _dynamicInstantiate = FunctionBind ( dynamicInstantiate , null ) ;
10879 }
10980
110- async getModuleJob ( specifier , parentURL = this . base ) {
81+ async getModuleJob ( specifier , parentURL ) {
11182 const { url, format } = await this . resolve ( specifier , parentURL ) ;
11283 let job = this . moduleMap . get ( url ) ;
11384 if ( job !== undefined )
@@ -134,24 +105,16 @@ class Loader {
134105 }
135106
136107 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 ;
143111 }
144112 job = new ModuleJob ( this , url , loaderInstance , inspectBrk ) ;
145113 this . moduleMap . set ( url , job ) ;
146114 return job ;
147115 }
148-
149- static registerImportDynamicallyCallback ( loader ) {
150- setImportModuleDynamicallyCallback ( async ( referrer , specifier ) => {
151- return loader . import ( specifier , normalizeReferrerURL ( referrer ) ) ;
152- } ) ;
153- }
154116}
155117
156118Object . setPrototypeOf ( Loader . prototype , null ) ;
119+
157120module . exports = Loader ;
0 commit comments