Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

code cleanup for glossary and requires strategy

  • Loading branch information...
commit 3bc3babce14ff36ca92da014a2e463eec7d37f49 1 parent e99796e
@logicalparadox authored
Showing with 70 additions and 55 deletions.
  1. +15 −10 lib/folio/glossary.js
  2. +55 −45 lib/folio/strategies/requires.js
View
25 lib/folio/glossary.js
@@ -57,21 +57,25 @@ Glossary.prototype.loglevel = function (level) {
Glossary.prototype.compile = function (cb) {
var self = this
- , source = ''
- , sourceMin = '';
+ , cache = '';
function handleErr (err) {
self.emit('error', err);
cb(err);
}
- function build (next) {
- breeze.forEachSeries(self._stack, function (fn) {
+ function build (done) {
+ var source = '';
+ breeze.forEachSeries(self._stack, function (fn, next) {
fn.compile(source, function (err, src) {
if (err) return next(err);
source = src;
next();
});
+ }, function (err) {
+ if (err) return next(err);
+ cache = source;
+ done(null, source);
});
}
@@ -79,23 +83,24 @@ Glossary.prototype.compile = function (cb) {
if (!self._files.min) return next();
var jsp = require("uglify-js").parser
, pro = require("uglify-js").uglify
- , orig_code = source;
+ , orig_code = cache
+ , source = '';
var ast = jsp.parse(orig_code);
ast = pro.ast_mangle(ast);
ast = pro.ast_squeeze(ast);
- sourceMin = pro.gen_code(ast);
- next();
+ source = pro.gen_code(ast);
+ next(null, source);
}
function save (err, res) {
if (err) return handleErr(err);
var file = path.join(self._attrs.out, self._attrs.name + '.js')
, min = path.join(self._attrs.out, self._attrs.name + '.min.js')
- , saves = [ saveFile(file, source) ];
+ , saves = [ saveFile(file, res.build) ];
- if (self._attrs.min) {
- saves.push(saveFile(min, sourceMin));
+ if (self._attrs.min && res.min) {
+ saves.push(saveFile(min, res.min));
}
breeze.series(saves, function (err) {
View
100 lib/folio/strategies/requires.js
@@ -57,52 +57,35 @@ Requires.prototype.compile = function (str, cb) {
var self = this
, dir = this.attrs.dir;
+ // normalize our paths
if (!fs.isPathAbsolute(this.attrs)) {
var root = this.glossary._attrs.root;
dir = path.resolve(root, this.attrs.dir);
}
+ // get recursive file listing
fs.tree(dir, function (err, res) {
if (err) return cb(err);
- function checkIgnore (file) {
- var res = true
- , ignore = self.attrs.ignore || [];
-
- ignore.forEach(function (ig) {
- var full = !fs.isPathAbsolute(ig)
- ? path.resolve(dir, ig)
- : ig;
- if (~file.indexOf(full)) res = false;
- });
-
- return res;
- }
-
- function filterJs (file) {
- var ext = path.extname(file).toLowerCase()
- if (ext !== '.js') return false;
- return checkIgnore(file);
- }
-
- function mapFiles (file) {
- return {
- req: file.replace(dir + '/', '')
- , file: file
- };
- }
-
+ // prepare our file names
res = res
- .filter(filterJs)
- .map(mapFiles);
-
- console.log(res);
+ .filter(function (file) {
+ var ext = path.extname(file).toLowerCase()
+ if (ext !== '.js') return false;
+ return checkIgnore(file, self.attrs.ignore);
+ })
+ .map(function (file) {
+ return {
+ req: file.replace(dir + '/', '')
+ , file: file
+ };
+ });
- var buf = '';
+ var buf = str;
+ // include commonJS require header
if (self.attrs.header !== false) {
- buf += '\n// CommonJS require()\n\n';
- buf += browser.require + '\n\n';
+ buf += '\n\n' + browser.require + '\n\n';
buf += 'require.modules = {};\n\n';
buf += 'require.resolve = ' + browser.resolve + ';\n\n';
buf += 'require.register = ' + browser.register + ';\n\n';
@@ -110,32 +93,57 @@ Requires.prototype.compile = function (str, cb) {
buf += 'require.alias = ' + browser.alias + ';\n\n';
}
- breeze.forEachSeries(res, function (js, next) {
+ // file iterator: load and modify
+ function iterator (js, next) {
fs.readFile(js.file, 'utf8', function (err, src) {
if (err) return next (err);
+ // modifications
src = parseInheritance(src);
+ src = parseReplace(src, self.attrs.replace);
- if (Array.isArray(self.attrs.replace)) {
- src = parseReplace(src, self.attrs.replace);
- }
-
+ // include in buffer
buf += '\nrequire.register("' + js.req + '", function(module, exports, require){\n';
buf += src;
buf += '\n}); // module: ' + js.req + '\n';
+
+ // continue
next();
});
- }, function (err) {
+ }
+
+ // finish off this require
+ function finalize (err) {
if (err) return cb(err);
+
+ // add entry point
var entry = self.attrs.entry.replace(dir + '/', '')
, name = self.attrs.name;
buf += '\nrequire.alias("' + entry + '", "' + name + '");\n';
+
+ // send back to glossary
cb(null, buf);
- });
+ }
+ // run our iterator on each file
+ breeze.forEachSeries(res, iterator, finalize);
});
};
+function checkIgnore (file, ignore) {
+ var res = true;
+ ignore = ignore || []
+
+ ignore.forEach(function (ig) {
+ var full = !fs.isPathAbsolute(ig)
+ ? path.resolve(dir, ig)
+ : ig;
+ if (~file.indexOf(full)) res = false;
+ });
+
+ return res;
+}
+
function parseInheritance (js) {
return js
.replace(/^ *(\w+)\.prototype\.__proto__ * = *(\w+)\.prototype *;?/gm, function(_, child, parent){
@@ -144,10 +152,12 @@ function parseInheritance (js) {
});
}
-function parseReplace (js, rep) {
- console.log(rep);
- rep.forEach(function (spec) {
+function parseReplace (js, replace) {
+ function iterator (spec) {
js = js.replace(spec.from, spec.to);
- });
+ }
+
+ replace = replace || [];
+ replace.forEach(iterator);
return js;
}
Please sign in to comment.
Something went wrong with that request. Please try again.