diff --git a/lib/chunkRequireTree.js b/lib/chunkRequireTree.js index 14f88f8..2c8e630 100644 --- a/lib/chunkRequireTree.js +++ b/lib/chunkRequireTree.js @@ -1,3 +1,5 @@ +var path = require("path"); + function pushChunk(item,sameAsChunkMap,into) { var s = sameAsChunkMap[item.filePath]; if (s) { @@ -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)) { @@ -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; } diff --git a/lib/webant.js b/lib/webant.js index 77e46e3..e9ae92e 100644 --- a/lib/webant.js +++ b/lib/webant.js @@ -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); }); }