1
- // tslint:disable
2
- // TODO: cleanup this file, it's copied as is from Angular CLI.
3
-
4
1
/**
5
2
* @license
6
3
* Copyright Google Inc. All Rights Reserved.
7
4
*
8
5
* Use of this source code is governed by an MIT-style license that can be
9
6
* found in the LICENSE file at https://angular.io/license
10
7
*/
11
-
8
+ import { createHash } from 'crypto' ;
9
+ import { Compiler , compilation } from 'webpack' ;
12
10
import { RawSource } from 'webpack-sources' ;
13
11
14
12
const parse5 = require ( 'parse5' ) ;
@@ -19,13 +17,15 @@ export interface IndexHtmlWebpackPluginOptions {
19
17
baseHref ?: string ;
20
18
entrypoints : string [ ] ;
21
19
deployUrl ?: string ;
20
+ sri : boolean ;
22
21
}
23
22
24
- function readFile ( filename : string , compilation : any ) : Promise < string > {
23
+ function readFile ( filename : string , compilation : compilation . Compilation ) : Promise < string > {
25
24
return new Promise < string > ( ( resolve , reject ) => {
26
25
compilation . inputFileSystem . readFile ( filename , ( err : Error , data : Buffer ) => {
27
26
if ( err ) {
28
27
reject ( err ) ;
28
+
29
29
return ;
30
30
}
31
31
@@ -53,23 +53,25 @@ export class IndexHtmlWebpackPlugin {
53
53
input : 'index.html' ,
54
54
output : 'index.html' ,
55
55
entrypoints : [ 'polyfills' , 'main' ] ,
56
- ...options
56
+ sri : false ,
57
+ ...options ,
57
58
} ;
58
59
}
59
60
60
- apply ( compiler : any ) {
61
- compiler . hooks . emit . tapPromise ( 'index-html-webpack-plugin' , async ( compilation : any ) => {
61
+ apply ( compiler : Compiler ) {
62
+ compiler . hooks . emit . tapPromise ( 'index-html-webpack-plugin' , async compilation => {
62
63
// Get input html file
63
64
const inputContent = await readFile ( this . _options . input , compilation ) ;
64
- compilation . fileDependencies . add ( this . _options . input ) ;
65
+ ( compilation as compilation . Compilation & { fileDependencies : Set < string > } )
66
+ . fileDependencies . add ( this . _options . input ) ;
65
67
66
68
67
69
// Get all files for selected entrypoints
68
- const unfilteredSortedFiles : string [ ] = [ ] ;
70
+ let unfilteredSortedFiles : string [ ] = [ ] ;
69
71
for ( const entryName of this . _options . entrypoints ) {
70
72
const entrypoint = compilation . entrypoints . get ( entryName ) ;
71
- if ( entrypoint ) {
72
- unfilteredSortedFiles . push ( ... entrypoint . getFiles ( ) ) ;
73
+ if ( entrypoint && entrypoint . getFiles ) {
74
+ unfilteredSortedFiles = unfilteredSortedFiles . concat ( entrypoint . getFiles ( ) || [ ] ) ;
73
75
}
74
76
}
75
77
@@ -116,13 +118,25 @@ export class IndexHtmlWebpackPlugin {
116
118
}
117
119
118
120
for ( const script of scripts ) {
121
+ const attrs = [
122
+ { name : 'type' , value : 'text/javascript' } ,
123
+ { name : 'src' , value : ( this . _options . deployUrl || '' ) + script } ,
124
+ ] ;
125
+ if ( this . _options . sri ) {
126
+ const algo = 'sha384' ;
127
+ const hash = createHash ( algo )
128
+ . update ( compilation . assets [ script ] . source ( ) , 'utf8' )
129
+ . digest ( 'base64' ) ;
130
+ attrs . push (
131
+ { name : 'integrity' , value : `${ algo } -${ hash } ` } ,
132
+ { name : 'crossorigin' , value : 'anonymous' } ,
133
+ ) ;
134
+ }
135
+
119
136
const element = treeAdapter . createElement (
120
137
'script' ,
121
138
undefined ,
122
- [
123
- { name : 'type' , value : 'text/javascript' } ,
124
- { name : 'src' , value : ( this . _options . deployUrl || '' ) + script } ,
125
- ]
139
+ attrs ,
126
140
) ;
127
141
treeAdapter . appendChild ( bodyElement , element ) ;
128
142
}
@@ -143,7 +157,7 @@ export class IndexHtmlWebpackPlugin {
143
157
undefined ,
144
158
[
145
159
{ name : 'href' , value : this . _options . baseHref } ,
146
- ]
160
+ ] ,
147
161
) ;
148
162
treeAdapter . appendChild ( headElement , element ) ;
149
163
} else {
@@ -168,7 +182,7 @@ export class IndexHtmlWebpackPlugin {
168
182
[
169
183
{ name : 'rel' , value : 'stylesheet' } ,
170
184
{ name : 'href' , value : ( this . _options . deployUrl || '' ) + stylesheet } ,
171
- ]
185
+ ] ,
172
186
) ;
173
187
treeAdapter . appendChild ( headElement , element ) ;
174
188
}
0 commit comments