From 94c5a54d5f309a51d319b5933f22da7296d032d6 Mon Sep 17 00:00:00 2001 From: Lorenz Gardner Date: Tue, 23 Dec 2014 12:01:33 -0600 Subject: [PATCH] Add child_process.fork support. --- lib/bundle.js | 30 +++++++++++++++++++++--------- lib/exe.js | 26 +++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/lib/bundle.js b/lib/bundle.js index ba9da5a..64f3a3b 100644 --- a/lib/bundle.js +++ b/lib/bundle.js @@ -77,11 +77,8 @@ function bundle (input, complete) { function bundleScript (deps, next) { var buffer = [loader.toString()]; - - var dbuffer = []; - for (var i = deps.length; i--;) { var dep = deps[i]; @@ -95,8 +92,7 @@ function bundle (input, complete) { dbuffer.push(JSON.stringify(dep.id) + ": ["+req+", "+!!dep.entry+", "+JSON.stringify(dep.deps)+"]") } - - buffer = ["(" + loader.toString() + ").call(null, {"+dbuffer.join(",")+"})"].toString(); + buffer = ["(" + loader.toString() + ").call(null, {"+dbuffer.join(",")+"}," + JSON.stringify(deps[deps.length-1].id) + ")"].toString(); next(null, buffer); } @@ -104,14 +100,29 @@ function bundle (input, complete) { } -var loader = function (deps) { - +var loader = function (deps, key) { + var pathModule = global.require("path"); var darr = []; var dir = global.require("path").dirname(process.execPath); - for (var key in deps) { - darr.push(deps[key]); + var argv3 = process.argv[3]; + if (argv3) { + argv3 = pathModule.relative(process.execPath, argv3); + argv3 = pathModule.resolve(pathModule.dirname(key), argv3); } + if (process.argv[2] == "--child_process" && deps[argv3]) { + //we've been forked and should run the module specified by argv3[3] instead of the start up module + key = argv3; + process.argv = process.argv.splice(2, 2); + } + else if (process.argv[2] == "--child_process") { + //fork called, but for a module not bundled. + argv3 = process.argv[3]; + process.argv = process.argv.splice(2, 2); + global.require(argv3); + return; + } + darr.push(deps[key]); //start up module. function initModule (dep) { if (dep.module) return dep.module.exports; @@ -126,6 +137,7 @@ var loader = function (deps) { return dep.module.exports; } + //run the startup module darr.forEach(initModule); return darr.filter(function (dep) { diff --git a/lib/exe.js b/lib/exe.js index 17c519b..36c6cfb 100644 --- a/lib/exe.js +++ b/lib/exe.js @@ -79,6 +79,13 @@ exports.compile = function (options, complete) { } }, + /** + * monkeypatch child_process.js so nexejs knows when it is a forked process + */ + function monkeyPatchChildProc(next) { + _monkeyPatchChildProcess(nodeCompiler, next); + }, + /** * compile the node application */ @@ -302,12 +309,29 @@ function _monkeyPatchMainJs (compiler, complete) { return ~content.indexOf("nexe"); }, function (content, next) { - next(null, content.replace(/\(function\(process\) \{/,'(function(process) {\n process._eval = \'require("nexe");\';\n process.argv.unshift("node");\n')) + next(null, content.replace(/\(function\(process\) \{/,'(function(process) {\n process._eval = \'require("nexe");\';\n process.argv.splice(1, 0, "nexe.js");\n')) }, complete ); } +/** + */ +function _monkeyPatchChildProcess(compiler, complete) { + var childProcPath = path.join(compiler.dir, "lib", "child_process.js"); + + _monkeypatch( + childProcPath, + function (content) { + return ~content.indexOf("--child_process"); + }, + function (content, next) { + next(null, content.replace(/return spawn\(/, 'args.unshift("--child_process");\n return spawn(')); + }, + complete + ); +} + /** */