Created
December 20, 2021 18:58
-
-
Save jscheid/b03aa1a5bf0421d8e2afdd040e4302ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/examples/lazy-hashes-cycles/webpack.config.js b/examples/lazy-hashes-cycles/webpack.config.js | |
index d91a6a8..ab72135 100644 | |
--- a/examples/lazy-hashes-cycles/webpack.config.js | |
+++ b/examples/lazy-hashes-cycles/webpack.config.js | |
@@ -34,12 +34,12 @@ module.exports = { | |
"utf-8" | |
); | |
const sriRegex = new RegExp( | |
- `${ | |
- isEntry ? "self.sriHashes=" : "Object.assign\\(self.sriHashes," | |
- }(?<sriHashJson>\{.*?\})` | |
+ `${isEntry ? "sriHashes=" : "sriHashes,"}(?<sriHashJson>\{.*?\})` | |
); | |
const regexMatch = sriRegex.exec(fileContent); | |
- const sriHashJson = regexMatch ? regexMatch.groups.sriHashJson : null; | |
+ const sriHashJson = regexMatch | |
+ ? regexMatch.groups.sriHashJson | |
+ : null; | |
if (!sriHashJson) { | |
return null; | |
} | |
diff --git a/examples/lazy-hashes-multiple-parents/webpack.config.js b/examples/lazy-hashes-multiple-parents/webpack.config.js | |
index c52f861..40f02ab 100644 | |
--- a/examples/lazy-hashes-multiple-parents/webpack.config.js | |
+++ b/examples/lazy-hashes-multiple-parents/webpack.config.js | |
@@ -34,12 +34,12 @@ module.exports = { | |
"utf-8" | |
); | |
const sriRegex = new RegExp( | |
- `${ | |
- isEntry ? "self.sriHashes=" : "Object.assign\\(self.sriHashes," | |
- }(?<sriHashJson>\{.*?\})` | |
+ `${isEntry ? "sriHashes=" : "sriHashes,"}(?<sriHashJson>\{.*?\})` | |
); | |
const regexMatch = sriRegex.exec(fileContent); | |
- const sriHashJson = regexMatch ? regexMatch.groups.sriHashJson : null; | |
+ const sriHashJson = regexMatch | |
+ ? regexMatch.groups.sriHashJson | |
+ : null; | |
if (!sriHashJson) { | |
return null; | |
} | |
diff --git a/examples/lazy-hashes-simple/webpack.config.js b/examples/lazy-hashes-simple/webpack.config.js | |
index 68cb63b..6681a62 100644 | |
--- a/examples/lazy-hashes-simple/webpack.config.js | |
+++ b/examples/lazy-hashes-simple/webpack.config.js | |
@@ -34,12 +34,12 @@ module.exports = { | |
"utf-8" | |
); | |
const sriRegex = new RegExp( | |
- `${ | |
- isEntry ? "self.sriHashes=" : "Object.assign\\(self.sriHashes," | |
- }(?<sriHashJson>\{.*?\})` | |
+ `${isEntry ? "sriHashes=" : "sriHashes,"}(?<sriHashJson>\{.*?\})` | |
); | |
const regexMatch = sriRegex.exec(fileContent); | |
- const sriHashJson = regexMatch ? regexMatch.groups.sriHashJson : null; | |
+ const sriHashJson = regexMatch | |
+ ? regexMatch.groups.sriHashJson | |
+ : null; | |
if (!sriHashJson) { | |
return null; | |
} | |
diff --git a/examples/sourcemap-code-splitting/webpack.config.js b/examples/sourcemap-code-splitting/webpack.config.js | |
index fbef6b4..63d5002 100644 | |
--- a/examples/sourcemap-code-splitting/webpack.config.js | |
+++ b/examples/sourcemap-code-splitting/webpack.config.js | |
@@ -48,7 +48,7 @@ module.exports = { | |
); | |
const sriHashesInMap = findAndStripSriHashString( | |
"dist/index.js.map", | |
- "self.sriHashes = " | |
+ "__webpack_require__.sriHashes = " | |
); | |
expect(sriHashesInSource.length).toEqual(sriHashesInMap.length); | |
}); | |
diff --git a/webpack-subresource-integrity/index.ts b/webpack-subresource-integrity/index.ts | |
index 8fb3e36..511bf51 100644 | |
--- a/webpack-subresource-integrity/index.ts | |
+++ b/webpack-subresource-integrity/index.ts | |
@@ -185,7 +185,7 @@ export class SubresourceIntegrityPlugin { | |
if (Object.keys(includedChunks).length > 0) { | |
return compilation.compiler.webpack.Template.asString([ | |
source, | |
- `${plugin.sriHashVariableReference} = ` + | |
+ `__webpack_require__.sriHashes = ` + | |
JSON.stringify( | |
generateSriHashPlaceholders( | |
Array.from(allChunks).filter( | |
@@ -206,27 +206,30 @@ export class SubresourceIntegrityPlugin { | |
if (this.options.lazyHashes) { | |
javascript.JavascriptModulesPlugin.getCompilationHooks( | |
compilation | |
- ).renderContent.tap(thisPluginName, (originalSource, { chunk }) => { | |
- const childChunks = plugin.getDirectChildChunks(chunk); | |
- | |
- if (childChunks.size === 0 || chunk.hasRuntime()) { | |
- return originalSource; | |
- } else { | |
- const newSource = new sources.ConcatSource(); | |
- | |
- newSource.add( | |
- `Object.assign(${plugin.sriHashVariableReference}, ${JSON.stringify( | |
- generateSriHashPlaceholders( | |
- childChunks, | |
- this.options.hashFuncNames | |
- ) | |
- )});` | |
- ); | |
- newSource.add(originalSource); | |
- | |
- return newSource; | |
+ ).renderModuleContent.tap( | |
+ thisPluginName, | |
+ (originalSource, _, { chunk }) => { | |
+ const childChunks = plugin.getDirectChildChunks(chunk); | |
+ | |
+ if (childChunks.size === 0 || chunk.hasRuntime()) { | |
+ return originalSource; | |
+ } else { | |
+ const newSource = new sources.ConcatSource(); | |
+ | |
+ newSource.add( | |
+ `Object.assign(__webpack_require__.sriHashes, ${JSON.stringify( | |
+ generateSriHashPlaceholders( | |
+ childChunks, | |
+ this.options.hashFuncNames | |
+ ) | |
+ )});` | |
+ ); | |
+ newSource.add(originalSource); | |
+ | |
+ return newSource; | |
+ } | |
} | |
- }); | |
+ ); | |
} | |
}; | |
diff --git a/webpack-subresource-integrity/plugin.ts b/webpack-subresource-integrity/plugin.ts | |
index 1acb14c..636a8ec 100644 | |
--- a/webpack-subresource-integrity/plugin.ts | |
+++ b/webpack-subresource-integrity/plugin.ts | |
@@ -112,11 +112,6 @@ export class Plugin { | |
*/ | |
private hashByChunkId = new Map<string | number, string>(); | |
- /** | |
- * @internal | |
- */ | |
- public readonly sriHashVariableReference: string; | |
- | |
public constructor( | |
compilation: Compilation, | |
options: SubresourceIntegrityPluginResolvedOptions, | |
@@ -125,9 +120,6 @@ export class Plugin { | |
this.compilation = compilation; | |
this.options = options; | |
this.reporter = reporter; | |
- this.sriHashVariableReference = `${ | |
- this.compilation.outputOptions.globalObject || "self" | |
- }.sriHashes`; | |
} | |
/** | |
@@ -305,7 +297,7 @@ more information." | |
return this.compilation.compiler.webpack.Template.asString([ | |
source, | |
- elName + `.integrity = ${this.sriHashVariableReference}[chunkId];`, | |
+ elName + `.integrity = __webpack_require__.sriHashes[chunkId];`, | |
elName + | |
".crossOrigin = " + | |
JSON.stringify(this.compilation.outputOptions.crossOriginLoading) + |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment