forked from harthur/brain
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use browserify for building browser file
- Loading branch information
Showing
5 changed files
with
33 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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;\ | ||
})()"; | ||
} |