Skip to content

Commit

Permalink
Fix bug with named chunks in Webpack 5
Browse files Browse the repository at this point in the history
Closes #135
  • Loading branch information
jscheid committed Nov 4, 2020
1 parent ed3c8cd commit a72dfc0
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 2 deletions.
2 changes: 1 addition & 1 deletion examples/dynamic-modified/test.js
Expand Up @@ -12,7 +12,7 @@ module.exports.skip = function skip() {

module.exports.check = function check(stats, url, browser) {
const otherAsset = Object.keys(stats.compilation.assets).find(key => key !== 'index.js' && key.endsWith(".js"));
fs.writeFileSync('dist/' + otherAsset, 'xxx');
fs.writeFileSync('dist/' + otherAsset, 'console.log("corrupted");');

return defaultCheck(stats, url, browser);
};
4 changes: 4 additions & 0 deletions examples/dynamic-named-chunks/README.md
@@ -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.
1 change: 1 addition & 0 deletions examples/dynamic-named-chunks/chunk.js
@@ -0,0 +1 @@
;
38 changes: 38 additions & 0 deletions examples/dynamic-named-chunks/index.js
@@ -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');
});
8 changes: 8 additions & 0 deletions examples/dynamic-named-chunks/test.js
@@ -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;
};
21 changes: 21 additions & 0 deletions examples/dynamic-named-chunks/webpack.config.js
@@ -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()
]
};
2 changes: 1 addition & 1 deletion jmtp.js
Expand Up @@ -63,7 +63,7 @@ WebIntegrityJsonpMainTemplatePlugin.prototype.addAttribute =
return (Template.asString || mainTemplate.asString)([
source,
elName + '.integrity = __webpack_require__.sriHashes[' +
((webpackVersionMajor >= 5 && elName === 'script') ? 'key.match(/^chunk-([0-9]+)$/)[1]' : 'chunkId') +
((webpackVersionMajor >= 5 && elName === 'script') ? 'key.match(/^chunk-(.+)$/)[1]' : 'chunkId') +
'];',
elName + '.crossOrigin = ' + JSON.stringify(outputOptions.crossOriginLoading) + ';',
]);
Expand Down

0 comments on commit a72dfc0

Please sign in to comment.