import isObject from 'lodash/isObject' ; import isString from 'lodash/isString' ; import isArray from 'lodash/isArray' ; import each from 'lodash/each' ; import uniq from 'lodash/uniq' ; import map from 'lodash/map' ; import compact from 'lodash/compact' ; import * as webpack from 'webpack' ; type DependencyMap = { path : string ; modules : string [ ] ; } ; export interface INormalizeAssets { client ? : string | string [ ] ; 'vendor~client' ? : string | string [ ] ; cssDependencyMap ? : DependencyMap [ ] ; jsDependencyMap ? : DependencyMap [ ] ; } const populate = ( chunks , type , publicPath ) => { const arr = [ ] ; each ( chunks , ( chunk ) => { let hasType = false ; let typeFileName = '' ; each ( chunk . files , ( f ) => { if ( hasType ) return ; hasType = f . indexOf ( type ) !== - 1 ; if ( hasType ) typeFileName = f ; } ) ; if ( ! hasType ) return ; let moduleReasons = [ ] ; each ( chunk . modules , ( m ) => { moduleReasons = moduleReasons . concat ( map ( m . reasons , 'userRequest' ) ) ; } ) ; each ( chunk . origins , ( m ) => { moduleReasons = moduleReasons . concat ( m . request ) ; } ) ; if ( Array . isArray ( chunk . names ) && ( chunk . names . indexOf ( 'client' ) !== - 1 || chunk . names . indexOf ( 'vendors~client' ) !== - 1 ) ) { moduleReasons . unshift ( 'pawProjectClient' ) ; } moduleReasons = uniq ( compact ( moduleReasons ) ) ; if ( chunk . names . indexOf ( 'vendors~client' ) === - 1 ) { arr . push ( { path : `${ publicPath }${ typeFileName }` , modules : moduleReasons , } ) ; } else { arr . unshift ( { path : `${ publicPath }${ typeFileName }` , modules : moduleReasons , } ) ; } } ) ; return arr ; } ; export default ( wStats ) : INormalizeAssets => { if ( ! wStats ) { return { } ; } let assets = { } ; let cssDependencyMap = [ ] ; let jsDependencyMap = [ ] ; let webpackStats = wStats . toJson ( ) ; if ( ( typeof webpackStats . assets === 'undefined' || ! webpackStats . assets . length ) && webpackStats . children && webpackStats . children . length === 1 ) { webpackStats = webpackStats . children ; } if ( ! isArray ( webpackStats ) ) { webpackStats = [ webpackStats ] ; } webpackStats . forEach ( ( stat ) => { const { assetsByChunkName : a , publicPath } = stat ; each ( a , ( chunkValue , chunkName ) => { if ( isArray ( chunkValue ) ) { each ( chunkValue , ( path , index ) => { a [ chunkName ] [ index ] = `${ publicPath }${ path }` ; } ) ; } else if ( isObject ( chunkValue ) ) { each ( chunkValue , ( subChunkValues , subChunkType ) => { if ( isArray ( subChunkValues ) || isObject ( subChunkValues ) ) { each ( subChunkValues , ( subChunkValue , subChunkIndex ) => { a [ chunkName ] [ subChunkType ] [ subChunkIndex ] = `${ publicPath }${ subChunkValue }` ; } ) ; } else if ( isString ( subChunkValues ) ) { a [ chunkName ] [ subChunkType ] = `${ publicPath }${ subChunkValues }` ; } } ) ; } else if ( isString ( chunkValue ) ) { a [ chunkName ] = `${ publicPath }${ chunkValue }` ; } } ) ; if ( stat . chunks ) { cssDependencyMap = populate ( stat . chunks , '.css' , publicPath || '' ) ; jsDependencyMap = populate ( stat . chunks , '.js' , publicPath || '' ) ; } assets = { ... a , cssDependencyMap , jsDependencyMap } ; } ) ; return assets ; } ;