Skip to content

Commit 63d3acb

Browse files
committed
fix: compatible with pure mini-css-extract module
1 parent e9a568a commit 63d3acb

File tree

1 file changed

+19
-9
lines changed
  • webpack-subresource-integrity/src

1 file changed

+19
-9
lines changed

webpack-subresource-integrity/src/util.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@
66
*/
77

88
import { createHash } from "crypto";
9-
import type { AssetInfo, Chunk, Compilation, Compiler, sources } from "webpack";
9+
import type {
10+
AssetInfo,
11+
Chunk,
12+
Compilation,
13+
Compiler,
14+
sources,
15+
Module,
16+
} from "webpack";
1017
import { sep } from "path";
1118
import type { HtmlTagObject } from "./types";
1219

@@ -114,23 +121,28 @@ export function notNil<TValue>(
114121
return value !== null && value !== undefined;
115122
}
116123

124+
export function hasMiniCssExtractModlue(modules: Module[]) {
125+
return modules.find((module) => module.type === miniCssExtractType);
126+
}
127+
117128
export function generateSriHashPlaceholders(
118129
compilation: Compilation,
119130
chunks: Iterable<Chunk>,
120131
hashFuncNames: [string, ...string[]]
121132
): Record<string, string> {
122133
return Array.from(chunks).reduce((sriHashes, depChunk: Chunk) => {
123134
if (depChunk.id) {
124-
const hasMiniCssExtractFile = compilation.chunkGraph
125-
.getChunkModules(depChunk)
126-
.find((module) => module.type === miniCssExtractType);
127-
if (hasMiniCssExtractFile) {
135+
const modules = compilation.chunkGraph.getChunkModules(depChunk);
136+
const containCssModule = hasMiniCssExtractModlue(modules);
137+
if (containCssModule) {
128138
sriHashes[`${depChunk.id}_${miniCssExtractType}`] = makePlaceholder(
129139
hashFuncNames,
130140
`${depChunk.id}_${miniCssExtractType}`
131141
);
132142
}
133-
sriHashes[depChunk.id] = makePlaceholder(hashFuncNames, depChunk.id);
143+
if (!containCssModule || (containCssModule && modules.length > 1)) {
144+
sriHashes[depChunk.id] = makePlaceholder(hashFuncNames, depChunk.id);
145+
}
134146
}
135147
return sriHashes;
136148
}, {} as { [key: string]: string });
@@ -311,9 +323,7 @@ export const normalizeChunkId = (
311323
) => {
312324
if (
313325
sourcePath.endsWith(".css") &&
314-
compilation.chunkGraph
315-
.getChunkModules(chunk)
316-
.find((module) => module.type === miniCssExtractType)
326+
hasMiniCssExtractModlue(compilation.chunkGraph.getChunkModules(chunk))
317327
) {
318328
return `${chunk.id}_${miniCssExtractType}`;
319329
}

0 commit comments

Comments
 (0)