@@ -12,22 +12,23 @@ function makePlaceholder(id) {
12
12
return '*-*-*-CHUNK-SRI-HASH-' + id + '-*-*-*' ;
13
13
}
14
14
15
- function findDepChunks ( chunk , allDepChunkIds ) {
16
- chunk . chunks . forEach ( function forEachChunk ( depChunk ) {
17
- if ( ! allDepChunkIds [ depChunk . id ] ) {
18
- allDepChunkIds [ depChunk . id ] = true ;
19
- findDepChunks ( depChunk , allDepChunkIds ) ;
20
- }
21
- } ) ;
22
- }
23
-
24
15
function WebIntegrityJsonpMainTemplatePlugin ( sriPlugin , compilation ) {
25
16
this . sriPlugin = sriPlugin ;
26
17
this . compilation = compilation ;
27
18
}
28
19
29
20
WebIntegrityJsonpMainTemplatePlugin . prototype . apply = function apply ( mainTemplate ) {
30
21
var self = this ;
22
+ var allDepChunkIds = { } ;
23
+
24
+ function findDepChunks ( chunk ) {
25
+ chunk . chunks . forEach ( function forEachChunk ( depChunk ) {
26
+ if ( ! allDepChunkIds [ depChunk . id ] ) {
27
+ allDepChunkIds [ depChunk . id ] = true ;
28
+ findDepChunks ( depChunk ) ;
29
+ }
30
+ } ) ;
31
+ }
31
32
32
33
/*
33
34
* Patch jsonp-script code to add the integrity attribute.
@@ -53,8 +54,7 @@ WebIntegrityJsonpMainTemplatePlugin.prototype.apply = function apply(mainTemplat
53
54
*/
54
55
mainTemplate . plugin ( 'local-vars' , function localVarsPlugin ( source , chunk ) {
55
56
if ( chunk . chunks . length > 0 ) {
56
- var allDepChunkIds = { } ;
57
- findDepChunks ( chunk , allDepChunkIds ) ;
57
+ findDepChunks ( chunk ) ;
58
58
59
59
return this . asString ( [
60
60
source ,
@@ -95,11 +95,7 @@ function SubresourceIntegrityPlugin(options) {
95
95
enabled : true
96
96
} ;
97
97
98
- for ( var key in useOptions ) {
99
- if ( useOptions . hasOwnProperty ( key ) ) {
100
- this . options [ key ] = useOptions [ key ] ;
101
- }
102
- }
98
+ Object . assign ( this . options , useOptions ) ;
103
99
104
100
this . emittedWarnings = { } ;
105
101
}
@@ -120,6 +116,10 @@ SubresourceIntegrityPlugin.prototype.error = function error(compilation, message
120
116
} ;
121
117
122
118
SubresourceIntegrityPlugin . prototype . validateOptions = function validateOptions ( compilation ) {
119
+ var foundStandardHashFunc = false ;
120
+ var hashFuncName ;
121
+ var i ;
122
+
123
123
if ( this . optionsValidated ) {
124
124
return ;
125
125
}
@@ -144,9 +144,8 @@ SubresourceIntegrityPlugin.prototype.validateOptions = function validateOptions(
144
144
'instead got \'' + this . options . hashFuncNames + '\'.' ) ;
145
145
this . options . enabled = false ;
146
146
} else {
147
- var foundStandardHashFunc = false ;
148
- for ( var i = 0 ; i < this . options . hashFuncNames . length ; i ++ ) {
149
- var hashFuncName = this . options . hashFuncNames [ i ] ;
147
+ for ( i = 0 ; i < this . options . hashFuncNames . length ; i += 1 ) {
148
+ hashFuncName = this . options . hashFuncNames [ i ] ;
150
149
if ( typeof hashFuncName !== 'string' &&
151
150
! ( hashFuncName instanceof String ) ) {
152
151
this . error (
@@ -246,6 +245,14 @@ SubresourceIntegrityPlugin.prototype.apply = function apply(compiler) {
246
245
compilation . plugin ( 'after-optimize-assets' , function optimizeAssetsPlugin ( assets ) {
247
246
var hashByChunkId = { } ;
248
247
var visitedByChunkId = { } ;
248
+ var chunkFile ;
249
+ var oldSource ;
250
+ var magicMarker ;
251
+ var magicMarkerPos ;
252
+ var newAsset ;
253
+ var asset ;
254
+ var newSource ;
255
+
249
256
function processChunkRecursive ( chunk ) {
250
257
var depChunkIds = [ ] ;
251
258
@@ -259,9 +266,9 @@ SubresourceIntegrityPlugin.prototype.apply = function apply(compiler) {
259
266
} ) ;
260
267
261
268
if ( chunk . files . length > 0 ) {
262
- var chunkFile = chunk . files [ 0 ] ;
269
+ chunkFile = chunk . files [ 0 ] ;
263
270
264
- var oldSource = assets [ chunkFile ] . source ( ) ;
271
+ oldSource = assets [ chunkFile ] . source ( ) ;
265
272
266
273
if ( oldSource . indexOf ( 'webpackHotUpdate' ) >= 0 ) {
267
274
self . warnOnce (
@@ -271,42 +278,43 @@ SubresourceIntegrityPlugin.prototype.apply = function apply(compiler) {
271
278
) ;
272
279
}
273
280
274
- var newAsset = new ReplaceSource ( assets [ chunkFile ] ) ;
281
+ newAsset = new ReplaceSource ( assets [ chunkFile ] ) ;
275
282
276
283
depChunkIds . forEach ( function forEachChunk ( depChunkId ) {
277
- var magicMarker = makePlaceholder ( depChunkId ) ;
278
- var magicMarkerPos = oldSource . indexOf ( magicMarker ) ;
284
+ magicMarker = makePlaceholder ( depChunkId ) ;
285
+ magicMarkerPos = oldSource . indexOf ( magicMarker ) ;
279
286
if ( magicMarkerPos >= 0 ) {
280
287
newAsset . replace (
281
288
magicMarkerPos ,
282
- magicMarkerPos + magicMarker . length - 1 ,
289
+ ( magicMarkerPos + magicMarker . length ) - 1 ,
283
290
hashByChunkId [ depChunkId ] ) ;
284
291
}
285
292
} ) ;
286
293
294
+ // eslint-disable-next-line no-param-reassign
287
295
assets [ chunkFile ] = newAsset ;
288
296
289
- var newSource = newAsset . source ( ) ;
290
- hashByChunkId [ chunk . id ] = newAsset . integrity = computeIntegrity ( newSource ) ;
297
+ newSource = newAsset . source ( ) ;
298
+ newAsset . integrity = computeIntegrity ( newSource ) ;
299
+ hashByChunkId [ chunk . id ] = newAsset . integrity ;
291
300
}
292
- return [ chunk . id ] . concat ( depChunkIds ) ;
301
+ return [ chunk . id ] . concat ( depChunkIds ) ;
293
302
}
294
303
295
304
compilation . chunks . forEach ( function forEachChunk ( chunk ) {
296
- // chunk.entry was removed in Webpack 2. Use hasRuntime() for this check instead (if it exists)
305
+ // chunk.entry was removed in Webpack 2. Use hasRuntime()
306
+ // for this check instead (if it exists)
297
307
if ( ( 'hasRuntime' in chunk ) ? chunk . hasRuntime ( ) : chunk . entry ) {
298
308
processChunkRecursive ( chunk ) ;
299
309
}
300
310
} ) ;
301
311
302
- for ( var key in assets ) {
303
- if ( assets . hasOwnProperty ( key ) ) {
304
- var asset = assets [ key ] ;
305
- if ( ! asset . integrity ) {
306
- asset . integrity = computeIntegrity ( asset . source ( ) ) ;
307
- }
312
+ Object . keys ( assets ) . forEach ( function loop ( assetKey ) {
313
+ asset = assets [ assetKey ] ;
314
+ if ( ! asset . integrity ) {
315
+ asset . integrity = computeIntegrity ( asset . source ( ) ) ;
308
316
}
309
- }
317
+ } ) ;
310
318
} ) ;
311
319
312
320
function getTagSrc ( tag ) {
@@ -339,8 +347,11 @@ SubresourceIntegrityPlugin.prototype.apply = function apply(compiler) {
339
347
return ;
340
348
}
341
349
// Add integrity check sums
350
+
351
+ /* eslint-disable no-param-reassign */
342
352
tag . attributes . integrity = checksum ;
343
353
tag . attributes . crossorigin = self . options . crossorigin ;
354
+ /* eslint-enable no-param-reassign */
344
355
}
345
356
346
357
pluginArgs . head . filter ( filterTag ) . forEach ( processTag ) ;
@@ -355,6 +366,7 @@ SubresourceIntegrityPlugin.prototype.apply = function apply(compiler) {
355
366
function beforeHtmlGeneration ( pluginArgs , callback ) {
356
367
self . hwpPublicPath = pluginArgs . assets . publicPath ;
357
368
[ 'js' , 'css' ] . forEach ( function addIntegrity ( fileType ) {
369
+ // eslint-disable-next-line no-param-reassign
358
370
pluginArgs . assets [ fileType + 'Integrity' ] =
359
371
pluginArgs . assets [ fileType ] . map ( function assetIntegrity ( filePath ) {
360
372
var src = self . hwpAssetPath ( filePath ) ;
0 commit comments