@@ -15,20 +15,16 @@ const {
1515const {
1616 isSea,
1717 isVfsEnabled : isVfsEnabledFlag ,
18+ mainCodePath : seaMainCodePath ,
1819 isExperimentalSeaWarningNeeded,
1920} = internalBinding ( 'sea' ) ;
20- const isLoadingSea = isSea ( ) ;
2121const { emitExperimentalWarning } = require ( 'internal/util' ) ;
2222const { emitWarningSync } = require ( 'internal/process/warning' ) ;
23- const { Module } = require ( 'internal/modules/cjs/loader' ) ;
23+ const { Module, wrapModuleLoad } = require ( 'internal/modules/cjs/loader' ) ;
2424const { compileFunctionForCJSLoader } = internalBinding ( 'contextify' ) ;
2525const { maybeCacheSourceMap } = require ( 'internal/source_map/source_map_cache' ) ;
26- const { BuiltinModule } = require ( 'internal/bootstrap/realm' ) ;
27- const { normalizeRequirableId } = BuiltinModule ;
28- const { codes : {
29- ERR_UNKNOWN_BUILTIN_MODULE ,
30- } } = require ( 'internal/errors' ) ;
3126const { pathToFileURL } = require ( 'internal/url' ) ;
27+ const { loadBuiltinModuleForEmbedder } = require ( 'internal/modules/helpers' ) ;
3228const { compileSourceTextModule, SourceTextModuleTypes : { kEmbedder } } = require ( 'internal/modules/esm/utils' ) ;
3329const { moduleFormats } = internalBinding ( 'modules' ) ;
3430const assert = require ( 'internal/assert' ) ;
@@ -38,7 +34,7 @@ const path = require('path');
3834// command line (e.g. it could be provided via an API or bundled into the executable).
3935prepareMainThreadExecution ( false , true ) ;
4036
41- const isBuiltinWarningNeeded = isLoadingSea && isExperimentalSeaWarningNeeded ( ) ;
37+ const isLoadingSea = isSea ( ) ;
4238if ( isExperimentalSeaWarningNeeded ( ) ) {
4339 emitExperimentalWarning ( 'Single executable application' ) ;
4440}
@@ -89,24 +85,9 @@ function embedderRunCjs(content, filename) {
8985 }
9086
9187 // Patch the module to make it look almost like a regular CJS module
92- // instance. When VFS is active, set the filename to a VFS path so that
93- // relative require('./foo.js') resolves under the VFS mount point,
94- // and use createRequire for a fully-featured require function.
95- let requireFn ;
96- if ( seaVfsActive && seaVfsMountPoint ) {
97- // VFS paths always use POSIX separators regardless of platform.
98- customModule . filename = seaVfsMountPoint + '/' + path . basename ( filename ) ;
99- customModule . paths = Module . _nodeModulePaths (
100- path . dirname ( customModule . filename ) ) ;
101- // Use createRequire so that require has all standard properties
102- // (resolve, cache, etc.) and builtin loading flows through hooks.
103- requireFn = Module . createRequire ( customModule . filename ) ;
104- requireFn . main = customModule ;
105- } else {
106- customModule . filename = process . execPath ;
107- customModule . paths = Module . _nodeModulePaths ( process . execPath ) ;
108- requireFn = embedderRequire ;
109- }
88+ // instance.
89+ customModule . filename = process . execPath ;
90+ customModule . paths = Module . _nodeModulePaths ( process . execPath ) ;
11091 embedderRequire . main = customModule ;
11192
11293 // This currently returns what the wrapper returns i.e. if the code
@@ -116,27 +97,13 @@ function embedderRunCjs(content, filename) {
11697 // out parameter.
11798 return compiledWrapper (
11899 customModule . exports , // exports
119- requireFn , // require
100+ embedderRequire , // require
120101 customModule , // module
121- customModule . filename , // __filename
122- path . dirname ( customModule . filename ) , // __dirname
102+ filename , // __filename
103+ customModule . path , // __dirname
123104 ) ;
124105}
125106
126- let warnedAboutBuiltins = false ;
127- function warnNonBuiltinInSEA ( ) {
128- if ( isBuiltinWarningNeeded && ! warnedAboutBuiltins ) {
129- emitWarningSync (
130- 'Currently the require() provided to the main script embedded into ' +
131- 'single-executable applications only supports loading built-in modules.\n' +
132- 'To load a module from disk after the single executable application is ' +
133- 'launched, use require("module").createRequire().\n' +
134- 'Support for bundled module loading or virtual file systems are under ' +
135- 'discussions in https://github.com/nodejs/single-executable' ) ;
136- warnedAboutBuiltins = true ;
137- }
138- }
139-
140107// Lazy-loaded SEA VFS support
141108let seaVfsInitialized = false ;
142109let seaVfsActive = false ;
@@ -148,7 +115,6 @@ function initSeaVfs() {
148115
149116 if ( ! isLoadingSea || ! isVfsEnabledFlag ) return ;
150117
151- // Check if SEA has VFS support
152118 const { getSeaVfs } = require ( 'internal/vfs/sea' ) ;
153119 const seaVfs = getSeaVfs ( ) ;
154120 if ( seaVfs ) {
@@ -158,15 +124,7 @@ function initSeaVfs() {
158124}
159125
160126function embedderRequire ( id ) {
161- const normalizedId = normalizeRequirableId ( id ) ;
162- if ( normalizedId ) {
163- return require ( normalizedId ) ;
164- }
165-
166- // When VFS is not active, only built-in modules are supported.
167- // VFS-enabled SEAs use createRequire instead of embedderRequire.
168- warnNonBuiltinInSEA ( ) ;
169- throw new ERR_UNKNOWN_BUILTIN_MODULE ( id ) ;
127+ return loadBuiltinModuleForEmbedder ( id ) . exports ;
170128}
171129
172130function embedderRunESM ( content , filename ) {
@@ -195,6 +153,12 @@ function embedderRunEntryPoint(content, format, filename) {
195153 initSeaVfs ( ) ;
196154 }
197155
156+ if ( seaVfsActive && format === moduleFormats . kCommonJS ) {
157+ const mainName = seaMainCodePath ? path . basename ( seaMainCodePath ) : path . basename ( filename ) ;
158+ const vfsMain = path . posix . join ( seaVfsMountPoint , mainName ) ;
159+ return wrapModuleLoad ( vfsMain , null , true ) ;
160+ }
161+
198162 if ( format === moduleFormats . kCommonJS ) {
199163 return embedderRunCjs ( content , filename ) ;
200164 } else if ( format === moduleFormats . kModule ) {
0 commit comments