|
7 | 7 |
|
8 | 8 | var Template = require('webpack/lib/Template');
|
9 | 9 | var util = require('./util');
|
| 10 | +var webpackVersionComponents = require('webpack/package.json').version.split( |
| 11 | + '.' |
| 12 | +); |
| 13 | +var webpackVersionMajor = Number(webpackVersionComponents[0]); |
10 | 14 |
|
11 | 15 | function WebIntegrityJsonpMainTemplatePlugin(sriPlugin, compilation) {
|
12 | 16 | this.sriPlugin = sriPlugin;
|
@@ -47,17 +51,20 @@ WebIntegrityJsonpMainTemplatePlugin.prototype.addSriHashes =
|
47 | 51 | * Patch jsonp-script code to add the integrity attribute.
|
48 | 52 | */
|
49 | 53 | WebIntegrityJsonpMainTemplatePlugin.prototype.addAttribute =
|
50 |
| - function addAttribute(mainTemplate, elName, source, chunk) { |
| 54 | + function addAttribute(mainTemplate, elName, source) { |
51 | 55 | const outputOptions = this.compilation.outputOptions || mainTemplate.outputOptions;
|
52 | 56 | if (!outputOptions.crossOriginLoading) {
|
53 | 57 | this.sriPlugin.errorOnce(
|
54 | 58 | this.compilation,
|
55 | 59 | 'webpack option output.crossOriginLoading not set, code splitting will not work!'
|
56 | 60 | );
|
57 | 61 | }
|
| 62 | + |
58 | 63 | return (Template.asString || mainTemplate.asString)([
|
59 | 64 | source,
|
60 |
| - elName + '.integrity = __webpack_require__.sriHashes[' + (chunk ? `'${chunk.id}'` : 'chunkId') + '];', |
| 65 | + elName + '.integrity = __webpack_require__.sriHashes[' + |
| 66 | + ((webpackVersionMajor >= 5 && elName === 'script') ? 'key.match(/^chunk-([0-9]+)$/)[1]' : 'chunkId') + |
| 67 | + '];', |
61 | 68 | elName + '.crossOrigin = ' + JSON.stringify(outputOptions.crossOriginLoading) + ';',
|
62 | 69 | ]);
|
63 | 70 | };
|
|
0 commit comments