diff --git a/lib/Compilation.js b/lib/Compilation.js index 6fcbf42e00f..c3e34ba70ca 100644 --- a/lib/Compilation.js +++ b/lib/Compilation.js @@ -1007,45 +1007,44 @@ class Compilation extends Tapable { } assignDepth(module) { - const assignDepthToModule = (module, depth) => { - // enter module - if(typeof module.depth === "number" && module.depth <= depth) return; - module.depth = depth; + const queue = new Set([module]); + let depth; - // enter it as block - assignDepthToDependencyBlock(module, depth + 1); + module.depth = 0; + + const enqueueJob = module => { + const d = module.depth; + if(typeof d === "number" && d <= depth) return; + queue.add(module); + module.depth = depth; }; const assignDepthToDependency = (dependency, depth) => { if(dependency.module) { - queue.push(() => assignDepthToModule(dependency.module, depth)); + enqueueJob(dependency.module); } }; - const assignDepthToDependencyBlock = (block, depth) => { - const iteratorDependency = d => assignDepthToDependency(d, depth); - - const iteratorBlock = b => assignDepthToDependencyBlock(b, depth); - + const assignDepthToDependencyBlock = block => { if(block.variables) { - iterationBlockVariable(block.variables, iteratorDependency); + iterationBlockVariable(block.variables, assignDepthToDependency); } if(block.dependencies) { - iterationOfArrayCallback(block.dependencies, iteratorDependency); + iterationOfArrayCallback(block.dependencies, assignDepthToDependency); } if(block.blocks) { - iterationOfArrayCallback(block.blocks, iteratorBlock); + iterationOfArrayCallback(block.blocks, assignDepthToDependencyBlock); } }; - const queue = [() => { - assignDepthToModule(module, 0); - }]; + for(module of queue) { + queue.delete(module); + depth = module.depth; - while(queue.length) { - queue.pop()(); + depth++; + assignDepthToDependencyBlock(module); } }