Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of chunks with multiple files #155

Merged
merged 4 commits into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions examples/issue-154/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import "./style.css";

window.addEventListener("load", () => {
const { integrity } = document.querySelector("link");
const loaded = getComputedStyle(document.body).background.match(
/rgb\(255, 0, 0\)/
);

console.log(integrity && loaded ? "ok" : "error");
});
18 changes: 18 additions & 0 deletions examples/issue-154/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "issue-154",
"description": "Test case for https://github.com/waysact/webpack-subresource-integrity/issues/154",
"version": "1.0.0",
"license": "MIT",
"private": true,
"devDependencies": {
"css-loader": "^5.1.0",
"expect": "^26.6.2",
"html-webpack-plugin": "^5.2.0",
"mini-css-extract-plugin": "^1.3.8",
"nyc": "*",
"webpack": "^5.24.0",
"webpack-cli": "4",
"webpack-subresource-integrity": "*",
"wsi-test-helper": "*"
}
}
3 changes: 3 additions & 0 deletions examples/issue-154/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: red;
}
54 changes: 54 additions & 0 deletions examples/issue-154/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { SubresourceIntegrityPlugin } = require("webpack-subresource-integrity");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const expect = require("expect");
const { RunInPuppeteerPlugin } = require("wsi-test-helper");

module.exports = {
// mode: "development",
devtool: "cheap-module-source-map",
entry: "./index.js",
output: {
filename: "[contenthash].js",
chunkFilename: "[contenthash].chunk.js",
crossOriginLoading: "anonymous",
},
optimization: {
moduleIds: "deterministic",
realContentHash: true,
chunkIds: "deterministic",
runtimeChunk: "single",
},
module: {
rules: [
{
test: /\.css$/i,
use: [MiniCssExtractPlugin.loader, "css-loader"],
},
],
},
plugins: [
new MiniCssExtractPlugin({
filename: `[contenthash].css`,
chunkFilename: `[contenthash].chunk.css`,
}),
new SubresourceIntegrityPlugin({ enabled: true }),
new HtmlWebpackPlugin(),
{
apply: (compiler) => {
compiler.hooks.done.tap("wsi-test", (stats) => {
const cssAsset = stats
.toJson()
.assets.find((asset) => asset.name.match(/\.css$/));

expect(cssAsset.info.contenthash).toBeDefined();
expect(
cssAsset.info.contenthash.find((hash) => hash.match(/^sha/))
).toBeDefined();
expect(cssAsset.integrity).toBeDefined();
});
},
},
new RunInPuppeteerPlugin(),
],
};
98 changes: 52 additions & 46 deletions webpack-subresource-integrity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,49 +298,50 @@ export class SubresourceIntegrityPlugin {
.forEach((childChunk: Chunk) => {
const files = Array.from(childChunk.files);

let sourcePath = files[files.length - 1];
if (!sourcePath) {
return;
}
files.forEach((sourcePath) => {
if (!sourcePath) {
return;
}

if (assets[sourcePath]) {
this.warnIfHotUpdate(compilation, assets[sourcePath].source());
const newAsset = this.replaceAsset(
compilation.compiler,
assets,
hashByChunkId,
sourcePath
);
const integrity = computeIntegrity(
this.options.hashFuncNames,
newAsset.source()
);

if (childChunk.id !== null) {
hashByChunkId.set(childChunk.id, integrity);
if (assets[sourcePath]) {
this.warnIfHotUpdate(compilation, assets[sourcePath].source());
const newAsset = this.replaceAsset(
compilation.compiler,
assets,
hashByChunkId,
sourcePath
);
const integrity = computeIntegrity(
this.options.hashFuncNames,
newAsset.source()
);

if (childChunk.id !== null) {
hashByChunkId.set(childChunk.id, integrity);
}
this.updateAssetIntegrity(sourcePath, integrity);
compilation.updateAsset(
sourcePath,
(x) => x,
(assetInfo) =>
assetInfo && {
...assetInfo,
contenthash: Array.isArray(assetInfo.contenthash)
? [...new Set([...assetInfo.contenthash, integrity])]
: assetInfo.contenthash
? [assetInfo.contenthash, integrity]
: integrity,
}
);
} else {
this.warnOnce(
compilation,
`No asset found for source path '${sourcePath}', options are ${Object.keys(
assets
).join(", ")}`
);
}
this.updateAssetIntegrity(sourcePath, integrity);
compilation.updateAsset(
sourcePath,
(x) => x,
(assetInfo) =>
assetInfo && {
...assetInfo,
contenthash: Array.isArray(assetInfo.contenthash)
? [...new Set([...assetInfo.contenthash, integrity])]
: assetInfo.contenthash
? [assetInfo.contenthash, integrity]
: integrity,
}
);
} else {
this.warnOnce(
compilation,
`No asset found for source path '${sourcePath}', options are ${Object.keys(
assets
).join(", ")}`
);
}
});
});
};

Expand Down Expand Up @@ -746,16 +747,21 @@ export class SubresourceIntegrityPlugin {
);

compiler.hooks.compilation.tap(
"DefaultStatsFactoryPlugin",
thisPluginName,
(compilation: Compilation) => {
compilation.hooks.statsFactory.tap(thisPluginName, (statsFactory) => {
statsFactory.hooks.extract
.for("asset")
.tap(thisPluginName, (object, asset) => {
if (this.assetIntegrity.has(asset.name)) {
(object as any).integrity = String(
this.assetIntegrity.get(asset.name)
);
const contenthash = asset.info?.contenthash;
if (contenthash) {
const shaHashes = (Array.isArray(contenthash)
? contenthash
: [contenthash]
).filter((hash: any) => String(hash).match(/^sha[0-9]+-/));
if (shaHashes.length > 0) {
(object as any).integrity = shaHashes.join(" ");
}
}
});
});
Expand Down