11import { fs } from '@vuepress/utils'
2- import type { WebpackPluginInstance } from 'webpack'
3- import type { FnModules , StatsToJsonOutput } from '../../types.webpack.js'
2+ import type { StatsModule , WebpackPluginInstance } from 'webpack'
43import { isCSS , isJS } from './utils.js'
54
65export interface ClientManifest {
@@ -29,17 +28,17 @@ export const createClientPlugin = (
2928 modules = [ ] ,
3029 entrypoints = { } ,
3130 chunks = [ ] ,
32- } : StatsToJsonOutput = compilation
33- . getStats ( )
34- . toJson ( ) as unknown as StatsToJsonOutput
31+ } = compilation . getStats ( ) . toJson ( )
3532
3633 // get all files
3734 const allFiles = assets . map ( ( a ) => a . name )
3835
3936 // get initial entry files
4037 const initialFiles = Object . keys ( entrypoints )
41- . map ( ( name ) => entrypoints [ name ] . assets . map ( ( item ) => item . name ) )
42- . reduce ( ( assets , all ) => all . concat ( assets ) , [ ] )
38+ . flatMap (
39+ ( name ) =>
40+ entrypoints [ name ] . assets ?. map ( ( item ) => item . name ) ?? [ ] ,
41+ )
4342 . filter ( ( file ) => isJS ( file ) || isCSS ( file ) )
4443
4544 // get files that should be loaded asynchronously
@@ -51,18 +50,19 @@ export const createClientPlugin = (
5150
5251 // get asset modules
5352 const assetModules = modules . filter (
54- ( m ) : m is FnModules & Required < Pick < FnModules , 'assets' > > =>
55- ! ! ( m . assets && m . assets . length ) ,
53+ ( m ) : m is StatsModule & Required < Pick < StatsModule , 'assets' > > =>
54+ Boolean ( m . assets ? .length ) ,
5655 )
5756
5857 // get modules for client manifest
5958 const manifestModules : ClientManifest [ 'modules' ] = { }
6059
61- const fileToIndex = ( file : string ) : number => allFiles . indexOf ( file )
60+ const fileToIndex = ( file : number | string ) : number =>
61+ allFiles . indexOf ( file . toString ( ) )
6262
6363 modules . forEach ( ( m ) => {
6464 // ignore modules duplicated in multiple chunks
65- if ( m . chunks . length !== 1 ) {
65+ if ( m . chunks ? .length !== 1 ) {
6666 return
6767 }
6868
@@ -75,21 +75,21 @@ export const createClientPlugin = (
7575
7676 // remove appended hash of module identifier
7777 // which is the request string of the module
78- const request = m . identifier . replace ( / \| \w + $ / , '' )
78+ const request = m . identifier ? .replace ( / \| \w + $ / , '' )
7979
8080 // get chunk files index
8181 const files = [ ...chunk . files . map ( fileToIndex ) ]
8282
8383 // find all asset modules associated with the same chunk
8484 assetModules . forEach ( ( m ) => {
85- if ( m . chunks . some ( ( id ) => id === cid ) ) {
85+ if ( m . chunks ? .some ( ( id ) => id === cid ) ) {
8686 // get asset files
8787 files . push ( ...m . assets . map ( fileToIndex ) )
8888 }
8989 } )
9090
9191 // map the module request to files index
92- manifestModules [ request ] = files
92+ if ( request ) manifestModules [ request ] = files
9393 } )
9494
9595 // generate client manifest json file
0 commit comments