Last active
December 21, 2021 06:44
-
-
Save jscheid/30ea9da1f2538a69b997dc4afb8dee08 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/webpack-subresource-integrity/index.ts b/webpack-subresource-integrity/index.ts | |
index 8fb3e36..5f89db5 100644 | |
--- a/webpack-subresource-integrity/index.ts | |
+++ b/webpack-subresource-integrity/index.ts | |
@@ -7,7 +7,7 @@ | |
import { createHash } from "crypto"; | |
import type { Compiler, Compilation } from "webpack"; | |
-import { javascript, sources } from "webpack"; | |
+import { sources, RuntimeModule, Template } from "webpack"; | |
import { | |
SubresourceIntegrityPluginResolvedOptions, | |
getHtmlWebpackPluginHooksType, | |
@@ -40,6 +40,23 @@ export interface SubresourceIntegrityPluginOptions { | |
readonly lazyHashes?: boolean; | |
} | |
+class AddLazySriRuntimeModule extends RuntimeModule { | |
+ private sriHashes: unknown; | |
+ | |
+ constructor(sriHashes: unknown) { | |
+ super("webpack-subresource-integrity add SRI hashes lazily"); | |
+ this.sriHashes = sriHashes; | |
+ } | |
+ | |
+ generate() { | |
+ return Template.asString([ | |
+ `Object.assign(__webpack_require__.sriHashes, ${JSON.stringify( | |
+ this.sriHashes | |
+ )});`, | |
+ ]); | |
+ } | |
+} | |
+ | |
/** | |
* The webpack-subresource-integrity plugin. | |
* | |
@@ -185,7 +202,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( | |
@@ -204,29 +221,23 @@ 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 | |
+ compilation.hooks.additionalChunkRuntimeRequirements.tap( | |
+ thisPluginName, | |
+ (chunk) => { | |
+ const childChunks = plugin.getDirectChildChunks(chunk); | |
+ if (childChunks.size > 0 && !chunk.hasRuntime()) { | |
+ compilation.addRuntimeModule( | |
+ chunk, | |
+ new AddLazySriRuntimeModule( | |
+ generateSriHashPlaceholders( | |
+ childChunks, | |
+ this.options.hashFuncNames | |
+ ) | |
) | |
- )});` | |
- ); | |
- newSource.add(originalSource); | |
- | |
- return newSource; | |
+ ); | |
+ } | |
} | |
- }); | |
+ ); | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment