Skip to content

Commit dae33e6

Browse files
authored
Merge pull request #137 from waysact/135-webpack-5-named-chunks
Fix bug with named chunks in Webpack 5
2 parents 8c5509e + a72dfc0 commit dae33e6

File tree

7 files changed

+74
-2
lines changed

7 files changed

+74
-2
lines changed

examples/dynamic-modified/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports.skip = function skip() {
1212

1313
module.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
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
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.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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(/^sha256-[-A-Za-z0-9+/=]{44} sha384-[-A-Za-z0-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+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
};

jmtp.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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
]);

0 commit comments

Comments
 (0)