Browse files

Improved caching and joining of files

  • Loading branch information...
1 parent d370eff commit e1361cc683138a89b0c48592d787bd7968815bc2 @serby committed Feb 6, 2012
Showing with 82 additions and 28 deletions.
  1. +29 −28 lib/compact.js
  2. +53 −0 test/compact.test.js
View
57 lib/compact.js
@@ -17,7 +17,6 @@ module.exports.createCompact = function(sourcePath, destinationPath, webPath) {
}
var namespaces = Object.create(null)
- , compactedJs = []
, namespaceGroupsCache = {}
, compressOperationCache = {};
@@ -81,13 +80,13 @@ module.exports.createCompact = function(sourcePath, destinationPath, webPath) {
return callback(error);
}
- fs.writeFile(outputFilename, compress(contents.join('')), 'utf-8', function(error) {
+ fs.writeFile(outputFilename, compress(contents.join(';\n')), 'utf-8', function(error) {
if (error) {
return callback(error);
}
- compactedJs.push(path.normalize(webPath + '/' + compactFilename));
+ var compactedWebPath = path.normalize(webPath + '/' + compactFilename);
compressOperationCache[compactFilename] = true;
- callback(undefined, outputFilename);
+ callback(undefined, compactedWebPath);
});
});
}
@@ -99,9 +98,18 @@ module.exports.createCompact = function(sourcePath, destinationPath, webPath) {
return uglifyer.gen_code(ast);
}
- function processNamespace(namespace) {
- if (!namespaces[namespace]) {
- throw new RangeError('Unknown namespace \'' + namespace + '\'');
+ function processNamespaceGroups(namespaceGroups, callback) {
+ var hash = namespaceGroups.join('|');
+ if (!namespaceGroupsCache[hash]) {
+ async.map(namespaceGroups, compressAndWriteJavascript, function(error, results) {
+ if (error) {
+ return callback(error);
+ }
+ namespaceGroupsCache[hash] = results;
+ callback(undefined, results);
+ });
+ } else {
+ callback(undefined, namespaceGroupsCache[hash]);
}
}
@@ -111,32 +119,25 @@ module.exports.createCompact = function(sourcePath, destinationPath, webPath) {
}
var namespaceGroups = Array.prototype.slice.call(arguments);
- compactedJs = [];
-
return function(req, res, next) {
- // Only do the work once for each namespaceGroups
- var hash = namespaceGroups.join('|');
- if (namespaceGroupsCache[hash]) {
- next();
- }
-
-
- async.forEach(namespaceGroups, compressAndWriteJavascript, function(error) {
+ processNamespaceGroups(namespaceGroups, function(error, results) {
if (error) {
return next(error);
}
-
- req.app.helpers({
- compactJs: function() {
- return compactedJs;
- },
- compactJsHtml: function() {
- return compactedJs.map(function(filename) {
- return '<script src="/' + filename + '"></script>';
- }).join('');
- },
+ var app = req.app;
+ app.configure(function() {
+ app.helpers({
+ compactJs: function() {
+ return results;
+ },
+ compactJsHtml: function() {
+ return results.map(function(filename) {
+ return '<script src="' + filename + '"></script>';
+ }).join('');
+ }
+ });
});
- namespaceGroupsCache[hash] = true;
+
next();
});
};
View
53 test/compact.test.js
@@ -133,6 +133,9 @@ describe('compact.js', function() {
helpers: function(helper) {
helper.compactJs().should.eql(['/global.js']);
done();
+ },
+ configure: function(fn) {
+ fn();
}
}
}
@@ -156,6 +159,9 @@ describe('compact.js', function() {
helpers: function(helper) {
helper.compactJs().should.eql(['/global-profile.js']);
done();
+ },
+ configure: function(fn) {
+ fn();
}
}
}
@@ -181,12 +187,59 @@ describe('compact.js', function() {
helpers: function(helper) {
helper.compactJs().should.eql(['/global-profile.js', '/blog.js']);
done();
+ },
+ configure: function(fn) {
+ fn();
}
}
}
, res;
compact.js(['global', 'profile'], ['blog'])(req, res, function() {});
});
+
+ it('should returned the correct helpers', function(done) {
+
+ compact.addNamespace('global')
+ .addJs('/a.js')
+ .addJs('/b.js')
+ .addJs('/c.js');
+
+ compact.addNamespace('profile')
+ .addJs('/b.js');
+
+ var
+ doneCount = 0,
+ app = {
+ helpers: null,
+ configure: function(fn) {
+ fn();
+ }
+ },
+ res,
+ globalReq = { app: app },
+ profileReq = { app: app };
+
+ globalReq.app.helpers = function(helper) {
+ helper.compactJs().should.eql(['/global.js']);
+ doneCount += 1;
+ if (doneCount === 2) {
+ done();
+ }
+ };
+
+ compact.js(['global'])(globalReq, res, function() {
+ profileReq.app.helpers = function(helper) {
+ helper.compactJs().should.eql(['/profile.js']);
+ doneCount += 1;
+ if (doneCount === 2) {
+ done();
+ }
+ };
+ compact.js(['profile'])(profileReq, res, function() {});
+ });
+
+
+ });
});
});

0 comments on commit e1361cc

Please sign in to comment.