Permalink
Browse files

refactor building, use browserify

  • Loading branch information...
harthur committed Nov 28, 2011
1 parent 8bcf84a commit ee00f481734e11f003d435e8069d96b075a108ac
Showing with 51 additions and 97 deletions.
  1. +13 −43 Jakefile.js
  2. +14 −0 build.js
  3. +19 −17 package.json
  4. +5 −37 test/visual/runvisual.js
View
@@ -1,68 +1,38 @@
/*
- Turns CommonJS package into a browser file.
+ 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");
+
+var pkg = JSON.parse(fs.readFileSync("package.json"));
+var prefix = pkg.name + "-" + pkg.version;
-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 || prefix + ".js";
+ build.build(dest);
sys.puts("> " + dest);
});
task('minify', [], function (file, dest) {
- var name = getPackage().name;
- file = file || name + ".js";
- dest = dest || name + ".min.js";
+ file = file || prefix + ".js";
+ dest = dest || prefix + ".min.js";
var minified = minify(fs.readFileSync(file, "utf-8"));
fs.writeFileSync(dest, minified, "utf-8");
sys.puts("> " + dest)
});
task('clean', [], function () {
- var name = getPackage().name;
- fs.unlink(name + ".js");
- fs.unlink(name + ".min.js");
+ fs.unlink(prefix + ".js");
+ fs.unlink(prefix + ".min.js");
});
-
-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,
View
@@ -0,0 +1,14 @@
+var fs = require("fs"),
+ path = require("path"),
+ browserify = require("browserify");
+
+var pkg = JSON.parse(fs.readFileSync("package.json"));
+
+exports.build = function(dest) {
+ var source = browserify.bundle({
+ require: [path.join(__dirname, pkg.main)],
+ });
+ source = "/* MIT license */\nvar clusterfck = (function() {"
+ + source + " return require('/clusterfck')})();"
+ fs.writeFileSync(dest, source);
+}
View
@@ -1,23 +1,25 @@
{
- "name": "clusterfck",
- "description": "hierarchical clustering",
- "version": "0.1.0",
- "author": "Heather Arthur <fayearthur@gmail.com>",
- "repository": {
- "type": "git",
- "url": "http://github.com/harthur/clusterfck.git"
- },
- "directories": {
- "lib": "./lib"
- },
- "dependencies" : {
- },
- "devDependencies": {
+ "name": "clusterfck",
+ "description": "hierarchical clustering",
+ "version": "0.1.1",
+ "author": "Heather Arthur <fayearthur@gmail.com>",
+ "repository": {
+ "type": "git",
+ "url": "http://github.com/harthur/clusterfck.git"
+ },
+ "directories": {
+ "lib": "./lib"
+ },
+ "dependencies" : {
+
+ },
+ "devDependencies": {
"uglify-js" : "*",
"underscore": "*",
"nomnom" : "*",
"cradle" : "*",
- "connect": "*"
- },
- "main": "./lib/clusterfck"
+ "connect": "*",
+ "browserify": "*"
+ },
+ "main": "./lib/clusterfck"
}
View
@@ -1,46 +1,14 @@
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");
-build(path.resolve(__dirname, "../../package.json"));
+build.build(path.join(root, "/clusterfck.js"));
connect.createServer(
- connect.staticProvider({ root: root, cache: true })
+ connect.static(root)
).listen(3000);
-sys.puts("visit http://127.0.0.1:3000/vis.html");
-
-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 code = "var " + name + " = " + getCode(pkg.main + ".js", " ");
- 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;\
- })()";
-}
+sys.puts("visit http://127.0.0.1:3000/vis.html");

0 comments on commit ee00f48

Please sign in to comment.