bundle/exe: more ES6, browserify.excludes, browserify.requires in package.json.

This commit is contained in:
Jared Allard
2016-02-23 23:00:19 -08:00
parent 71f39e5a11
commit f50e94a781
8 changed files with 110 additions and 43 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
### Nexe
[![Join the chat at https://gitter.im/crcn/nexe](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/jaredallard/nexe?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![Dependency Status](https://david-dm.org/jaredallard/nexe.svg)](https://david-dm.org/crcn/nexe)
[![Join the chat at https://gitter.im/jaredallard/nexe](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/jaredallard/nexe?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![Dependency Status](https://david-dm.org/jaredallard/nexe.svg)](https://david-dm.org/jaredallard/nexe)
[![Stories in Ready](https://badge.waffle.io/jaredallard/nexe.svg?label=ready&title=Ready)](http://waffle.io/jaredallard/nexe)
Nexe is a command-line utility that compiles your Node.js application into a single executable file.
+20 -3
View File
@@ -62,23 +62,35 @@ function bundle(input, nc, options, complete) {
_log('executing browserify via API');
let bproc = browserify([], {
let bproc = browserify([input], {
debug: options.debug,
commondir: false,
paths: [path.join(nc, 'lib')],
builtins: false,
insertGlobalVars: insertGlobalVars,
detectGlobals: true,
browserField: false
}).add(input);
});
if (options.browserifyExcludes && Array.isArray(options.browserifyExcludes)) {
for (let i = 0; i < options.browserifyExcludes.length; i++) {
let lib = options.browserifyExcludes[i];
_log('Excluding lib from browserify bundle', lib);
_log('Excluding \'%s\' from browserify bundle', lib);
bproc.exclude(lib);
}
}
// copy the excludes code for requires for now.
if (options.browserifyRequires && Array.isArray(options.browserifyRequires)) {
for (let i = 0; i < options.browserifyRequires.length; i++) {
let lib = options.browserifyRequires[i];
let name = lib.file || lib; // for object format.
_log('Force including \'%s\' in browserify bundle', name);
bproc.require(lib);
}
}
if(options.debug) {
bproc.require(require.resolve('source-map-support'))
}
@@ -87,6 +99,11 @@ function bundle(input, nc, options, complete) {
.pipe(exorcist(mapfile, mapfile, './')) // generate source maps.
.pipe(ws) // pipe to file
// error on require errors, still can't contionue. ffs browserify
bprocbun.on('error', function(err) {
_log('error', '[browserify] '+err);
});
ws.on('error', function(err) {
console.log(err);
_log('error', 'Failed to save stdout to disk');
+17 -14
View File
@@ -25,9 +25,21 @@
'use strict';
var path = require("path"),
let path = require("path"),
fs = require("fs");
/**
* Accessort for embed
**/
const accessor = function(key) {
if (embeddedFiles.hasOwnProperty(key)) {
return new Buffer(embeddedFiles[key], 'base64');
} else {
//file was not embedded, throw err.
throw new Error('Embedded file not found');
}
}
/**
* Embed files.
*
@@ -35,8 +47,8 @@ var path = require("path"),
* @param {string} resourceRoot - root of resources.
* @param {function} compelte - callback
**/
function embed(resourceFiles, resourceRoot, complete) {
function encode(filePath) {
function embed(resourceFiles, resourceRoot, options, complete) {
const encode = function(filePath) {
return fs.readFileSync(filePath).toString('base64');
}
@@ -47,8 +59,8 @@ function embed(resourceFiles, resourceRoot, complete) {
throw new Error("Bad Argument: resourceFiles is not an array");
}
var buffer = "var embeddedFiles = {\n";
for (var i = 0; i < resourceFiles.length; ++i) {
let buffer = "var embeddedFiles = {\n";
for (let i = 0; i < resourceFiles.length; ++i) {
buffer += JSON.stringify(path.relative(resourceRoot, resourceFiles[i])) + ': "';
buffer += encode(resourceFiles[i]) + '",\n';
}
@@ -58,13 +70,4 @@ function embed(resourceFiles, resourceRoot, complete) {
complete(null, buffer);
}
var accessor = function(key) {
if (embeddedFiles.hasOwnProperty(key)) {
return new Buffer(embeddedFiles[key], 'base64');
} else {
//file was not embedded, throw err.
throw new Error('Embedded file not found');
}
}
module.exports = embed;
+23 -25
View File
@@ -50,20 +50,6 @@ var isPy;
var framework;
var version;
var option_list = [
"python",
"debug",
"input",
"output",
"nodeTempDir",
"flags",
"framework",
"nodeConfigureArgs",
"nodeMakeArgs",
"nodeVersion",
"browserifyExcludes"
]
/**
* Compiliation process.
*/
@@ -144,20 +130,18 @@ exports.compile = function(options, complete) {
},
/**
* bundle the application into one script
*/
function combineProject(nc, next) {
* Embed Resources into a base64 encoded array.
**/
function embedResources(nc, next) {
nodeCompiler = nc;
_log("bundle %s", options.input);
bundle(options.input, nodeCompiler.dir, options, next);
},
function embedResources(next) {
_log("embedResources %s", options.resourceFiles);
embed(options.resourceFiles, options.resourceRoot, next);
embed(options.resourceFiles, options.resourceRoot, nc, next);
},
/**
* Write nexeres.js
**/
function writeResources(resources, next) {
let resourcePath = path.join(nodeCompiler.dir, "lib", "nexeres.js");
_log("resource -> %s", resourcePath);
@@ -165,6 +149,14 @@ exports.compile = function(options, complete) {
fs.writeFile(resourcePath, resources, next);
},
/**
* Bundle the application into one script
**/
function combineProject(next) {
_log("bundle %s", options.input);
bundle(options.input, nodeCompiler.dir, options, next);
},
/**
* monkeypatch some files so that the nexe.js file is loaded when the app runs
*/
@@ -686,6 +678,7 @@ function _monkeyPatchMainJs(compiler, complete) {
}
/**
* Make child_process work.
*/
function _monkeyPatchChildProcess(compiler, complete) {
var childProcPath = path.join(compiler.dir, "lib", "child_process.js");
@@ -953,7 +946,7 @@ function _logProgress(req) {
* @return {object} nexe.compile - options object
**/
exports.package = function(path, options) {
var _package; // scope
let _package; // scope
// check if the file exists
if (fs.existsSync(path) === false) {
@@ -970,7 +963,7 @@ exports.package = function(path, options) {
}
// construct the object
var obj = {
let obj = {
input: (_package.nexe.input || options.i),
output: (_package.nexe.output || options.o),
flags: (_package.nexe.runtime.ignoreFlags || (options.f || false)),
@@ -986,6 +979,11 @@ exports.package = function(path, options) {
framework: (_package.nexe.runtime.framework || options.f)
}
if(_package.nexe.browserify !== undefined) {
obj.browserifyRequires = (_package.nexe.browserify.requires || []);
obj.browserifyExcludes = (_package.nexe.browserify.excludes || []);
}
Object.keys(_package.nexe).forEach(function(v, i) {
if (v !== "runtime") {
_log("log", v + " => '" + _package.nexe[v] + "'");
+3
View File
@@ -41,6 +41,9 @@
"input": "./bin/nexe",
"output": "nexe^$",
"temp": "src",
"browserifyRequires": [
"lib/nexeres.js"
],
"runtime": {
"framework": "nodejs",
"version": "5.5.0",
+1
View File
@@ -0,0 +1 @@
Hello, world! I'm in a file :)
+11
View File
@@ -0,0 +1,11 @@
/**
* Embedded Files Test
*
* @author Jared Allard <jaredallard@outlook.com>
* @version 0.0.1
* @license MIT
**/
const nexeres = require('nexeres');
console.log(nexeres.get('hw.txt').toString('ascii'))
+34
View File
@@ -0,0 +1,34 @@
{
"name": "embededfiles-test",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node index.js"
},
"dependencies": {
},
"nexe": {
"input": "index.js",
"output": "test.nex",
"resourceFiles": [
"hw.txt"
],
"browserify": {
"excludes": [
"./process"
],
"requires": [
{
"file": "nexeres",
"expose": "nexeres"
}
]
},
"temp": "src",
"debug": false,
"runtime": {
"framework": "node",
"version": "5.5.0"
}
}
}