File tree Expand file tree Collapse file tree 7 files changed +74
-2
lines changed Expand file tree Collapse file tree 7 files changed +74
-2
lines changed Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ module.exports.skip = function skip() {
1212
1313module . exports . check = function check ( stats , url , browser ) {
1414 const otherAsset = Object . keys ( stats . compilation . assets ) . find ( key => key !== 'index.js' && key . endsWith ( ".js" ) ) ;
15- fs . writeFileSync ( 'dist/' + otherAsset , 'xxx ' ) ;
15+ fs . writeFileSync ( 'dist/' + otherAsset , 'console.log("corrupted"); ' ) ;
1616
1717 return defaultCheck ( stats , url , browser ) ;
1818} ;
Original file line number Diff line number Diff line change 1+ # With a dynamically loaded, named chunk #hwp
2+
3+ Ensure that when a named chunk is loaded dynamically with Webpack 5,
4+ it receives a SRI hash.
Original file line number Diff line number Diff line change 1+ ;
Original file line number Diff line number Diff line change 1+ let scriptsWithIntegrity = [ ] ;
2+
3+ const observer = new MutationObserver ( mutationsList => {
4+ Array . from ( mutationsList ) . forEach ( mutation => {
5+ Array . from ( mutation . addedNodes || [ ] ) . forEach ( node => {
6+ if ( node . nodeName === 'SCRIPT' ) {
7+ if (
8+ node . getAttribute ( 'crossOrigin' ) === 'anonymous' &&
9+ node
10+ . getAttribute ( 'integrity' )
11+ . match ( / ^ s h a 2 5 6 - [ - A - Z a - z 0 - 9 + / = ] { 44 } s h a 3 8 4 - [ - A - Z a - z 0 - 9 + / = ] { 64 } $ / )
12+ ) {
13+ scriptsWithIntegrity . push ( node ) ;
14+ }
15+ }
16+ } ) ;
17+ } ) ;
18+ } ) ;
19+
20+ observer . observe ( document . querySelector ( 'head' ) , { childList : true } ) ;
21+
22+ import ( './chunk' )
23+ . then ( ( ) => {
24+ if (
25+ scriptsWithIntegrity . some (
26+ script =>
27+ new URL ( script . getAttribute ( 'src' ) ) . pathname === '/chunk_js.js'
28+ )
29+ ) {
30+ console . log ( 'ok' ) ;
31+ } else {
32+ console . log ( 'error' ) ;
33+ }
34+ } )
35+ . catch ( e => {
36+ console . error ( e ) ;
37+ console . log ( 'error' ) ;
38+ } ) ;
Original file line number Diff line number Diff line change 1+ var webpackVersionComponents = require ( 'webpack/package.json' ) . version . split (
2+ '.'
3+ ) ;
4+ var webpackVersionMajor = Number ( webpackVersionComponents [ 0 ] ) ;
5+
6+ module . exports . skip = function skip ( ) {
7+ return webpackVersionMajor < 5 ;
8+ } ;
Original file line number Diff line number Diff line change 1+ var SriPlugin = require ( 'webpack-subresource-integrity' ) ;
2+ var HtmlWebpackPlugin = require ( 'html-webpack-plugin' ) ;
3+
4+ module . exports = {
5+ entry : {
6+ index : './index.js'
7+ } ,
8+ output : {
9+ crossOriginLoading : 'anonymous' ,
10+ } ,
11+ optimization : {
12+ chunkIds : 'named' ,
13+ } ,
14+ plugins : [
15+ new SriPlugin ( {
16+ hashFuncNames : [ 'sha256' , 'sha384' ] ,
17+ enabled : true
18+ } ) ,
19+ new HtmlWebpackPlugin ( )
20+ ]
21+ } ;
Original file line number Diff line number Diff line change @@ -63,7 +63,7 @@ WebIntegrityJsonpMainTemplatePlugin.prototype.addAttribute =
6363 return ( Template . asString || mainTemplate . asString ) ( [
6464 source ,
6565 elName + '.integrity = __webpack_require__.sriHashes[' +
66- ( ( webpackVersionMajor >= 5 && elName === 'script' ) ? 'key.match(/^chunk-([0-9] +)$/)[1]' : 'chunkId' ) +
66+ ( ( webpackVersionMajor >= 5 && elName === 'script' ) ? 'key.match(/^chunk-(. +)$/)[1]' : 'chunkId' ) +
6767 '];' ,
6868 elName + '.crossOrigin = ' + JSON . stringify ( outputOptions . crossOriginLoading ) + ';' ,
6969 ] ) ;
You can’t perform that action at this time.
0 commit comments