Add child_process.fork support.
This commit is contained in:
+21
-9
@@ -77,11 +77,8 @@ function bundle (input, complete) {
|
|||||||
|
|
||||||
function bundleScript (deps, next) {
|
function bundleScript (deps, next) {
|
||||||
var buffer = [loader.toString()];
|
var buffer = [loader.toString()];
|
||||||
|
|
||||||
|
|
||||||
var dbuffer = [];
|
var dbuffer = [];
|
||||||
|
|
||||||
|
|
||||||
for (var i = deps.length; i--;) {
|
for (var i = deps.length; i--;) {
|
||||||
var dep = deps[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)+"]")
|
dbuffer.push(JSON.stringify(dep.id) + ": ["+req+", "+!!dep.entry+", "+JSON.stringify(dep.deps)+"]")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
buffer = ["(" + loader.toString() + ").call(null, {"+dbuffer.join(",")+"}," + JSON.stringify(deps[deps.length-1].id) + ")"].toString();
|
||||||
buffer = ["(" + loader.toString() + ").call(null, {"+dbuffer.join(",")+"})"].toString();
|
|
||||||
|
|
||||||
next(null, buffer);
|
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 darr = [];
|
||||||
var dir = global.require("path").dirname(process.execPath);
|
var dir = global.require("path").dirname(process.execPath);
|
||||||
|
|
||||||
for (var key in deps) {
|
var argv3 = process.argv[3];
|
||||||
darr.push(deps[key]);
|
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) {
|
function initModule (dep) {
|
||||||
if (dep.module) return dep.module.exports;
|
if (dep.module) return dep.module.exports;
|
||||||
@@ -126,6 +137,7 @@ var loader = function (deps) {
|
|||||||
return dep.module.exports;
|
return dep.module.exports;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//run the startup module
|
||||||
darr.forEach(initModule);
|
darr.forEach(initModule);
|
||||||
|
|
||||||
return darr.filter(function (dep) {
|
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
|
* compile the node application
|
||||||
*/
|
*/
|
||||||
@@ -302,7 +309,24 @@ function _monkeyPatchMainJs (compiler, complete) {
|
|||||||
return ~content.indexOf("nexe");
|
return ~content.indexOf("nexe");
|
||||||
},
|
},
|
||||||
function (content, next) {
|
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
|
complete
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user