Skip to content

Commit

Permalink
use browserify for building browser file
Browse files Browse the repository at this point in the history
  • Loading branch information
harthur committed May 19, 2011
1 parent f36fc7b commit 7c05af5
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 84 deletions.
40 changes: 6 additions & 34 deletions Jakefile.js
@@ -1,23 +1,18 @@
/*
Turns CommonJS package into a browser file.
Minifying requires UglifyJS (http://github.com/mishoo/UglifyJS)
to be in the dir above this one.
Turns CommonJS package into a browser file and minifies.
uses node-jake http://github.com/mde/node-jake
run with 'jake [build|minify|clean]'
*/
var fs = require("fs"),
path = require("path"),
sys = require("sys");
sys = require("sys")
build = require("./build");

task('build', [], function (name, dest) {
task('build', [], function (dest) {
sys.puts("building...");
var pkg = getPackage();
name = name || pkg.name;
dest = dest || name + ".js";

var code = "var " + name + " = " + getCode(pkg.main + ".js", " ");
fs.writeFileSync(dest, code, "utf-8");
dest = dest || getPackage().name + ".js";
build.build(dest);
sys.puts("> " + dest);
});

Expand All @@ -42,29 +37,6 @@ function getPackage() {
return JSON.parse(fs.readFileSync("package.json"));
}

function getCode(file, indent) {
sys.puts(indent + file);
var code = fs.readFileSync(file, "utf-8");

// replace all the require("mod")s with their code
// can't handle dep cycles
var re = /require\(["'](.+?)["']\)/g;
function expand(match, mod) {
if(mod.indexOf(".") != 0)
return "window"; // external dep, assume it will be global
var dep = path.join(path.dirname(file), mod + ".js");
return getCode(dep, indent + " ");
}
code = code.replace(re, expand);

return "(function() {\n\
var module = { exports: {}};\n\
var exports = module.exports;\n"
+ code +
"\nreturn module.exports;\
})()";
}

function minify(code) {
var uglifyjs = require("uglify-js"),
parser = uglifyjs.parser,
Expand Down
17 changes: 7 additions & 10 deletions README.md
Expand Up @@ -22,29 +22,26 @@ Naive Bayesian classifier example:
var category = bayes.classify("free watches");


# using as a commonJS package
If you have [node](http://nodejs.org/) and [npm](http://github.com/isaacs/npm) you can:
# using in node
If you have [node](http://nodejs.org/) you can install with [npm](http://github.com/isaacs/npm):

npm install brain

then:

var brain = require("brain");
var net = new brain.NeuralNetwork();

# using in the browser
Download the latest [brain.js](http://github.com/harthur/brain/downloads). If you're using `BayesianClassifier`, you can only use the `localStorage` and (default) in-memory backends, and you'll need to grab [underscore.js](http://documentcloud.github.com/underscore/). If you're using the `NeuralNetwork` you should try to train the network offline (or on a Worker) and use the `toFunction()` or `toJSON()` options to plug the pre-trained network in to your website.
Download the latest [brain.js](http://github.com/harthur/brain/downloads). If you're using `BayesianClassifier`, you can only use the `localStorage` and (default) in-memory backends. If you're using the `NeuralNetwork` you should try to train the network offline (or on a Worker) and use the `toFunction()` or `toJSON()` options to plug the pre-trained network in to your website.


# tests
Running the tests requires checking out the code and installing the dev dependencies: `npm install brain --dev`. To run the suite of tests:
Running the tests requires checking out the code and installing the dev dependencies: `npm install --dev`. To run the suite of tests:

node test/runtests.js

To run the other tests: [browser and cross-validation tests](https://github.com/harthur/brain/tree/master/test)

# build
To build a browser file from the CommonJS package you'll need [node-jake](https://github.com/mde/node-jake), then:
To build a browser file from the CommonJS package you'll need [node-jake](https://github.com/mde/node-jake):

npm install jake -g

jake build
jake minify
15 changes: 15 additions & 0 deletions build.js
@@ -0,0 +1,15 @@
var fs = require("fs");

exports.build = function(dest) {
var source = require("browserify").bundle({
name: "brain",
base: __dirname + "/lib",
main: __dirname + "/lib/brain.js",
require: "underscore",
shim: false
});

source = "var brain = (function() {" + source + " return require('brain')})();"

fs.writeFileSync(dest, source);
}
5 changes: 2 additions & 3 deletions package.json
@@ -1,7 +1,7 @@
{
"name": "brain",
"description": "neural network and classifier library",
"version": "0.3.4",
"version": "0.3.5",
"author": "Heather Arthur <fayearthur@gmail.com>",
"repository": {
"type": "git",
Expand All @@ -17,8 +17,7 @@
"connect" : ">=1.4.0",
"uglify-js" : ">=1.0.1",
"colors" : ">=0.5.0",
"jake": ">=0.1.12"
"browserify" : ">=0.4.1"
},

"main": "./lib/brain"
}
40 changes: 3 additions & 37 deletions test/browser/browsertest.js
@@ -1,51 +1,17 @@
var connect = require('connect'),
fs = require("fs"),
path = require("path"),
sys = require("sys");
sys = require("sys")
build = require("../../build");

var root = path.join(__dirname, "files");

exports.runTests = function() {
build(path.resolve(__dirname, "../../package.json"));
build.build(root + "/brain.js");

connect.createServer(
connect.static(root)
).listen(3000);

sys.puts("visit http://127.0.0.1:3000/index.html");
}

/* todo - use same code as Jakefile */
function build(pkgFile, name, dest) {
sys.puts("building...");
var pkg = JSON.parse(fs.readFileSync(pkgFile || "package.json"));
name = name || pkg.name;
dest = dest || path.join(root, name + ".js");
var main = path.join(path.dirname(pkgFile), pkg.main + ".js");
var code = "var " + name + " = " + getCode(main, " ");
fs.writeFileSync(dest, code, "utf-8");
sys.puts("> " + dest);
};

function getCode(file, indent) {
sys.puts(indent + file);
var code = fs.readFileSync(file, "utf-8");

// replace all the require("mod")s with their code
// can't handle dep cycles
var re = /require\(["'](.+?)["']\)/g;
function expand(match, mod) {
if(mod.indexOf(".") != 0)
return "window"; // external dep, assume it will be global
var dep = path.join(path.dirname(file), mod + ".js");
return getCode(dep, indent + " ");
}
code = code.replace(re, expand);

return "(function() {\n\
var module = { exports: {}};\n\
var exports = module.exports;\n"
+ code +
"\nreturn module.exports;\
})()";
}

0 comments on commit 7c05af5

Please sign in to comment.