Merge pull request #92 from LorenzGardner/child.fork
Add child_process.fork support.
This commit is contained in:
@@ -84,6 +84,10 @@ There are potential use cases for __dirname where the executable path is not the
|
||||
|
||||
Note: __filename will be 'undefined'
|
||||
|
||||
### child_process.fork
|
||||
|
||||
child_process.spawn works is unmodified, but child_process.fork will make an attempt to lunch a new instance of your executable and run the bundled module.
|
||||
|
||||
## Installation
|
||||
|
||||
Via NPM:
|
||||
|
||||
+21
-9
@@ -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) {
|
||||
|
||||
+25
-1
@@ -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
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
|
||||
|
||||
Reference in New Issue
Block a user