-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix bug with named chunks in Webpack 5
Closes #135
- Loading branch information
Showing
7 changed files
with
74 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# With a dynamically loaded, named chunk #hwp | ||
|
||
Ensure that when a named chunk is loaded dynamically with Webpack 5, | ||
it receives a SRI hash. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
let scriptsWithIntegrity = []; | ||
|
||
const observer = new MutationObserver(mutationsList => { | ||
Array.from(mutationsList).forEach(mutation => { | ||
Array.from(mutation.addedNodes || []).forEach(node => { | ||
if (node.nodeName === 'SCRIPT') { | ||
if ( | ||
node.getAttribute('crossOrigin') === 'anonymous' && | ||
node | ||
.getAttribute('integrity') | ||
.match(/^sha256-[-A-Za-z0-9+/=]{44} sha384-[-A-Za-z0-9+/=]{64}$/) | ||
) { | ||
scriptsWithIntegrity.push(node); | ||
} | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
observer.observe(document.querySelector('head'), { childList: true }); | ||
|
||
import('./chunk') | ||
.then(() => { | ||
if ( | ||
scriptsWithIntegrity.some( | ||
script => | ||
new URL(script.getAttribute('src')).pathname === '/chunk_js.js' | ||
) | ||
) { | ||
console.log('ok'); | ||
} else { | ||
console.log('error'); | ||
} | ||
}) | ||
.catch(e => { | ||
console.error(e); | ||
console.log('error'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
var webpackVersionComponents = require('webpack/package.json').version.split( | ||
'.' | ||
); | ||
var webpackVersionMajor = Number(webpackVersionComponents[0]); | ||
|
||
module.exports.skip = function skip() { | ||
return webpackVersionMajor < 5; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
var SriPlugin = require('webpack-subresource-integrity'); | ||
var HtmlWebpackPlugin = require('html-webpack-plugin'); | ||
|
||
module.exports = { | ||
entry: { | ||
index: './index.js' | ||
}, | ||
output: { | ||
crossOriginLoading: 'anonymous', | ||
}, | ||
optimization: { | ||
chunkIds: 'named', | ||
}, | ||
plugins: [ | ||
new SriPlugin({ | ||
hashFuncNames: ['sha256', 'sha384'], | ||
enabled: true | ||
}), | ||
new HtmlWebpackPlugin() | ||
] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters