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,18 +28,20 @@ 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
40- const initialFiles = Object . keys ( entrypoints )
41- . map ( ( name ) => entrypoints [ name ] . assets . map ( ( item ) => item . name ) )
42- . reduce ( ( assets , all ) => all . concat ( assets ) , [ ] )
43- . filter ( ( file ) => isJS ( file ) || isCSS ( file ) )
37+ const initialFiles =
38+ Object . keys ( entrypoints )
39+ . map (
40+ ( name ) =>
41+ entrypoints [ name ] . assets ?. map ( ( item ) => item . name ) ?? [ ] ,
42+ )
43+ . reduce ( ( assets , all ) => all . concat ( assets ) , [ ] )
44+ . filter ( ( file ) => isJS ( file ) || isCSS ( file ) ) ?? [ ]
4445
4546 // get files that should be loaded asynchronously
4647 // i.e. script and style files that are not included in the initial entry files
@@ -51,18 +52,19 @@ export const createClientPlugin = (
5152
5253 // get asset modules
5354 const assetModules = modules . filter (
54- ( m ) : m is FnModules & Required < Pick < FnModules , 'assets' > > =>
55- ! ! ( m . assets && m . assets . length ) ,
55+ ( m ) : m is StatsModule & Required < Pick < StatsModule , 'assets' > > =>
56+ Boolean ( m . assets ? .length ) ,
5657 )
5758
5859 // get modules for client manifest
5960 const manifestModules : ClientManifest [ 'modules' ] = { }
6061
61- const fileToIndex = ( file : string ) : number => allFiles . indexOf ( file )
62+ const fileToIndex = ( file : number | string ) : number =>
63+ allFiles . indexOf ( file . toString ( ) )
6264
6365 modules . forEach ( ( m ) => {
6466 // ignore modules duplicated in multiple chunks
65- if ( m . chunks . length !== 1 ) {
67+ if ( m . chunks ? .length !== 1 ) {
6668 return
6769 }
6870
@@ -75,21 +77,21 @@ export const createClientPlugin = (
7577
7678 // remove appended hash of module identifier
7779 // which is the request string of the module
78- const request = m . identifier . replace ( / \| \w + $ / , '' )
80+ const request = m . identifier ? .replace ( / \| \w + $ / , '' )
7981
8082 // get chunk files index
8183 const files = [ ...chunk . files . map ( fileToIndex ) ]
8284
8385 // find all asset modules associated with the same chunk
8486 assetModules . forEach ( ( m ) => {
85- if ( m . chunks . some ( ( id ) => id === cid ) ) {
87+ if ( m . chunks ? .some ( ( id ) => id === cid ) ) {
8688 // get asset files
8789 files . push ( ...m . assets . map ( fileToIndex ) )
8890 }
8991 } )
9092
9193 // map the module request to files index
92- manifestModules [ request ] = files
94+ if ( request ) manifestModules [ request ] = files
9395 } )
9496
9597 // generate client manifest json file
0 commit comments