From f458298ffbf555427401709b6fcab64c5766a72f Mon Sep 17 00:00:00 2001 From: LorenzIndus Date: Wed, 17 Dec 2014 08:48:01 -0600 Subject: [PATCH 1/6] update version of builtins in deps Get the fix they did to include the "constants" builtin module. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 06e1d9a..b770dfb 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "main": "./lib/index.js", "dependencies": { "async": "~0.2.10", - "builtins": "0.0.5", + "builtins": "0.0.7", "colors": "~0.6.2", "glob": "~3.2.9", "mkdirp": "~0.3.5", From 99a8cbe9b31d099e6f951b0d561fedd4e4d0c777 Mon Sep 17 00:00:00 2001 From: Lorenz Gardner Date: Wed, 17 Dec 2014 13:09:00 -0600 Subject: [PATCH 2/6] Add JSON support for require statments. --- lib/bundle.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/bundle.js b/lib/bundle.js index 5213e3e..c6813ae 100644 --- a/lib/bundle.js +++ b/lib/bundle.js @@ -79,14 +79,19 @@ function bundle (input, complete) { var buffer = [loader.toString()]; - var dbuffer = []; + var dbuffer = []; for (var i = deps.length; i--;) { var dep = deps[i]; - var req = "function (require, module, exports) { \n" + dep.source + "}" + var req = "function (require, module, exports) { \n" + if (dep.id.toLowerCase().indexOf(".json", dep.id.length - 5) !== -1) { + //this is the result of require("someFile.json"), so we need to export it from the function + req += "module.exports = " + } + req += dep.source + "}"; dbuffer.push(JSON.stringify(dep.id) + ": ["+req+", "+!!dep.entry+", "+JSON.stringify(dep.deps)+"]") From ec79ff85d498ac51910738ea83f2d8ab31cc0c6c Mon Sep 17 00:00:00 2001 From: Lorenz Gardner Date: Wed, 17 Dec 2014 18:52:04 -0600 Subject: [PATCH 3/6] add __dirname support --- lib/bundle.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/bundle.js b/lib/bundle.js index 5213e3e..36d544c 100644 --- a/lib/bundle.js +++ b/lib/bundle.js @@ -86,7 +86,7 @@ function bundle (input, complete) { var dep = deps[i]; - var req = "function (require, module, exports) { \n" + dep.source + "}" + var req = "function (require, module, exports, __dirname) { \n" + dep.source + "}" dbuffer.push(JSON.stringify(dep.id) + ": ["+req+", "+!!dep.entry+", "+JSON.stringify(dep.deps)+"]") @@ -104,6 +104,7 @@ function bundle (input, complete) { var loader = function (deps) { var darr = []; + var cwd = process.cwd(); for (var key in deps) { darr.push(deps[key]); @@ -117,7 +118,7 @@ var loader = function (deps) { var rdep = deps[dep[2][path]]; var exports = rdep ? rdep.module ? rdep.module.exports : initModule(rdep) : global.require(path); return exports; - }, dep.module, dep.module.exports); + }, dep.module, dep.module.exports, cwd); return dep.module.exports; } From 7c1331bed8679d59a31da280f93e62bde8423dc4 Mon Sep 17 00:00:00 2001 From: LorenzGardner Date: Thu, 18 Dec 2014 17:46:55 -0600 Subject: [PATCH 4/6] Update README.md update the readme to explain how to workaround the limitations (not supporting dynamic require statements and lack of native module support the pseudo support of __dirname) --- README.md | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f1fda7f..ca13477 100644 --- a/README.md +++ b/README.md @@ -20,9 +20,69 @@ Nexe is a command-line utility that compiles your Node.js application into a sin - Linux / Mac / BSD / Windows - Windows: Python 2.6 or 2.7 (in PATH), Visual Studio 2010 or 2012 -## Caveats +##Caveats -- Doesn't support native modules (yet). +### Doesn't support native modules + +- Use the techniques below for working around dynamic require statments to exclude the module from the bundling, and deploy along side the executable in a node_module folder so your app can find it. + +###Doesn't support dynamic require statments +Such As: +``` +var x = require(someVar); +``` + +In this case nexe won't bundle the file + +``` + var x; + if (someCheck) { + x = require("./ver1.js"); + } + else { + x = require("./var2.js"); + } +``` + +In this case nexe will bundle both files. + +Workarounds: +1) for dyanmic requires that you want bundled add the following into your project +``` + var dummyToForceIncludeForBundle = false; + if (dummyToForceIncludeForBundle) { + require("./loadedDynamicallyLater.js"); + ... + } +``` +this will trick the bundler into including them. + +2) for dynamic files getting included that you don't want to be +``` + var moduleName = "./ver2.js"; + if (someCheck) { + moduleName = "./ver1.js"; + } + var x = require(moduleName) +``` +Note: neither file will be bundled. + +Using these two techniques you can change your application code so mdoules are not bundles, and generate a includes.js file as part of your build process so that the right files get bundled for your build configuration. + +### __dirname + +Once the module is budnled it is part of the executable. __dirname is therefore the CWD (current working dir) of the executable when run. Thus if you put resources on a realtive path from the cwd of the executable (in most cases the path to the executable) your app will be able to access them. + +If you had a data file at /dev/myNodeApp/stateManager/handler/data/some.csv +and a file at /dev/myNodeApp/stateManager/handler/loader.js +``` + module.exports = fw.readFileSync(path.join(__dirname, "./data/some.csv")); +``` +you would need to deploy some.csv in a sub dir data/ along side your executable + +There are potential use cases for __dirname where the CWD is not the correct substitution, and could result in a silent error (possibly even in a dependciey that you are unaware of). + +Note: __filename will be 'undefined' ## Installation From f7ad9a8e83db52a25c2e2dd2c74c08279a3afcd5 Mon Sep 17 00:00:00 2001 From: Lorenz Gardner Date: Thu, 18 Dec 2014 18:02:48 -0600 Subject: [PATCH 5/6] It actually makes way more sense for __driname to be exectuable dir rather than the cwd. --- README.md | 9 ++------- lib/bundle.js | 4 ++-- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index ca13477..68eeea3 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ Using these two techniques you can change your application code so mdoules are n ### __dirname -Once the module is budnled it is part of the executable. __dirname is therefore the CWD (current working dir) of the executable when run. Thus if you put resources on a realtive path from the cwd of the executable (in most cases the path to the executable) your app will be able to access them. +Once the module is budnled it is part of the executable. __dirname is therefore the executable dir (process.execPath). Thus if you put resources on a realtive path from the the executable your app will be able to access them. If you had a data file at /dev/myNodeApp/stateManager/handler/data/some.csv and a file at /dev/myNodeApp/stateManager/handler/loader.js @@ -80,7 +80,7 @@ and a file at /dev/myNodeApp/stateManager/handler/loader.js ``` you would need to deploy some.csv in a sub dir data/ along side your executable -There are potential use cases for __dirname where the CWD is not the correct substitution, and could result in a silent error (possibly even in a dependciey that you are unaware of). +There are potential use cases for __dirname where the executable path is not the correct substitution, and could result in a silent error (possibly even in a dependciey that you are unaware of). Note: __filename will be 'undefined' @@ -131,8 +131,3 @@ nexe.compile({ }); ```` - - - - - diff --git a/lib/bundle.js b/lib/bundle.js index 4539db8..ba9da5a 100644 --- a/lib/bundle.js +++ b/lib/bundle.js @@ -107,7 +107,7 @@ function bundle (input, complete) { var loader = function (deps) { var darr = []; - var cwd = process.cwd(); + var dir = global.require("path").dirname(process.execPath); for (var key in deps) { darr.push(deps[key]); @@ -121,7 +121,7 @@ var loader = function (deps) { var rdep = deps[dep[2][path]]; var exports = rdep ? rdep.module ? rdep.module.exports : initModule(rdep) : global.require(path); return exports; - }, dep.module, dep.module.exports, cwd); + }, dep.module, dep.module.exports, dir); return dep.module.exports; } From 9b5892e6ad6e57b61ab1dfedbe048eec4523f42e Mon Sep 17 00:00:00 2001 From: LorenzGardner Date: Fri, 19 Dec 2014 14:23:40 -0600 Subject: [PATCH 6/6] clarify workaround for native module support --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 68eeea3..a5d36db 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ Nexe is a command-line utility that compiles your Node.js application into a sin ### Doesn't support native modules -- Use the techniques below for working around dynamic require statments to exclude the module from the bundling, and deploy along side the executable in a node_module folder so your app can find it. +- Use the techniques below for working around dynamic require statments to exclude the module from the bundling, and deploy along side the executable in a node_module folder so your app can find it. Note: On windows you may need to have your app be named node.exe if .node file depends on node. ###Doesn't support dynamic require statments Such As: