@@ -15,16 +15,12 @@ const {
1515const { isExperimentalSeaWarningNeeded, isSea } = internalBinding ( 'sea' ) ;
1616const { emitExperimentalWarning } = require ( 'internal/util' ) ;
1717const { emitWarningSync } = require ( 'internal/process/warning' ) ;
18- const { BuiltinModule } = require ( 'internal/bootstrap/realm' ) ;
19- const { normalizeRequirableId } = BuiltinModule ;
2018const { Module } = require ( 'internal/modules/cjs/loader' ) ;
2119const { compileFunctionForCJSLoader } = internalBinding ( 'contextify' ) ;
2220const { maybeCacheSourceMap } = require ( 'internal/source_map/source_map_cache' ) ;
23- const { codes : {
24- ERR_UNKNOWN_BUILTIN_MODULE ,
25- } } = require ( 'internal/errors' ) ;
2621const { pathToFileURL } = require ( 'internal/url' ) ;
27- const { loadBuiltinModule } = require ( 'internal/modules/helpers' ) ;
22+ const { loadBuiltinModuleForEmbedder } = require ( 'internal/modules/helpers' ) ;
23+ const { compileSourceTextModuleForEmbedder } = require ( 'internal/modules/esm/utils' ) ;
2824const { moduleFormats } = internalBinding ( 'modules' ) ;
2925const assert = require ( 'internal/assert' ) ;
3026const path = require ( 'path' ) ;
@@ -34,7 +30,6 @@ const path = require('path');
3430prepareMainThreadExecution ( false , true ) ;
3531
3632const isLoadingSea = isSea ( ) ;
37- const isBuiltinWarningNeeded = isLoadingSea && isExperimentalSeaWarningNeeded ( ) ;
3833if ( isExperimentalSeaWarningNeeded ( ) ) {
3934 emitExperimentalWarning ( 'Single executable application' ) ;
4035}
@@ -103,28 +98,8 @@ function embedderRunCjs(content, filename) {
10398 ) ;
10499}
105100
106- let warnedAboutBuiltins = false ;
107- function warnNonBuiltinInSEA ( ) {
108- if ( isBuiltinWarningNeeded && ! warnedAboutBuiltins ) {
109- emitWarningSync (
110- 'Currently the require() provided to the main script embedded into ' +
111- 'single-executable applications only supports loading built-in modules.\n' +
112- 'To load a module from disk after the single executable application is ' +
113- 'launched, use require("module").createRequire().\n' +
114- 'Support for bundled module loading or virtual file systems are under ' +
115- 'discussions in https://github.com/nodejs/single-executable' ) ;
116- warnedAboutBuiltins = true ;
117- }
118- }
119-
120101function embedderRequire ( id ) {
121- const normalizedId = normalizeRequirableId ( id ) ;
122-
123- if ( ! normalizedId ) {
124- warnNonBuiltinInSEA ( ) ;
125- throw new ERR_UNKNOWN_BUILTIN_MODULE ( id ) ;
126- }
127- return require ( normalizedId ) ;
102+ return loadBuiltinModuleForEmbedder ( id ) . exports ;
128103}
129104
130105function embedderRunESM ( content , filename ) {
@@ -134,31 +109,10 @@ function embedderRunESM(content, filename) {
134109 } else {
135110 resourceName = filename ;
136111 }
137- const { compileSourceTextModule } = require ( 'internal/modules/esm/utils' ) ;
138- // TODO(joyeecheung): support code cache, dynamic import() and import.meta.
139- const wrap = compileSourceTextModule ( resourceName , content ) ;
140- // Cache the source map for the module if present.
141- if ( wrap . sourceMapURL ) {
142- maybeCacheSourceMap ( resourceName , content , wrap , false , undefined , wrap . sourceMapURL ) ;
143- }
144- const requests = wrap . getModuleRequests ( ) ;
145- const modules = [ ] ;
146- for ( let i = 0 ; i < requests . length ; ++ i ) {
147- const { specifier } = requests [ i ] ;
148- const normalizedId = normalizeRequirableId ( specifier ) ;
149- if ( ! normalizedId ) {
150- warnNonBuiltinInSEA ( ) ;
151- throw new ERR_UNKNOWN_BUILTIN_MODULE ( specifier ) ;
152- }
153- const mod = loadBuiltinModule ( normalizedId ) ;
154- if ( ! mod ) {
155- throw new ERR_UNKNOWN_BUILTIN_MODULE ( specifier ) ;
156- }
157- modules . push ( mod . getESMFacade ( ) ) ;
158- }
159- wrap . link ( modules ) ;
160- wrap . instantiate ( ) ;
161- wrap . evaluate ( - 1 , false ) ;
112+ // TODO(joyeecheung): allow configuration from node::ModuleData,
113+ // either via a more generic context object, or something like import.meta extensions.
114+ const context = { isMain : true , __proto__ : null } ;
115+ const wrap = compileSourceTextModuleForEmbedder ( resourceName , content , context ) ;
162116
163117 // TODO(joyeecheung): we may want to return the v8::Module via a vm.SourceTextModule
164118 // when vm.SourceTextModule stablizes, or put it in an out parameter.
0 commit comments