Skip to content

Commit 46a79dd

Browse files
committed
fix: the same chunkId is overwritten after using mini-css-extract-plugin
1 parent 1008584 commit 46a79dd

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

examples/mini-css-extract-plugin/webpack.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ module.exports = {
1313
// both options are optional
1414
filename: "[name].css",
1515
chunkFilename: "[id].css",
16+
insert: (link) => {
17+
link.integrity =
18+
__webpack_require__.sriHashes[chunkId + "_css/mini-extract"];
19+
document.head.appendChild(link);
20+
},
1621
}),
1722
new SubresourceIntegrityPlugin({
1823
hashFuncNames: ["sha256", "sha384"],

webpack-subresource-integrity/src/plugin.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
tryGetSource,
3333
replaceInSource,
3434
usesAnyHash,
35+
normalizeChunkId,
3536
} from "./util";
3637
import { getChunkToManifestMap } from "./manifest";
3738
import { AssetIntegrity } from "./integrity";
@@ -176,7 +177,10 @@ export class Plugin {
176177

177178
if (childChunk.id !== null) {
178179
this.hashByPlaceholder.set(
179-
makePlaceholder(this.options.hashFuncNames, childChunk.id),
180+
makePlaceholder(
181+
this.options.hashFuncNames,
182+
normalizeChunkId(sourcePath, childChunk)
183+
),
180184
integrity
181185
);
182186
}

webpack-subresource-integrity/src/util.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export type ChunkGroup = ReturnType<Compilation["addChunkInGroup"]>;
1414

1515
export const sriHashVariableReference = "__webpack_require__.sriHashes";
1616

17+
export const miniCssExtractType = "css/mini-extract";
18+
1719
export function assert(value: unknown, message: string): asserts value {
1820
if (!value) {
1921
throw new Error(message);
@@ -118,6 +120,13 @@ export function generateSriHashPlaceholders(
118120
): Record<string, string> {
119121
return Array.from(chunks).reduce((sriHashes, depChunk: Chunk) => {
120122
if (depChunk.id) {
123+
const hasMiniCssExtractFile = !!depChunk.contentHash[miniCssExtractType];
124+
if (hasMiniCssExtractFile) {
125+
sriHashes[`${depChunk.id}_${miniCssExtractType}`] = makePlaceholder(
126+
hashFuncNames,
127+
`${depChunk.id}_${miniCssExtractType}`
128+
);
129+
}
121130
sriHashes[depChunk.id] = makePlaceholder(hashFuncNames, depChunk.id);
122131
}
123132
return sriHashes;
@@ -291,3 +300,10 @@ export function hasOwnProperty<X extends object, Y extends PropertyKey>(
291300
): obj is X & Record<Y, unknown> {
292301
return Object.prototype.hasOwnProperty.call(obj, prop);
293302
}
303+
304+
export const normalizeChunkId = (sourcePath: string, chunk: Chunk) => {
305+
if (sourcePath.endsWith(".css") && chunk.contentHash[miniCssExtractType]) {
306+
return `${chunk.id}_${miniCssExtractType}`;
307+
}
308+
return chunk.id as string | number;
309+
};

0 commit comments

Comments
 (0)