Merge pull request #1 from crcn/master

update
This commit is contained in:
LorenzGardner
2014-12-30 12:07:37 -06:00
3 changed files with 50 additions and 10 deletions
+4
View File
@@ -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
View File
@@ -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
View File
@@ -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
);
}
/**
*/