Skip to content

Commit fc7cf19

Browse files
authored
Add lazyHashes option to define integrity hashes only in direct parents of assets (#172)
Also use Node 12 tsconfig base, as the "engine" field in package.json requires.
1 parent 3d93750 commit fc7cf19

File tree

31 files changed

+930
-82
lines changed

31 files changed

+930
-82
lines changed

examples/hwp-custom-template/webpack.config.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,16 @@ module.exports = {
4040

4141
const jsIntegrity = stats
4242
.toJson()
43-
.assets.find((asset) => asset.name === "subdir/bundle.js")
44-
.integrity;
43+
.assets.find(
44+
(asset) => asset.name === "subdir/bundle.js"
45+
).integrity;
4546
expect(jsIntegrity).toMatch(/^sha/);
4647

4748
const cssIntegrity = stats
4849
.toJson()
49-
.assets.find((asset) => asset.name === "subdir/styles.css")
50-
.integrity;
50+
.assets.find(
51+
(asset) => asset.name === "subdir/styles.css"
52+
).integrity;
5153
expect(cssIntegrity).toMatch(/^sha/);
5254

5355
return new Promise((resolve, reject) => {

examples/lazy-hashes-cycles/1.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import("./2.js");
2+
import("./leaf.js");
3+
export default {
4+
chunk: 1,
5+
};

examples/lazy-hashes-cycles/2.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import("./3.js");
2+
export default {
3+
chunk: 2,
4+
};

examples/lazy-hashes-cycles/3.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import("./1.js");
2+
export default {
3+
chunk: 3,
4+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Sourcemap and code splitting
2+
3+
Test case for lazy hashes where there is a chunk dependency cycle
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import("./1.js");
2+
import("./2.js");
3+
console.log("ok");
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
chunk: "leaf",
3+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "lazy-hashes-cycles",
3+
"description": "Test case for lazy hashes where there is a chunk dependency cycle",
4+
"version": "1.0.0",
5+
"license": "MIT",
6+
"private": true,
7+
"devDependencies": {
8+
"expect": "^26.6.2",
9+
"html-webpack-plugin": ">= 5.0.0-beta.1",
10+
"nyc": "*",
11+
"webpack": "^5.44.0",
12+
"webpack-cli": "4",
13+
"webpack-subresource-integrity": "*",
14+
"wsi-test-helper": "*"
15+
}
16+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
const { SubresourceIntegrityPlugin } = require("webpack-subresource-integrity");
2+
const HtmlWebpackPlugin = require("html-webpack-plugin");
3+
const { readFileSync } = require("fs");
4+
const { join } = require("path");
5+
const expect = require("expect");
6+
7+
module.exports = {
8+
entry: {
9+
index: "./index.js",
10+
},
11+
output: {
12+
crossOriginLoading: "anonymous",
13+
},
14+
plugins: [
15+
new SubresourceIntegrityPlugin({
16+
enabled: true,
17+
hashLoading: "lazy",
18+
}),
19+
new HtmlWebpackPlugin(),
20+
{
21+
apply: (compiler) => {
22+
compiler.hooks.done.tap("wsi-test", (stats) => {
23+
if (stats && stats.hasErrors()) {
24+
throw new Error(
25+
stats
26+
.toJson()
27+
.errors.map((error) => error.message)
28+
.join(", ")
29+
);
30+
}
31+
function getSriHashes(chunkName, isEntry) {
32+
const fileContent = readFileSync(
33+
join(__dirname, "dist", `${chunkName}.js`),
34+
"utf-8"
35+
);
36+
const sriRegex = new RegExp(
37+
`${
38+
isEntry
39+
? "(\\w+|__webpack_require__)\\.sriHashes="
40+
: "Object.assign\\((\\w+|__webpack_require__)\\.sriHashes,"
41+
}(?<sriHashJson>\{.*?\})`
42+
);
43+
const regexMatch = sriRegex.exec(fileContent);
44+
const sriHashJson = regexMatch
45+
? regexMatch.groups.sriHashJson
46+
: null;
47+
if (!sriHashJson) {
48+
return null;
49+
}
50+
try {
51+
// The hashes are not *strict* JSON, since they can have numerical keys
52+
return JSON.parse(
53+
sriHashJson.replace(/\d+(?=:)/g, (num) => `"${num}"`)
54+
);
55+
} catch (err) {
56+
throw new Error(
57+
`Could not parse SRI hashes \n\t${sriHashJson}\n in asset: ${err}`
58+
);
59+
}
60+
}
61+
62+
const indexHashes = getSriHashes("index", true);
63+
expect(Object.keys(indexHashes).length).toEqual(3);
64+
65+
expect(
66+
stats
67+
.toJson()
68+
.assets.filter(({ name }) => /\.js$/.test(name))
69+
.every(({ integrity }) => !!integrity)
70+
).toEqual(true);
71+
});
72+
},
73+
},
74+
],
75+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import("./leaf.js");
2+
export default {
3+
chunk: 1,
4+
};

0 commit comments

Comments
 (0)