@@ -16,32 +16,25 @@ const parse5 = require('parse5');
16
16
* after processing several configurations in order to build different sets of
17
17
* bundles for differential serving.
18
18
*/
19
- function generateIndexHtml ( params ) {
20
- const loadOutputFile = params . loadOutputFile ;
21
- // Filter files
22
- const existingFiles = new Set ( ) ;
23
- const stylesheets = [ ] ;
24
- const scripts = [ ] ;
25
- const fileNames = params . unfilteredSortedFiles . map ( f => f . file ) ;
26
- const moduleFilesArray = params . unfilteredSortedFiles
27
- . filter ( f => f . type === 'module' )
28
- . map ( f => f . file ) ;
29
- const moduleFiles = new Set ( moduleFilesArray ) ;
30
- const noModuleFilesArray = params . unfilteredSortedFiles
31
- . filter ( f => f . type === 'nomodule' )
32
- . map ( f => f . file ) ;
33
- noModuleFilesArray . push ( ...params . noModuleFiles ) ;
34
- const noModuleFiles = new Set ( noModuleFilesArray ) ;
35
- for ( const file of fileNames ) {
36
- if ( existingFiles . has ( file ) ) {
37
- continue ;
38
- }
39
- existingFiles . add ( file ) ;
40
- if ( file . endsWith ( '.js' ) ) {
41
- scripts . push ( file ) ;
42
- }
43
- else if ( file . endsWith ( '.css' ) ) {
44
- stylesheets . push ( file ) ;
19
+ async function generateIndexHtml ( params ) {
20
+ const { loadOutputFile, files, noModuleFiles = [ ] , moduleFiles = [ ] , entrypoints, } = params ;
21
+ const stylesheets = new Set ( ) ;
22
+ const scripts = new Set ( ) ;
23
+ // Sort files in the order we want to insert them by entrypoint and dedupes duplicates
24
+ const mergedFiles = [ ...noModuleFiles , ...moduleFiles , ...files ] ;
25
+ for ( const entrypoint of entrypoints ) {
26
+ for ( const { extension, fileName, name } of mergedFiles ) {
27
+ if ( name !== entrypoint ) {
28
+ continue ;
29
+ }
30
+ switch ( extension ) {
31
+ case '.js' :
32
+ scripts . add ( fileName ) ;
33
+ break ;
34
+ case '.css' :
35
+ stylesheets . add ( fileName ) ;
36
+ break ;
37
+ }
45
38
}
46
39
}
47
40
// Find the head and body elements
@@ -55,7 +48,7 @@ function generateIndexHtml(params) {
55
48
if ( htmlChild . tagName === 'head' ) {
56
49
headElement = htmlChild ;
57
50
}
58
- if ( htmlChild . tagName === 'body' ) {
51
+ else if ( htmlChild . tagName === 'body' ) {
59
52
bodyElement = htmlChild ;
60
53
}
61
54
}
@@ -90,14 +83,19 @@ function generateIndexHtml(params) {
90
83
const attrs = [
91
84
{ name : 'src' , value : ( params . deployUrl || '' ) + script } ,
92
85
] ;
93
- if ( noModuleFiles . has ( script ) ) {
94
- attrs . push ( { name : 'nomodule' , value : null } ) ;
95
- }
96
- if ( moduleFiles . has ( script ) ) {
97
- attrs . push ( { name : 'type' , value : 'module' } ) ;
86
+ // We want to include nomodule or module when a file is not common amongs all
87
+ // such as runtime.js
88
+ const scriptPredictor = ( { fileName } ) => fileName === script ;
89
+ if ( ! files . some ( scriptPredictor ) ) {
90
+ if ( noModuleFiles . some ( scriptPredictor ) ) {
91
+ attrs . push ( { name : 'nomodule' , value : null } ) ;
92
+ }
93
+ else if ( moduleFiles . some ( scriptPredictor ) ) {
94
+ attrs . push ( { name : 'type' , value : 'module' } ) ;
95
+ }
98
96
}
99
97
if ( params . sri ) {
100
- const content = loadOutputFile ( script ) ;
98
+ const content = await loadOutputFile ( script ) ;
101
99
attrs . push ( ..._generateSriAttributes ( content ) ) ;
102
100
}
103
101
const attributes = attrs
@@ -146,7 +144,7 @@ function generateIndexHtml(params) {
146
144
{ name : 'href' , value : ( params . deployUrl || '' ) + stylesheet } ,
147
145
] ;
148
146
if ( params . sri ) {
149
- const content = loadOutputFile ( stylesheet ) ;
147
+ const content = await loadOutputFile ( stylesheet ) ;
150
148
attrs . push ( ..._generateSriAttributes ( content ) ) ;
151
149
}
152
150
const element = treeAdapter . createElement ( 'link' , undefined , attrs ) ;
0 commit comments