20
20
}
21
21
}
22
22
23
+ /**
24
+ * Remove the public path from a URL, if present
25
+ * @param publicPath
26
+ * @param {string } filePath
27
+ * @returns {string }
28
+ */
29
+ const getFilename = ( publicPath , filePath ) => {
30
+ if ( ! publicPath || ! filePath . startsWith ( publicPath ) ) {
31
+ return filePath ;
32
+ }
33
+ return filePath . substr ( publicPath . length ) ;
34
+ } ;
35
+
23
36
/**
24
37
* The default function for adding the CSP to the head of a document
25
38
* Can be overwritten to allow the developer to process the CSP in their own way
@@ -411,19 +424,6 @@ class CspHtmlWebpackPlugin {
411
424
return compileCb ( null , htmlPluginData ) ;
412
425
}
413
426
414
- /**
415
- * Remove the public path from a URL, if present
416
- * @param publicPath
417
- * @param {string } path
418
- * @returns {string }
419
- */
420
- getFilename ( publicPath , path ) {
421
- if ( ! publicPath || ! path . startsWith ( publicPath ) ) {
422
- return path ;
423
- }
424
- return path . substr ( publicPath . length ) ;
425
- }
426
-
427
427
/**
428
428
* Add integrity attributes to asset tags
429
429
* @param compilation
@@ -432,20 +432,38 @@ class CspHtmlWebpackPlugin {
432
432
*/
433
433
addIntegrityAttributes ( compilation , htmlPluginData , compileCb ) {
434
434
if ( this . hashEnabled [ 'script-src' ] !== false ) {
435
- htmlPluginData . assetTags . scripts . filter ( tag => tag . attributes . src ) . forEach ( tag => {
436
- const filename = this . getFilename ( compilation . options . output . publicPath , tag . attributes . src ) ;
437
- if ( filename in compilation . assets ) {
438
- tag . attributes . integrity = this . hashFile ( compilation . assets , filename ) . slice ( 1 , - 1 ) ;
439
- }
440
- } ) ;
435
+ htmlPluginData . assetTags . scripts
436
+ . filter ( ( tag ) => tag . attributes . src )
437
+ . forEach ( ( tag ) => {
438
+ const filename = getFilename (
439
+ compilation . options . output . publicPath ,
440
+ tag . attributes . src
441
+ ) ;
442
+ if ( filename in compilation . assets ) {
443
+ // eslint-disable-next-line no-param-reassign
444
+ tag . attributes . integrity = this . hashFile (
445
+ compilation . assets ,
446
+ filename
447
+ ) . slice ( 1 , - 1 ) ;
448
+ }
449
+ } ) ;
441
450
}
442
451
if ( this . hashEnabled [ 'style-src' ] !== false ) {
443
- htmlPluginData . assetTags . styles . filter ( tag => tag . attributes . href ) . forEach ( tag => {
444
- const filename = this . getFilename ( compilation . options . output . publicPath , tag . attributes . href ) ;
445
- if ( filename in compilation . assets ) {
446
- tag . attributes . integrity = this . hashFile ( compilation . assets , filename ) . slice ( 1 , - 1 ) ;
447
- }
448
- } ) ;
452
+ htmlPluginData . assetTags . styles
453
+ . filter ( ( tag ) => tag . attributes . href )
454
+ . forEach ( ( tag ) => {
455
+ const filename = getFilename (
456
+ compilation . options . output . publicPath ,
457
+ tag . attributes . href
458
+ ) ;
459
+ if ( filename in compilation . assets ) {
460
+ // eslint-disable-next-line no-param-reassign
461
+ tag . attributes . integrity = this . hashFile (
462
+ compilation . assets ,
463
+ filename
464
+ ) . slice ( 1 , - 1 ) ;
465
+ }
466
+ } ) ;
449
467
}
450
468
return compileCb ( null , htmlPluginData ) ;
451
469
}
@@ -471,7 +489,7 @@ class CspHtmlWebpackPlugin {
471
489
HtmlWebpackPlugin . getHooks ( compilation ) . alterAssetTags . tapAsync (
472
490
'CspHtmlWebpackPlugin' ,
473
491
this . addIntegrityAttributes . bind ( this , compilation )
474
- )
492
+ ) ;
475
493
} ) ;
476
494
}
477
495
}
0 commit comments