@@ -11,9 +11,10 @@ import { RawSource, ReplaceSource } from 'webpack-sources';
1111
1212const parse5 = require ( 'parse5' ) ;
1313
14-
1514export type LoadOutputFileFunctionType = ( file : string ) => Promise < string > ;
1615
16+ export type CrossOriginValue = 'none' | 'anonymous' | 'use-credentials' ;
17+
1718export interface AugmentIndexHtmlOptions {
1819 /* Input file name (e. g. index.html) */
1920 input : string ;
@@ -22,6 +23,8 @@ export interface AugmentIndexHtmlOptions {
2223 baseHref ?: string ;
2324 deployUrl ?: string ;
2425 sri : boolean ;
26+ /** crossorigin attribute setting of elements that provide CORS support */
27+ crossOrigin ?: CrossOriginValue ;
2528 /*
2629 * Files emitted by the build.
2730 * Js files will be added without 'nomodule' nor 'module'.
@@ -54,13 +57,12 @@ export interface FileInfo {
5457 * bundles for differential serving.
5558 */
5659export async function augmentIndexHtml ( params : AugmentIndexHtmlOptions ) : Promise < string > {
57- const {
58- loadOutputFile,
59- files,
60- noModuleFiles = [ ] ,
61- moduleFiles = [ ] ,
62- entrypoints,
63- } = params ;
60+ const { loadOutputFile, files, noModuleFiles = [ ] , moduleFiles = [ ] , entrypoints } = params ;
61+
62+ let { crossOrigin = 'none' } = params ;
63+ if ( params . sri && crossOrigin === 'none' ) {
64+ crossOrigin = 'anonymous' ;
65+ }
6466
6567 const stylesheets = new Set < string > ( ) ;
6668 const scripts = new Set < string > ( ) ;
@@ -69,7 +71,9 @@ export async function augmentIndexHtml(params: AugmentIndexHtmlOptions): Promise
6971 const mergedFiles = [ ...moduleFiles , ...noModuleFiles , ...files ] ;
7072 for ( const entrypoint of entrypoints ) {
7173 for ( const { extension, file, name } of mergedFiles ) {
72- if ( name !== entrypoint ) { continue ; }
74+ if ( name !== entrypoint ) {
75+ continue ;
76+ }
7377
7478 switch ( extension ) {
7579 case '.js' :
@@ -127,10 +131,14 @@ export async function augmentIndexHtml(params: AugmentIndexHtmlOptions): Promise
127131
128132 let scriptElements = '' ;
129133 for ( const script of scripts ) {
130- const attrs : { name : string , value : string | null } [ ] = [
134+ const attrs : { name : string ; value : string | null } [ ] = [
131135 { name : 'src' , value : ( params . deployUrl || '' ) + script } ,
132136 ] ;
133137
138+ if ( crossOrigin !== 'none' ) {
139+ attrs . push ( { name : 'crossorigin' , value : crossOrigin } ) ;
140+ }
141+
134142 // We want to include nomodule or module when a file is not common amongs all
135143 // such as runtime.js
136144 const scriptPredictor = ( { file } : FileInfo ) : boolean => file === script ;
@@ -154,15 +162,12 @@ export async function augmentIndexHtml(params: AugmentIndexHtmlOptions): Promise
154162 }
155163
156164 const attributes = attrs
157- . map ( attr => attr . value === null ? attr . name : `${ attr . name } ="${ attr . value } "` )
165+ . map ( attr => ( attr . value === null ? attr . name : `${ attr . name } ="${ attr . value } "` ) )
158166 . join ( ' ' ) ;
159167 scriptElements += `<script ${ attributes } ></script>` ;
160168 }
161169
162- indexSource . insert (
163- scriptInsertionPoint ,
164- scriptElements ,
165- ) ;
170+ indexSource . insert ( scriptInsertionPoint , scriptElements ) ;
166171
167172 // Adjust base href if specified
168173 if ( typeof params . baseHref == 'string' ) {
@@ -176,13 +181,9 @@ export async function augmentIndexHtml(params: AugmentIndexHtmlOptions): Promise
176181 const baseFragment = treeAdapter . createDocumentFragment ( ) ;
177182
178183 if ( ! baseElement ) {
179- baseElement = treeAdapter . createElement (
180- 'base' ,
181- undefined ,
182- [
183- { name : 'href' , value : params . baseHref } ,
184- ] ,
185- ) ;
184+ baseElement = treeAdapter . createElement ( 'base' , undefined , [
185+ { name : 'href' , value : params . baseHref } ,
186+ ] ) ;
186187
187188 treeAdapter . appendChild ( baseFragment , baseElement ) ;
188189 indexSource . insert (
@@ -218,6 +219,10 @@ export async function augmentIndexHtml(params: AugmentIndexHtmlOptions): Promise
218219 { name : 'href' , value : ( params . deployUrl || '' ) + stylesheet } ,
219220 ] ;
220221
222+ if ( crossOrigin !== 'none' ) {
223+ attrs . push ( { name : 'crossorigin' , value : crossOrigin } ) ;
224+ }
225+
221226 if ( params . sri ) {
222227 const content = await loadOutputFile ( stylesheet ) ;
223228 attrs . push ( ..._generateSriAttributes ( content ) ) ;
@@ -227,10 +232,7 @@ export async function augmentIndexHtml(params: AugmentIndexHtmlOptions): Promise
227232 treeAdapter . appendChild ( styleElements , element ) ;
228233 }
229234
230- indexSource . insert (
231- styleInsertionPoint ,
232- parse5 . serialize ( styleElements , { treeAdapter } ) ,
233- ) ;
235+ indexSource . insert ( styleInsertionPoint , parse5 . serialize ( styleElements , { treeAdapter } ) ) ;
234236
235237 return indexSource . source ( ) ;
236238}
@@ -241,8 +243,5 @@ function _generateSriAttributes(content: string) {
241243 . update ( content , 'utf8' )
242244 . digest ( 'base64' ) ;
243245
244- return [
245- { name : 'integrity' , value : `${ algo } -${ hash } ` } ,
246- { name : 'crossorigin' , value : 'anonymous' } ,
247- ] ;
246+ return [ { name : 'integrity' , value : `${ algo } -${ hash } ` } ] ;
248247}
0 commit comments