|
6 | 6 | */ |
7 | 7 |
|
8 | 8 | 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"; |
10 | 17 | import { sep } from "path"; |
11 | 18 | import type { HtmlTagObject } from "./types"; |
12 | 19 |
|
@@ -114,23 +121,28 @@ export function notNil<TValue>( |
114 | 121 | return value !== null && value !== undefined; |
115 | 122 | } |
116 | 123 |
|
| 124 | +export function hasMiniCssExtractModlue(modules: Module[]) { |
| 125 | + return modules.find((module) => module.type === miniCssExtractType); |
| 126 | +} |
| 127 | + |
117 | 128 | export function generateSriHashPlaceholders( |
118 | 129 | compilation: Compilation, |
119 | 130 | chunks: Iterable<Chunk>, |
120 | 131 | hashFuncNames: [string, ...string[]] |
121 | 132 | ): Record<string, string> { |
122 | 133 | return Array.from(chunks).reduce((sriHashes, depChunk: Chunk) => { |
123 | 134 | 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) { |
128 | 138 | sriHashes[`${depChunk.id}_${miniCssExtractType}`] = makePlaceholder( |
129 | 139 | hashFuncNames, |
130 | 140 | `${depChunk.id}_${miniCssExtractType}` |
131 | 141 | ); |
132 | 142 | } |
133 | | - sriHashes[depChunk.id] = makePlaceholder(hashFuncNames, depChunk.id); |
| 143 | + if (!containCssModule || (containCssModule && modules.length > 1)) { |
| 144 | + sriHashes[depChunk.id] = makePlaceholder(hashFuncNames, depChunk.id); |
| 145 | + } |
134 | 146 | } |
135 | 147 | return sriHashes; |
136 | 148 | }, {} as { [key: string]: string }); |
@@ -311,9 +323,7 @@ export const normalizeChunkId = ( |
311 | 323 | ) => { |
312 | 324 | if ( |
313 | 325 | sourcePath.endsWith(".css") && |
314 | | - compilation.chunkGraph |
315 | | - .getChunkModules(chunk) |
316 | | - .find((module) => module.type === miniCssExtractType) |
| 326 | + hasMiniCssExtractModlue(compilation.chunkGraph.getChunkModules(chunk)) |
317 | 327 | ) { |
318 | 328 | return `${chunk.id}_${miniCssExtractType}`; |
319 | 329 | } |
|
0 commit comments