1'use strict' 2var createNode = require('./node.js').create 3module.exports = function (tree) { 4 return copyTree(tree, {}) 5} 6 7function copyTree (tree, cache) { 8 if (cache[tree.path]) { return cache[tree.path] } 9 var newTree = cache[tree.path] = createNode(Object.assign({}, tree)) 10 copyModuleList(newTree, 'children', cache) 11 newTree.children.forEach(function (child) { 12 child.parent = newTree 13 }) 14 copyModuleList(newTree, 'requires', cache) 15 copyModuleList(newTree, 'requiredBy', cache) 16 return newTree 17} 18 19function copyModuleList (tree, key, cache) { 20 var newList = [] 21 if (tree[key]) { 22 tree[key].forEach(function (child) { 23 const copy = copyTree(child, cache) 24 if (copy) { 25 newList.push(copy) 26 } 27 }) 28 } 29 tree[key] = newList 30} 31