build(DiffingPluginWrapper): ignore null/undefined input trees

this is handy to conditionally create build graph but keep mergeTree() declarative - any input tree passed into
mergeTree that is null or undefined will simply be ignored
This commit is contained in:
Igor Minar 2015-11-13 02:00:32 -08:00
parent 2ce9e95fc1
commit cf0183ac7f
1 changed files with 13 additions and 4 deletions

View File

@ -20,7 +20,7 @@ export {DiffResult} from './tree-differ';
* an instance of BroccoliTree. * an instance of BroccoliTree.
* *
* @param pluginClass * @param pluginClass
* @returns {DiffingPlugin} * @returns {DiffingBroccoliPlugin}
*/ */
export function wrapDiffingPlugin(pluginClass): DiffingPluginWrapperFactory { export function wrapDiffingPlugin(pluginClass): DiffingPluginWrapperFactory {
return function() { return new DiffingPluginWrapper(pluginClass, arguments); }; return function() { return new DiffingPluginWrapper(pluginClass, arguments); };
@ -160,10 +160,19 @@ class DiffingPluginWrapper implements BroccoliTree {
private stabilizeTrees(trees: BroccoliTree[]) { private stabilizeTrees(trees: BroccoliTree[]) {
// Prevent extensions to prevent array from being mutated from the outside. // Prevent extensions to prevent array from being mutated from the outside.
// For-loop used to avoid re-allocating a new array. // For-loop used to avoid re-allocating a new array.
var stableTrees = [];
for (let i = 0; i < trees.length; ++i) { for (let i = 0; i < trees.length; ++i) {
trees[i] = this.stabilizeTree(trees[i]); // ignore null/undefined input tries in order to support conditional build pipelines
if (trees[i]) {
stableTrees.push(this.stabilizeTree(trees[i]));
}
} }
return Object.freeze(trees);
if (stableTrees.length === 0) {
throw new Error('No input trees provided!');
}
return Object.freeze(stableTrees);
} }
@ -172,7 +181,7 @@ class DiffingPluginWrapper implements BroccoliTree {
// so we need to stabilize them. // so we need to stabilize them.
// Since it's not safe to use instanceof operator in node, we are checking the constructor.name. // Since it's not safe to use instanceof operator in node, we are checking the constructor.name.
// //
// New-styler/rebuild trees should always be stable. // New-style/rebuild trees should always be stable.
let isNewStyleTree = !!(tree['newStyleTree'] || typeof tree.rebuild === 'function' || let isNewStyleTree = !!(tree['newStyleTree'] || typeof tree.rebuild === 'function' ||
tree['isReadAPICompatTree'] || tree.constructor['name'] === 'Funnel'); tree['isReadAPICompatTree'] || tree.constructor['name'] === 'Funnel');