Skip to content

Commit

Permalink
fix: compatible with pure mini-css-extract module
Browse files Browse the repository at this point in the history
  • Loading branch information
msidolphin committed Oct 13, 2023
1 parent e9a568a commit 63d3acb
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions webpack-subresource-integrity/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

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

export function hasMiniCssExtractModlue(modules: Module[]) {
return modules.find((module) => module.type === miniCssExtractType);
}

export function generateSriHashPlaceholders(
compilation: Compilation,
chunks: Iterable<Chunk>,
hashFuncNames: [string, ...string[]]
): Record<string, string> {
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 });
Expand Down Expand Up @@ -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}`;
}
Expand Down

0 comments on commit 63d3acb

Please sign in to comment.