Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #17 from mixu/master

Adds a Stylus-like API for browserbuild
  • Loading branch information...
commit 18b45f767fa30411924b79ba5fe2ba40d0a8eb7e 2 parents 28e0f52 + 775e24f
@rauchg rauchg authored
Showing with 108 additions and 86 deletions.
  1. +6 −86 bin/browserbuild
  2. +101 −0 lib/browserbuild.js
  3. +1 −0  package.json
View
92 bin/browserbuild
@@ -1,23 +1,8 @@
#!/usr/bin/env node
var program = require('commander')
- , path = require('path')
- , fs = require('fs')
, pwd = process.cwd()
- , debug = require('debug')('browserbuild')
-
-// reads a client-side file
-
-function read (file) {
- return fs.readFileSync(file, 'utf8')
-}
-
-// source for client-side require
-
-var source = read(__dirname + '/../lib/require.js')
- .replace(/\/*([^/]+)\/\n/g, '')
- .replace(/\n/g, '')
- .replace(/ +/g, ' ')
+ , browserbuild = require('../lib/browserbuild.js')
// program
@@ -42,74 +27,9 @@ if (!program.args.length) {
return;
}
-// process argument
-
-var target = program.args
- , mod = path.basename(program.main)
-
-function handle (filepath) {
- var stat = fs.statSync(filepath);
-
- if (stat.isDirectory()) {
- var files = fs.readdirSync(filepath);
-
- files.forEach(function (f) {
- handle(filepath + '/' + f);
- });
- } else {
- var base = program.basepath
- , source = fs.readFileSync(filepath, 'utf8')
-
- if (base && base == filepath.substr(0, base.length)) {
- filepath = filepath.substr(base.length);
- }
-
- // remove `if node`
- var ignoring = false
-
- source = source.split('\n').map(function (line, i) {
- if (ignoring) {
- if (/^ *\/\/ *end/.test(line)) {
- ignoring = false;
- }
- return '';
- } else {
- if (/^ *\/\/ *if *node/.test(line)) {
- debug('[%s] skipping node-only code at line %d'
- , path.basename(filepath), i + 1);
- ignoring = true;
- return '';
- } else {
- return line;
- }
- }
- }).join('\n');
-
- // wrap
- data += 'require.register("' + filepath + '", '
- + 'function(module, exports, require, global){\n' + source + '\n});';
- }
-};
-
-// debug
-var debugCode;
-if (program.debug) {
- debugCode = read(__dirname + '/../support/debug/debug.js');
-} else {
- debugCode = 'function debug(){return debug};';
-}
-
-// head
-var data = '(function(){'
- + 'var global = this;'
- + debugCode
- + source
-
-// handle files
-program.args.forEach(handle);
-
-// tail
-data += (program.global || program.main) + ' = require(\'' + mod + '\');\n';
-data += '})();';
+// process arguments
-console.log(data);
+browserbuild(program.args, program)
+ .render(function (err, txt) {
+ console.log(txt);
+ });
View
101 lib/browserbuild.js
@@ -0,0 +1,101 @@
+var fs = require('fs'),
+ path = require('path'),
+ debug = require('debug')('browserbuild');
+
+// source for client-side require
+var requireCode = fs.readFileSync(__dirname + '/require.js', 'utf8')
+ .replace(/\/*([^/]+)\/\n/g, '')
+ .replace(/\n/g, '')
+ .replace(/ +/g, ' ');
+
+var debugCode = fs.readFileSync(__dirname + '/../support/debug/debug.js', 'utf8');
+
+function Renderer(options) {
+ this.options = options || {};
+ this.paths = [];
+};
+
+Renderer.prototype.set = function(key, val){
+ this.options[key] = val;
+ return this;
+};
+
+Renderer.prototype.include = function(filepath){
+ if(!filepath) return this;
+ var self = this,
+ paths = (Array.isArray(filepath) ? filepath : [ filepath ]);
+
+ paths.forEach(function(p) {
+ var isDirectory = fs.statSync(p).isDirectory();
+ if (isDirectory) {
+ return fs.readdirSync(p).forEach(function (f) {
+ self.include(p + '/' + f);
+ });
+ }
+ self.paths.push(p);
+ });
+ return this;
+};
+
+Renderer.prototype.render = function(done){
+ if(!done) return this;
+ var self = this,
+ opt = this.options,
+ mod = path.basename(opt.main);
+
+ var data = '(function(){'
+ + 'var global = this;'
+ + (opt.debug ? debugCode : 'function debug(){return debug};')
+ + requireCode
+ + this.paths.reduce(function(str, path) {
+ return str + self._render(path);
+ }, '')
+ + (opt.global || opt.main) + ' = require(\'' + mod + '\');\n';
+ + '})();';
+
+ done(undefined, data);
+};
+
+Renderer.prototype._render = function(filepath) {
+ var base = this.options.basepath
+ , source = fs.readFileSync(filepath, 'utf8')
+
+ if (base && base == filepath.substr(0, base.length)) {
+ filepath = filepath.substr(base.length);
+ }
+
+ // remove `if node`
+ var ignoring = false
+
+ source = source.split('\n').map(function (line, i) {
+ if (ignoring) {
+ if (/^ *\/\/ *end/.test(line)) {
+ ignoring = false;
+ }
+ return '';
+ } else {
+ if (/^ *\/\/ *if *node/.test(line)) {
+ debug('[%s] skipping node-only code at line %d'
+ , path.basename(filepath), i + 1);
+ ignoring = true;
+ return '';
+ } else {
+ return line;
+ }
+ }
+ }).join('\n');
+
+ // wrap
+ return 'require.register("' + filepath + '", '
+ + 'function(module, exports, require, global){\n' + source + '\n});';
+};
+
+function render(paths, options, fn) {
+ if ('function' == typeof options) fn = options, options = {};
+ return new Renderer(options).include(paths).render(fn);
+};
+
+// e.g. browserbuild(paths, options).set(key, val).include(paths).render(fn)
+exports = module.exports = render;
+// e.g. browserbuild.render(paths, options, fn)
+exports.render = render;
View
1  package.json
@@ -2,6 +2,7 @@
"name": "browserbuild"
, "version": "0.4.8"
, "bin": { "browserbuild": "./bin/browserbuild" }
+ , "main": "./lib/browserbuild.js"
, "dependencies": {
"commander": "0.5.x"
, "debug": "0.6.0"
Please sign in to comment.
Something went wrong with that request. Please try again.