diff --git a/webpack-subresource-integrity/src/util.ts b/webpack-subresource-integrity/src/util.ts index c1e6b4d..eaaf99e 100644 --- a/webpack-subresource-integrity/src/util.ts +++ b/webpack-subresource-integrity/src/util.ts @@ -6,7 +6,14 @@ */ import { createHash } from "crypto"; -import type { AssetInfo, Chunk, Compilation, Compiler, sources } from "webpack"; +import type { + AssetInfo, + Chunk, + Compilation, + Compiler, + sources, + Module, +} from "webpack"; import { sep } from "path"; import type { HtmlTagObject } from "./types"; @@ -114,6 +121,10 @@ export function notNil( return value !== null && value !== undefined; } +export function hasMiniCssExtractModlue(modules: Module[]) { + return modules.find((module) => module.type === miniCssExtractType); +} + export function generateSriHashPlaceholders( compilation: Compilation, chunks: Iterable, @@ -121,16 +132,17 @@ export function generateSriHashPlaceholders( ): Record { return Array.from(chunks).reduce((sriHashes, depChunk: Chunk) => { if (depChunk.id) { - const hasMiniCssExtractFile = compilation.chunkGraph - .getChunkModules(depChunk) - .find((module) => module.type === miniCssExtractType); - if (hasMiniCssExtractFile) { + const modules = compilation.chunkGraph.getChunkModules(depChunk); + const containCssModule = hasMiniCssExtractModlue(modules); + if (containCssModule) { sriHashes[`${depChunk.id}_${miniCssExtractType}`] = makePlaceholder( hashFuncNames, `${depChunk.id}_${miniCssExtractType}` ); } - sriHashes[depChunk.id] = makePlaceholder(hashFuncNames, depChunk.id); + if (!containCssModule || (containCssModule && modules.length > 1)) { + sriHashes[depChunk.id] = makePlaceholder(hashFuncNames, depChunk.id); + } } return sriHashes; }, {} as { [key: string]: string }); @@ -311,9 +323,7 @@ export const normalizeChunkId = ( ) => { if ( sourcePath.endsWith(".css") && - compilation.chunkGraph - .getChunkModules(chunk) - .find((module) => module.type === miniCssExtractType) + hasMiniCssExtractModlue(compilation.chunkGraph.getChunkModules(chunk)) ) { return `${chunk.id}_${miniCssExtractType}`; }