import { ModuleGraph , ModuleGraphNode } from '../../../types/BundleData' ; import { Stats , Module } from '../../../types/Stats' ; import { arrayUnion } from '../../../util/arrayUnion' ; import ModuleIdToNameMap from './ModuleIdToNameMap' ; import NamedChunkGroupLookupMap from '../NamedChunkGroupLookupMap' ; import { validateGraph } from './validateGraph' ; import { processReasons } from './processReasons' ; export function deriveGraph ( stats , validate ? ) { const moduleIdToNameMap = new ModuleIdToNameMap ( stats ) ; const ncgLookup = new NamedChunkGroupLookupMap ( stats ) ; let graph = { } ; for ( let module of stats . modules ) { processModule ( module , graph , moduleIdToNameMap , ncgLookup ) ; } if ( validate ) { validateGraph ( graph ) ; } return graph ; } export function processModule ( module , graph , moduleIdToNameMap , ncgLookup ) { if ( module . identifier . startsWith ( 'ignored ' ) ) { return ; } const namedChunkGroups = ncgLookup . getNamedChunkGroups ( module . chunks ) ; if ( ! module . modules ) { addModuleToGraph ( graph , { name : module . name , namedChunkGroups , size : module . size , ... processReasons ( module . reasons , moduleIdToNameMap ) , } ) ; } else { const primaryModule = module . modules [ 0 ] ; addModuleToGraph ( graph , { name : primaryModule . name , containsHoistedModules : true , namedChunkGroups , size : primaryModule . size , ... processReasons ( module . reasons , moduleIdToNameMap ) , } ) ; for ( let i = 1 ; i < module . modules . length ; i ++ ) { const hoistedModule = module . modules [ i ] ; addModuleToGraph ( graph , { name : hoistedModule . name , parents : [ primaryModule . name ] , directParents : [ primaryModule . name ] , lazyParents : [ ] , namedChunkGroups , size : hoistedModule . size , } ) ; } } } function addModuleToGraph ( graph , moduleNode ) { if ( graph [ moduleNode . name ] ) { const graphNode = graph [ moduleNode . name ] ; graphNode . parents = arrayUnion ( graphNode . parents , moduleNode . parents ) ; graphNode . namedChunkGroups = arrayUnion ( graphNode . namedChunkGroups , moduleNode . namedChunkGroups ) ; } else { graph [ moduleNode . name ] = moduleNode ; } }