@@ -30,6 +30,13 @@ export type ClientManifest = {
30
30
}
31
31
} ;
32
32
33
+ type PreloadFile = {
34
+ file : string ;
35
+ extension : string ;
36
+ fileWithoutQuery: string ;
37
+ asType: string ;
38
+ } ;
39
+
33
40
export default class TemplateRenderer {
34
41
options : TemplateRendererOptions ;
35
42
inject : boolean ;
@@ -118,10 +125,19 @@ export default class TemplateRenderer {
118
125
return this . renderPreloadLinks ( context ) + this . renderPrefetchLinks ( context )
119
126
}
120
127
121
- getPreloadFiles ( context : Object ) {
128
+ getPreloadFiles ( context : Object ) : Array < PreloadFile > {
122
129
const usedAsyncFiles = this . getUsedAsyncFiles ( context )
123
130
if ( this . preloadFiles || usedAsyncFiles ) {
124
- return ( this . preloadFiles || [ ] ) . concat ( usedAsyncFiles || [ ] )
131
+ return ( this . preloadFiles || [ ] ) . concat ( usedAsyncFiles || [ ] ) . map ( file => {
132
+ const withoutQuery = file . replace ( / \? .* / , '' )
133
+ const extension = path . extname ( withoutQuery ) . slice ( 1 )
134
+ return {
135
+ file,
136
+ extension,
137
+ fileWithoutQuery : withoutQuery ,
138
+ asType : getPreloadType ( extension )
139
+ }
140
+ } )
125
141
} else {
126
142
return [ ]
127
143
}
@@ -130,27 +146,24 @@ export default class TemplateRenderer {
130
146
renderPreloadLinks ( context : Object ) : string {
131
147
const files = this . getPreloadFiles ( context )
132
148
if ( files . length ) {
133
- return files . map ( file => {
149
+ return files . map ( ( { file, extension , fileWithoutQuery , asType } ) => {
134
150
let extra = ''
135
- const withoutQuery = file . replace ( / \? .* / , '' )
136
- const ext = path . extname ( withoutQuery ) . slice ( 1 )
137
- const type = getPreloadType ( ext )
138
151
const shouldPreload = this . options . shouldPreload
139
152
// by default, we only preload scripts or css
140
- if ( ! shouldPreload && type !== 'script' && type !== 'style' ) {
153
+ if ( ! shouldPreload && asType !== 'script' && asType !== 'style' ) {
141
154
return ''
142
155
}
143
156
// user wants to explicitly control what to preload
144
- if ( shouldPreload && ! shouldPreload ( withoutQuery , type ) ) {
157
+ if ( shouldPreload && ! shouldPreload ( fileWithoutQuery , asType ) ) {
145
158
return ''
146
159
}
147
- if ( type === 'font' ) {
148
- extra = ` type="font/${ ext } " crossorigin`
160
+ if ( asType === 'font' ) {
161
+ extra = ` type="font/${ extension } " crossorigin`
149
162
}
150
163
return `<link rel="preload" href="${
151
164
this . publicPath } /${ file
152
165
} "${
153
- type !== '' ? ` as="${ type } "` : ''
166
+ asType !== '' ? ` as="${ asType } "` : ''
154
167
} ${
155
168
extra
156
169
} >`
0 commit comments