Skip to content

Commit

Permalink
fix: the same chunkId is overwritten after using mini-css-extract-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
msidolphin committed Oct 12, 2023
1 parent 1008584 commit 46a79dd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions examples/mini-css-extract-plugin/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ module.exports = {
// both options are optional
filename: "[name].css",
chunkFilename: "[id].css",
insert: (link) => {
link.integrity =
__webpack_require__.sriHashes[chunkId + "_css/mini-extract"];

Check warning on line 18 in examples/mini-css-extract-plugin/webpack.config.js

View workflow job for this annotation

GitHub Actions / lint

'__webpack_require__' is not defined

Check warning on line 18 in examples/mini-css-extract-plugin/webpack.config.js

View workflow job for this annotation

GitHub Actions / lint

'chunkId' is not defined
document.head.appendChild(link);
},
}),
new SubresourceIntegrityPlugin({
hashFuncNames: ["sha256", "sha384"],
Expand Down
6 changes: 5 additions & 1 deletion webpack-subresource-integrity/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
tryGetSource,
replaceInSource,
usesAnyHash,
normalizeChunkId,
} from "./util";
import { getChunkToManifestMap } from "./manifest";
import { AssetIntegrity } from "./integrity";
Expand Down Expand Up @@ -176,7 +177,10 @@ export class Plugin {

if (childChunk.id !== null) {
this.hashByPlaceholder.set(
makePlaceholder(this.options.hashFuncNames, childChunk.id),
makePlaceholder(
this.options.hashFuncNames,
normalizeChunkId(sourcePath, childChunk)
),
integrity
);
}
Expand Down
16 changes: 16 additions & 0 deletions webpack-subresource-integrity/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export type ChunkGroup = ReturnType<Compilation["addChunkInGroup"]>;

export const sriHashVariableReference = "__webpack_require__.sriHashes";

export const miniCssExtractType = "css/mini-extract";

export function assert(value: unknown, message: string): asserts value {
if (!value) {
throw new Error(message);
Expand Down Expand Up @@ -118,6 +120,13 @@ export function generateSriHashPlaceholders(
): Record<string, string> {
return Array.from(chunks).reduce((sriHashes, depChunk: Chunk) => {
if (depChunk.id) {
const hasMiniCssExtractFile = !!depChunk.contentHash[miniCssExtractType];
if (hasMiniCssExtractFile) {
sriHashes[`${depChunk.id}_${miniCssExtractType}`] = makePlaceholder(
hashFuncNames,
`${depChunk.id}_${miniCssExtractType}`
);
}
sriHashes[depChunk.id] = makePlaceholder(hashFuncNames, depChunk.id);
}
return sriHashes;
Expand Down Expand Up @@ -291,3 +300,10 @@ export function hasOwnProperty<X extends object, Y extends PropertyKey>(
): obj is X & Record<Y, unknown> {
return Object.prototype.hasOwnProperty.call(obj, prop);
}

export const normalizeChunkId = (sourcePath: string, chunk: Chunk) => {
if (sourcePath.endsWith(".css") && chunk.contentHash[miniCssExtractType]) {
return `${chunk.id}_${miniCssExtractType}`;
}
return chunk.id as string | number;
};

0 comments on commit 46a79dd

Please sign in to comment.