@@ -65,17 +65,8 @@ export interface FileInfo {
6565export async function augmentIndexHtml (
6666 params : AugmentIndexHtmlOptions ,
6767) : Promise < { content : string ; warnings : string [ ] ; errors : string [ ] } > {
68- const {
69- loadOutputFile,
70- files,
71- entrypoints,
72- sri,
73- deployUrl = '' ,
74- lang,
75- baseHref,
76- html,
77- imageDomains,
78- } = params ;
68+ const { loadOutputFile, files, entrypoints, sri, deployUrl, lang, baseHref, html, imageDomains } =
69+ params ;
7970
8071 const warnings : string [ ] = [ ] ;
8172 const errors : string [ ] = [ ] ;
@@ -117,7 +108,7 @@ export async function augmentIndexHtml(
117108
118109 let scriptTags : string [ ] = [ ] ;
119110 for ( const [ src , isModule ] of scripts ) {
120- const attrs = [ `src="${ deployUrl } ${ src } "` ] ;
111+ const attrs = [ `src="${ generateUrl ( src , deployUrl ) } "` ] ;
121112
122113 // This is also need for non entry-points as they may contain problematic code.
123114 if ( isModule ) {
@@ -141,7 +132,7 @@ export async function augmentIndexHtml(
141132 let headerLinkTags : string [ ] = [ ] ;
142133 let bodyLinkTags : string [ ] = [ ] ;
143134 for ( const src of stylesheets ) {
144- const attrs = [ `rel="stylesheet"` , `href="${ deployUrl } ${ src } "` ] ;
135+ const attrs = [ `rel="stylesheet"` , `href="${ generateUrl ( src , deployUrl ) } "` ] ;
145136
146137 if ( crossOrigin !== 'none' ) {
147138 attrs . push ( `crossorigin="${ crossOrigin } "` ) ;
@@ -157,7 +148,7 @@ export async function augmentIndexHtml(
157148
158149 if ( params . hints ?. length ) {
159150 for ( const hint of params . hints ) {
160- const attrs = [ `rel="${ hint . mode } "` , `href="${ deployUrl } ${ hint . url } "` ] ;
151+ const attrs = [ `rel="${ hint . mode } "` , `href="${ generateUrl ( hint . url , deployUrl ) } "` ] ;
161152
162153 if ( hint . mode !== 'modulepreload' && crossOrigin !== 'none' ) {
163154 // Value is considered anonymous by the browser when not present or empty
@@ -303,6 +294,19 @@ function generateSriAttributes(content: string): string {
303294 return `integrity="${ algo } -${ hash } "` ;
304295}
305296
297+ function generateUrl ( value : string , deployUrl : string | undefined ) : string {
298+ if ( ! deployUrl ) {
299+ return value ;
300+ }
301+
302+ // Skip if root-relative, absolute or protocol relative url
303+ if ( / ^ ( (?: \w + : ) ? \/ \/ | d a t a : | c h r o m e : | \/ ) / . test ( value ) ) {
304+ return value ;
305+ }
306+
307+ return `${ deployUrl } ${ value } ` ;
308+ }
309+
306310function updateAttribute (
307311 tag : { attrs : { name : string ; value : string } [ ] } ,
308312 name : string ,
0 commit comments