mirror of
https://github.com/nodejs/node.git
synced 2025-04-29 14:25:18 +00:00

Compile a JavaScript file into a single executable application: ```console $ echo 'console.log(`Hello, ${process.argv[2]}!`);' > hello.js $ cp $(command -v node) hello $ npx postject hello NODE_JS_CODE hello.js \ --sentinel-fuse NODE_JS_FUSE_fce680ab2cc467b6e072b8b5df1996b2 $ npx postject hello NODE_JS_CODE hello.js \ --sentinel-fuse NODE_JS_FUSE_fce680ab2cc467b6e072b8b5df1996b2 \ --macho-segment-name NODE_JS $ ./hello world Hello, world! ``` Signed-off-by: Darshan Sen <raisinten@gmail.com> PR-URL: https://github.com/nodejs/node/pull/45038 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
56 lines
1.5 KiB
JavaScript
56 lines
1.5 KiB
JavaScript
'use strict';
|
|
const {
|
|
prepareMainThreadExecution,
|
|
markBootstrapComplete,
|
|
} = require('internal/process/pre_execution');
|
|
const { getSingleExecutableCode } = internalBinding('sea');
|
|
const { emitExperimentalWarning } = require('internal/util');
|
|
const { Module, wrapSafe } = require('internal/modules/cjs/loader');
|
|
const { codes: { ERR_UNKNOWN_BUILTIN_MODULE } } = require('internal/errors');
|
|
|
|
prepareMainThreadExecution(false, true);
|
|
markBootstrapComplete();
|
|
|
|
emitExperimentalWarning('Single executable application');
|
|
|
|
// This is roughly the same as:
|
|
//
|
|
// const mod = new Module(filename);
|
|
// mod._compile(contents, filename);
|
|
//
|
|
// but the code has been duplicated because currently there is no way to set the
|
|
// value of require.main to module.
|
|
//
|
|
// TODO(RaisinTen): Find a way to deduplicate this.
|
|
|
|
const filename = process.execPath;
|
|
const contents = getSingleExecutableCode();
|
|
const compiledWrapper = wrapSafe(filename, contents);
|
|
|
|
const customModule = new Module(filename, null);
|
|
customModule.filename = filename;
|
|
customModule.paths = Module._nodeModulePaths(customModule.path);
|
|
|
|
const customExports = customModule.exports;
|
|
|
|
function customRequire(path) {
|
|
if (!Module.isBuiltin(path)) {
|
|
throw new ERR_UNKNOWN_BUILTIN_MODULE(path);
|
|
}
|
|
|
|
return require(path);
|
|
}
|
|
|
|
customRequire.main = customModule;
|
|
|
|
const customFilename = customModule.filename;
|
|
|
|
const customDirname = customModule.path;
|
|
|
|
compiledWrapper(
|
|
customExports,
|
|
customRequire,
|
|
customModule,
|
|
customFilename,
|
|
customDirname);
|