Skip to content

Commit 9b34195

Browse files
fix: SubResourceIntegrity (#803)
1 parent 73b17fd commit 9b34195

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

CONTRIBUTING.md

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ _Before_ submitting a pull request, please make sure the following is done…
6464
8. Ensure the test suite passes via `yarn test`.
6565

6666
```sh-session
67+
$ yarn test:prepare # build example and generate fixtures before running tests
6768
$ yarn test
6869
```
6970

packages/server/src/ChunkExtractor.js

+3
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,12 @@ class ChunkExtractor {
233233
createChunkAsset({ filename, chunk, type, linkType }) {
234234
const resolvedFilename =
235235
typeof filename === 'object' && filename.name ? filename.name : filename
236+
const resolvedIntegrity =
237+
typeof filename === 'object' && filename.integrity ? filename.integrity : null
236238

237239
return {
238240
filename: resolvedFilename,
241+
integrity: resolvedIntegrity,
239242
scriptType: getFileScriptType(resolvedFilename),
240243
chunk,
241244
url: this.resolvePublicUrl(resolvedFilename),

packages/server/src/ChunkExtractor.test.js

+25
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,31 @@ describe('ChunkExtrator', () => {
102102
`)
103103
})
104104

105+
it('should add integrity if available in stats', () => {
106+
const testExtractor = new ChunkExtractor({
107+
stats: {
108+
...stats,
109+
namedChunkGroups: {
110+
...stats.namedChunkGroups,
111+
main: {
112+
...stats.namedChunkGroups.main,
113+
assets: stats.namedChunkGroups.main.assets.map(name => ({
114+
name,
115+
// pseudo hash - reversed name
116+
integrity: name.split('').reverse().join(''),
117+
})),
118+
},
119+
},
120+
},
121+
outputPath: targetPath,
122+
})
123+
expect(testExtractor.getScriptTags({ crossorigin: 'anonymous' }))
124+
.toMatchInlineSnapshot(`
125+
"<script id=\\"__LOADABLE_REQUIRED_CHUNKS__\\" type=\\"application/json\\" crossorigin=\\"anonymous\\">[]</script><script id=\\"__LOADABLE_REQUIRED_CHUNKS___ext\\" type=\\"application/json\\" crossorigin=\\"anonymous\\">{\\"namedChunks\\":[]}</script>
126+
<script async data-chunk=\\"main\\" src=\\"/dist/node/main.js\\" integrity=\\"sj.niam\\" crossorigin=\\"anonymous\\"></script>"
127+
`)
128+
})
129+
105130
it('should add extra props if specified - object argument', () => {
106131
extractor.addChunk('letters-A')
107132
expect(extractor.getScriptTags({ nonce: 'testnonce' }))

packages/webpack-plugin/src/index.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,20 @@ class LoadablePlugin {
3939
return {
4040
id: chunk.id,
4141
files: [...chunk.files],
42-
};
43-
});
42+
}
43+
})
44+
45+
// update namedChunkGroups with integrity from webpack-subresource-integrity if available
46+
Object.values(stats.namedChunkGroups).forEach(namedChunkGroup => {
47+
namedChunkGroup.assets.forEach(namedChunkGroupAsset => {
48+
if (!namedChunkGroupAsset.integrity) {
49+
const asset = stats.assets.find(a => a.name === namedChunkGroupAsset.name) || {}
50+
if (asset.integrity) {
51+
namedChunkGroupAsset.integrity = asset.integrity
52+
}
53+
}
54+
})
55+
})
4456

4557
const result = JSON.stringify(stats, null, 2)
4658

0 commit comments

Comments
 (0)