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.
browser file building more correct and better
- Loading branch information
Showing
1 changed file
with
51 additions
and
71 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,76 @@ | ||
/* | ||
Turns CommonJS package into a browser file. | ||
Requires UglifyJS (http://github.com/mishoo/UglifyJS) | ||
Minifying requires UglifyJS (http://github.com/mishoo/UglifyJS) | ||
to be in the dir above this one. | ||
uses node-jake http://github.com/mde/node-jake | ||
run with 'jake [browser|minify|clean]' | ||
run with 'jake [build|minify|clean]' | ||
*/ | ||
|
||
var jsp = require("../UglifyJS/lib/parse-js"), | ||
pro = require("../UglifyJS/lib/process"), | ||
fs = require("fs"), | ||
var fs = require("fs"), | ||
path = require("path"), | ||
sys = require("sys"); | ||
|
||
task('browser', [], function () { | ||
// stitch everything under 'lib' together | ||
var files = readdirDeepSync("lib"); | ||
var code = stitch(files); | ||
|
||
// replace require("x")s | ||
var externals = ["underscore"]; | ||
var lines = code.split("\n"); | ||
lines = lines.map(function(line) { | ||
// remove requires for external resources that will be global | ||
externals.forEach(function(external) { | ||
line = line.replace(new RegExp('.*require\\(.*' + external + '.*'), ""); | ||
}); | ||
line = line.replace(/require\(.*?\)/, "exports"); | ||
return line; | ||
}); | ||
code = lines.join("\n"); | ||
task('build', [], function (name, dest) { | ||
sys.puts("building..."); | ||
var pkg = getPackage(); | ||
name = name || pkg.name; | ||
dest = dest || name + ".js"; | ||
|
||
// namespace it | ||
code = license() + header("brain") + code + footer(); | ||
|
||
fs.writeFileSync("brain.js", code, "utf-8"); | ||
var code = "var " + name + " = " + getCode(pkg.main + ".js", " "); | ||
fs.writeFileSync(dest, code, "utf-8"); | ||
sys.puts("> " + dest); | ||
}); | ||
|
||
task('minify', [], function (filename) { | ||
var filename = filename || "brain.js"; | ||
var minified = minify(fs.readFileSync(filename, "utf-8")) | ||
fs.writeFileSync("brain-min.js", minified, "utf-8"); | ||
task('minify', [], function (file, dest) { | ||
var name = getPackage().name; | ||
file = file || name + ".js"; | ||
dest = dest || name + ".min.js"; | ||
|
||
var minified = minify(fs.readFileSync(file, "utf-8")); | ||
fs.writeFileSync(dest, minified, "utf-8"); | ||
sys.puts("> " + dest) | ||
}); | ||
|
||
task('clean', [], function (filename) { | ||
fs.unlink("brain.js"); | ||
fs.unlink("brain-min.js"); | ||
task('clean', [], function () { | ||
var name = getPackage().name; | ||
fs.unlink(name + ".js"); | ||
fs.unlink(name + ".min.js"); | ||
}); | ||
|
||
|
||
/* utils */ | ||
function minify(code) { | ||
var ast = jsp.parse(code); | ||
ast = pro.ast_mangle(ast); | ||
ast = pro.ast_squeeze(ast); | ||
return pro.gen_code(ast); | ||
function getPackage() { | ||
return JSON.parse(fs.readFileSync("package.json")); | ||
} | ||
|
||
function license(filename) { | ||
filename = filename || "LICENSE"; | ||
var license = fs.readFileSync(filename, "utf-8"); | ||
return "/*\n" + license + "\n*/\n\n" | ||
} | ||
function getCode(file, indent) { | ||
sys.puts(indent + file); | ||
var code = fs.readFileSync(file, "utf-8"); | ||
|
||
function header(namespace) { | ||
return "var " + namespace + " = (function() {\n\ | ||
var exports = {};\n"; | ||
} | ||
// 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); | ||
|
||
function footer() { | ||
return "return exports;\n\ | ||
})();\n"; | ||
return "(function() {\n\ | ||
var module = { exports: {}};\n\ | ||
var exports = module.exports;\n" | ||
+ code + | ||
"\nreturn module.exports;\ | ||
})()"; | ||
} | ||
|
||
function stitch(files) { | ||
var codez = []; | ||
files.forEach(function(file) { | ||
codez.push(fs.readFileSync(file, "utf-8")); | ||
}); | ||
return codez.join("\n"); | ||
} | ||
function minify(code) { | ||
var jsp = require("../UglifyJS/lib/parse-js"), | ||
pro = require("../UglifyJS/lib/process"); | ||
|
||
function readdirDeepSync(dir) { | ||
var files = []; | ||
fs.readdirSync(dir).forEach(function(file) { | ||
var fname = path.join(dir, file); | ||
var stats = fs.statSync(fname); | ||
if(stats.isDirectory()) { | ||
files = files.concat(readdirDeepSync(fname)); | ||
} else { | ||
files.push(fname); | ||
} | ||
}); | ||
return files; | ||
var ast = jsp.parse(code); | ||
ast = pro.ast_mangle(ast); | ||
ast = pro.ast_squeeze(ast); | ||
return pro.gen_code(ast); | ||
} |