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,8 @@ 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+ if ( parentURL !== undefined && typeof parentURL !== 'string' )
7646 throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'parentURL' , 'string' ) ;
7747
7848 const { url, format } =
@@ -93,7 +63,7 @@ class Loader {
9363 return { url, format } ;
9464 }
9565
96- async import ( specifier , parent = this . base ) {
66+ async import ( specifier , parent ) {
9767 const job = await this . getModuleJob ( specifier , parent ) ;
9868 const module = await job . run ( ) ;
9969 return module . namespace ( ) ;
@@ -107,7 +77,7 @@ class Loader {
10777 this . _dynamicInstantiate = FunctionBind ( dynamicInstantiate , null ) ;
10878 }
10979
110- async getModuleJob ( specifier , parentURL = this . base ) {
80+ async getModuleJob ( specifier , parentURL ) {
11181 const { url, format } = await this . resolve ( specifier , parentURL ) ;
11282 let job = this . moduleMap . get ( url ) ;
11383 if ( job !== undefined )
@@ -134,24 +104,16 @@ class Loader {
134104 }
135105
136106 let inspectBrk = false ;
137- if ( this . isMain ) {
138- if ( process . _breakFirstLine ) {
139- delete process . _breakFirstLine ;
140- inspectBrk = true ;
141- }
142- this . isMain = false ;
107+ if ( process . _breakFirstLine ) {
108+ delete process . _breakFirstLine ;
109+ inspectBrk = true ;
143110 }
144111 job = new ModuleJob ( this , url , loaderInstance , inspectBrk ) ;
145112 this . moduleMap . set ( url , job ) ;
146113 return job ;
147114 }
148-
149- static registerImportDynamicallyCallback ( loader ) {
150- setImportModuleDynamicallyCallback ( async ( referrer , specifier ) => {
151- return loader . import ( specifier , normalizeReferrerURL ( referrer ) ) ;
152- } ) ;
153- }
154115}
155116
156117Object . setPrototypeOf ( Loader . prototype , null ) ;
118+
157119module . exports = Loader ;
0 commit comments