diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ec29205f..d3565590 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -64,6 +64,7 @@ _Before_ submitting a pull request, please make sure the following is doneā€¦ 8. Ensure the test suite passes via `yarn test`. ```sh-session + $ yarn test:prepare # build example and generate fixtures before running tests $ yarn test ``` diff --git a/packages/server/src/ChunkExtractor.js b/packages/server/src/ChunkExtractor.js index 23fab97c..8ffadbd5 100644 --- a/packages/server/src/ChunkExtractor.js +++ b/packages/server/src/ChunkExtractor.js @@ -233,9 +233,12 @@ class ChunkExtractor { createChunkAsset({ filename, chunk, type, linkType }) { const resolvedFilename = typeof filename === 'object' && filename.name ? filename.name : filename + const resolvedIntegrity = + typeof filename === 'object' && filename.integrity ? filename.integrity : null return { filename: resolvedFilename, + integrity: resolvedIntegrity, scriptType: getFileScriptType(resolvedFilename), chunk, url: this.resolvePublicUrl(resolvedFilename), diff --git a/packages/server/src/ChunkExtractor.test.js b/packages/server/src/ChunkExtractor.test.js index 95c3070e..4ee95632 100644 --- a/packages/server/src/ChunkExtractor.test.js +++ b/packages/server/src/ChunkExtractor.test.js @@ -102,6 +102,31 @@ describe('ChunkExtrator', () => { `) }) + it('should add integrity if available in stats', () => { + const testExtractor = new ChunkExtractor({ + stats: { + ...stats, + namedChunkGroups: { + ...stats.namedChunkGroups, + main: { + ...stats.namedChunkGroups.main, + assets: stats.namedChunkGroups.main.assets.map(name => ({ + name, + // pseudo hash - reversed name + integrity: name.split('').reverse().join(''), + })), + }, + }, + }, + outputPath: targetPath, + }) + expect(testExtractor.getScriptTags({ crossorigin: 'anonymous' })) + .toMatchInlineSnapshot(` + " + " + `) + }) + it('should add extra props if specified - object argument', () => { extractor.addChunk('letters-A') expect(extractor.getScriptTags({ nonce: 'testnonce' })) diff --git a/packages/webpack-plugin/src/index.js b/packages/webpack-plugin/src/index.js index a844cacb..381b7e7e 100644 --- a/packages/webpack-plugin/src/index.js +++ b/packages/webpack-plugin/src/index.js @@ -39,8 +39,18 @@ class LoadablePlugin { return { id: chunk.id, files: [...chunk.files], - }; - }); + } + }) + + // update namedChunkGroups with integrity from webpack-subresource-integrity if available + Object.values(stats.namedChunkGroups).forEach(namedChunkGroup => { + namedChunkGroup.assets.forEach(namedChunkGroupAsset => { + const asset = stats.assets.find(a => a.name === namedChunkGroupAsset.name) || {} + if (asset.integrity) { + namedChunkGroupAsset.integrity = asset.integrity + } + }) + }) const result = JSON.stringify(stats, null, 2)