diff --git a/lib/compile.js b/lib/compile.js index 62898aa..61d8316 100644 --- a/lib/compile.js +++ b/lib/compile.js @@ -185,38 +185,20 @@ function compileTree(loader, tree, traceOpts, compileOpts, outputOpts, cache) { // verify that the tree is a tree verifyTree(tree); - + var ordered = getTreeModulesPostOrder(tree, traceOpts); - // compileOpts.entryPoints can be unnormalized var inputEntryPoints; - if (compileOpts.entryPoints) - inputEntryPoints = compileOpts.entryPoints.map(function(entryPoint) { - return loader.getCanonicalName(loader.normalizeSync(entryPoint)); - }); // get entrypoints from graph algorithm - var entryPoints = inputEntryPoints || []; + var entryPoints; - ordered.entryPoints.forEach(function(entryPoint) { - if (entryPoints.indexOf(entryPoint) == -1) - entryPoints.push(entryPoint); - }); - - var modules = ordered.modules.filter(function(moduleName) { - var load = tree[moduleName]; - if (load.runtimePlugin && compileOpts.static) - throw new TypeError('Plugin ' + load.plugin + ' does not support static builds, compiling ' + load.name + '.'); - return load && !load.conditional && !load.runtimePlugin; - }); + var modules; // plugins have the ability to report an asset list during builds var assetList = []; - if (compileOpts.encodeNames) - entryPoints = entryPoints.map(function(name) { - return getEncoding(name, cache.encodings) - }); + var outputs = []; var outputs = []; @@ -224,25 +206,63 @@ function compileTree(loader, tree, traceOpts, compileOpts, outputOpts, cache) { var pluginLoads = {}; var compilers = {}; - // create load output objects - return Promise.all(modules.map(function(name) { - return Promise.resolve() - .then(function() { - var load = tree[name]; + return Promise.resolve() + .then(function() { + // compileOpts.entryPoints can be unnormalized + if (!compileOpts.entryPoints) + return []; - if (load === true) - throw new TypeError(name + ' was defined via a bundle, so can only be used for subtraction or union operations.'); + return Promise.all(compileOpts.entryPoints.map(function(entryPoint) { - // store plugin loads for bundle hook - if (load.metadata.loader) { - var pluginLoad = extend({}, load); - pluginLoad.address = loader.baseURL + load.path; - (pluginLoads[load.metadata.loader] = pluginLoads[load.metadata.loader] || []).push(pluginLoad); - } + // NB do we need to check that entry points are conditionally unique? + return loader.normalize(entryPoint) + .then(function(normalized) { + return loader.getCanonicalName(normalized); + }); + })); + }) + .then(function(inputEntryPoints) { + entryPoints = inputEntryPoints || []; + + ordered.entryPoints.forEach(function(entryPoint) { + if (entryPoints.indexOf(entryPoint) == -1) + entryPoints.push(entryPoint); + }); - return compileLoad(loader, tree[name], compileOpts, cache); + modules = ordered.modules.filter(function(moduleName) { + var load = tree[moduleName]; + if (load.runtimePlugin && compileOpts.static) + throw new TypeError('Plugin ' + load.plugin + ' does not support static builds, compiling ' + load.name + '.'); + return load && !load.conditional && !load.runtimePlugin; }); - })) + + if (compileOpts.encodeNames) + entryPoints = entryPoints.map(function(name) { + return getEncoding(name, cache.encodings) + }); + }) + + // create load output objects + .then(function() { + return Promise.all(modules.map(function(name) { + return Promise.resolve() + .then(function() { + var load = tree[name]; + + if (load === true) + throw new TypeError(name + ' was defined via a bundle, so can only be used for subtraction or union operations.'); + + // store plugin loads for bundle hook + if (load.metadata.loader) { + var pluginLoad = extend({}, load); + pluginLoad.address = loader.baseURL + load.path; + (pluginLoads[load.metadata.loader] = pluginLoads[load.metadata.loader] || []).push(pluginLoad); + } + + return compileLoad(loader, tree[name], compileOpts, cache); + }); + })); + }) .then(function(compiled) { outputs = outputs.concat(compiled); })