Skip to content

Commit

Permalink
Better component decoupling.
Browse files Browse the repository at this point in the history
  • Loading branch information
theakman2 committed May 4, 2014
1 parent c6e669d commit b6da9fd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
40 changes: 37 additions & 3 deletions lib/chunkRequireTree.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var path = require("path");

function pushChunk(item,sameAsChunkMap,into) {
var s = sameAsChunkMap[item.filePath];
if (s) {
Expand All @@ -10,6 +12,34 @@ function pushChunk(item,sameAsChunkMap,into) {
}
}

function chunkIdxToUrl(idx,urlDest) {
if (idx === 0) {
return urlDest;
}

var urlDir = path.dirname(urlDest);
var urlExt = path.extname(urlDest);
var urlBase = path.basename(urlDest,urlExt);

if (urlBase + urlExt === urlDest) {
var srcBefore = urlBase;
} else {
var srcBefore = urlDir+"/"+urlBase;
}

return srcBefore + idx + urlExt;
}

function chunkIdxToFilePath(idx,dest) {
if (idx === 0) {
return dest;
}
var dir = path.dirname(dest);
var ext = path.extname(dest);
var base = path.basename(dest,ext);
return (dir + path.sep + base + idx + ext);
}

function map(node,into) {
into = into || {};
if (!into.hasOwnProperty(node.filePath)) {
Expand Down Expand Up @@ -43,18 +73,22 @@ function getSameAsChunkMap(mappings) {
return sameAsChunkMap;
}

function chunkRequireTree(tree) {
function chunkRequireTree(tree,urlDest,dest) {
var mappings = map(tree);
var sameAsChunkMap = getSameAsChunkMap(mappings);

var keys;
var chunks = [];
var idx = 0;
while(keys = Object.keys(sameAsChunkMap), keys.length) {
var into = [];
chunks.push(into);
idx = chunks.push({
modules:into,
urlDest:chunkIdxToUrl(idx,urlDest),
fileDest:chunkIdxToFilePath(idx,dest)
});
pushChunk(mappings[keys[0]],sameAsChunkMap,into);
}

return chunks;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/webant.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function webant(config,callback) {
}

webant.getRequireTree(settings.entry,settings,function(err,tree){
var chunks = webant.chunkRequireTree(tree);
var chunks = webant.chunkRequireTree(tree,settings.urlDest,settings.dest);
webant.write(stringifyChunks(chunks,settings),callback);
});
}
Expand Down

0 comments on commit b6da9fd

Please sign in to comment.