Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

refactor building, use browserify

  • Loading branch information...
commit ee00f481734e11f003d435e8069d96b075a108ac 1 parent 8bcf84a
Heather Arthur authored
56 Jakefile.js
... ... @@ -1,28 +1,27 @@
1 1 /*
2   - Turns CommonJS package into a browser file.
  2 + Turns CommonJS package into a browser file and minifies.
3 3
4 4 uses node-jake http://github.com/mde/node-jake
5 5 run with 'jake [build|minify|clean]'
6 6 */
7 7 var fs = require("fs"),
8 8 path = require("path"),
9   - sys = require("sys");
  9 + sys = require("sys")
  10 + build = require("./build");
  11 +
  12 +var pkg = JSON.parse(fs.readFileSync("package.json"));
  13 +var prefix = pkg.name + "-" + pkg.version;
10 14
11   -task('build', [], function (name, dest) {
  15 +task('build', [], function (dest) {
12 16 sys.puts("building...");
13   - var pkg = getPackage();
14   - name = name || pkg.name;
15   - dest = dest || name + ".js";
16   -
17   - var code = "var " + name + " = " + getCode(pkg.main + ".js", " ");
18   - fs.writeFileSync(dest, code, "utf-8");
  17 + dest = dest || prefix + ".js";
  18 + build.build(dest);
19 19 sys.puts("> " + dest);
20 20 });
21 21
22 22 task('minify', [], function (file, dest) {
23   - var name = getPackage().name;
24   - file = file || name + ".js";
25   - dest = dest || name + ".min.js";
  23 + file = file || prefix + ".js";
  24 + dest = dest || prefix + ".min.js";
26 25
27 26 var minified = minify(fs.readFileSync(file, "utf-8"));
28 27 fs.writeFileSync(dest, minified, "utf-8");
@@ -30,39 +29,10 @@ task('minify', [], function (file, dest) {
30 29 });
31 30
32 31 task('clean', [], function () {
33   - var name = getPackage().name;
34   - fs.unlink(name + ".js");
35   - fs.unlink(name + ".min.js");
  32 + fs.unlink(prefix + ".js");
  33 + fs.unlink(prefix + ".min.js");
36 34 });
37 35
38   -
39   -function getPackage() {
40   - return JSON.parse(fs.readFileSync("package.json"));
41   -}
42   -
43   -function getCode(file, indent) {
44   - sys.puts(indent + file);
45   - var code = fs.readFileSync(file, "utf-8");
46   -
47   - // replace all the require("mod")s with their code
48   - // can't handle dep cycles
49   - var re = /require\(["'](.+?)["']\)/g;
50   - function expand(match, mod) {
51   - if(mod.indexOf(".") != 0)
52   - return "window"; // external dep, assume it will be global
53   - var dep = path.join(path.dirname(file), mod + ".js");
54   - return getCode(dep, indent + " ");
55   - }
56   - code = code.replace(re, expand);
57   -
58   - return "(function() {\n\
59   - var module = { exports: {}};\n\
60   - var exports = module.exports;\n"
61   - + code +
62   - "\nreturn module.exports;\
63   - })()";
64   -}
65   -
66 36 function minify(code) {
67 37 var uglifyjs = require("uglify-js"),
68 38 parser = uglifyjs.parser,
14 build.js
... ... @@ -0,0 +1,14 @@
  1 +var fs = require("fs"),
  2 + path = require("path"),
  3 + browserify = require("browserify");
  4 +
  5 +var pkg = JSON.parse(fs.readFileSync("package.json"));
  6 +
  7 +exports.build = function(dest) {
  8 + var source = browserify.bundle({
  9 + require: [path.join(__dirname, pkg.main)],
  10 + });
  11 + source = "/* MIT license */\nvar clusterfck = (function() {"
  12 + + source + " return require('/clusterfck')})();"
  13 + fs.writeFileSync(dest, source);
  14 +}
36 package.json
... ... @@ -1,23 +1,25 @@
1 1 {
2   - "name": "clusterfck",
3   - "description": "hierarchical clustering",
4   - "version": "0.1.0",
5   - "author": "Heather Arthur <fayearthur@gmail.com>",
6   - "repository": {
7   - "type": "git",
8   - "url": "http://github.com/harthur/clusterfck.git"
9   - },
10   - "directories": {
11   - "lib": "./lib"
12   - },
13   - "dependencies" : {
14   - },
15   - "devDependencies": {
  2 + "name": "clusterfck",
  3 + "description": "hierarchical clustering",
  4 + "version": "0.1.1",
  5 + "author": "Heather Arthur <fayearthur@gmail.com>",
  6 + "repository": {
  7 + "type": "git",
  8 + "url": "http://github.com/harthur/clusterfck.git"
  9 + },
  10 + "directories": {
  11 + "lib": "./lib"
  12 + },
  13 + "dependencies" : {
  14 +
  15 + },
  16 + "devDependencies": {
16 17 "uglify-js" : "*",
17 18 "underscore": "*",
18 19 "nomnom" : "*",
19 20 "cradle" : "*",
20   - "connect": "*"
21   - },
22   - "main": "./lib/clusterfck"
  21 + "connect": "*",
  22 + "browserify": "*"
  23 + },
  24 + "main": "./lib/clusterfck"
23 25 }
42 test/visual/runvisual.js
... ... @@ -1,46 +1,14 @@
1 1 var connect = require('connect'),
2 2 fs = require("fs"),
3 3 path = require("path"),
4   - sys = require("sys");
  4 + sys = require("sys"),
  5 + build = require("../../build");
5 6
6 7 var root = path.join(__dirname, "files");
7   -build(path.resolve(__dirname, "../../package.json"));
  8 +build.build(path.join(root, "/clusterfck.js"));
8 9
9 10 connect.createServer(
10   - connect.staticProvider({ root: root, cache: true })
  11 + connect.static(root)
11 12 ).listen(3000);
12 13
13   -sys.puts("visit http://127.0.0.1:3000/vis.html");
14   -
15   -function build(pkgFile, name, dest) {
16   - sys.puts("building...");
17   - var pkg = JSON.parse(fs.readFileSync(pkgFile || "package.json"));
18   - name = name || pkg.name;
19   - dest = dest || path.join(root, name + ".js");
20   - var code = "var " + name + " = " + getCode(pkg.main + ".js", " ");
21   - fs.writeFileSync(dest, code, "utf-8");
22   - sys.puts("> " + dest);
23   -};
24   -
25   -function getCode(file, indent) {
26   - sys.puts(indent + file);
27   - var code = fs.readFileSync(file, "utf-8");
28   -
29   - // replace all the require("mod")s with their code
30   - // can't handle dep cycles
31   - var re = /require\(["'](.+?)["']\)/g;
32   - function expand(match, mod) {
33   - if(mod.indexOf(".") != 0)
34   - return "window"; // external dep, assume it will be global
35   - var dep = path.join(path.dirname(file), mod + ".js");
36   - return getCode(dep, indent + " ");
37   - }
38   - code = code.replace(re, expand);
39   -
40   - return "(function() {\n\
41   - var module = { exports: {}};\n\
42   - var exports = module.exports;\n"
43   - + code +
44   - "\nreturn module.exports;\
45   - })()";
46   -}
  14 +sys.puts("visit http://127.0.0.1:3000/vis.html");

0 comments on commit ee00f48

Please sign in to comment.
Something went wrong with that request. Please try again.