1'use strict'; 2const { 3 prepareMainThreadExecution, 4 markBootstrapComplete, 5} = require('internal/process/pre_execution'); 6const { getSingleExecutableCode } = internalBinding('sea'); 7const { emitExperimentalWarning } = require('internal/util'); 8const { Module, wrapSafe } = require('internal/modules/cjs/loader'); 9const { codes: { ERR_UNKNOWN_BUILTIN_MODULE } } = require('internal/errors'); 10 11prepareMainThreadExecution(false, true); 12markBootstrapComplete(); 13 14emitExperimentalWarning('Single executable application'); 15 16// This is roughly the same as: 17// 18// const mod = new Module(filename); 19// mod._compile(contents, filename); 20// 21// but the code has been duplicated because currently there is no way to set the 22// value of require.main to module. 23// 24// TODO(RaisinTen): Find a way to deduplicate this. 25 26const filename = process.execPath; 27const contents = getSingleExecutableCode(); 28const compiledWrapper = wrapSafe(filename, contents); 29 30const customModule = new Module(filename, null); 31customModule.filename = filename; 32customModule.paths = Module._nodeModulePaths(customModule.path); 33 34const customExports = customModule.exports; 35 36function customRequire(path) { 37 if (!Module.isBuiltin(path)) { 38 throw new ERR_UNKNOWN_BUILTIN_MODULE(path); 39 } 40 41 return require(path); 42} 43 44customRequire.main = customModule; 45 46const customFilename = customModule.filename; 47 48const customDirname = customModule.path; 49 50compiledWrapper( 51 customExports, 52 customRequire, 53 customModule, 54 customFilename, 55 customDirname); 56