Skip to content

Commit 5878f78

Browse files
authored
feat: add integrity field to manifest when sri is enabled (#6634)
1 parent 5a7e35e commit 5878f78

File tree

9 files changed

+78
-7
lines changed

9 files changed

+78
-7
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { expect, getFileContent, rspackTest } from '@e2e/helper';
2+
import type { ManifestData } from '@rsbuild/core';
3+
4+
rspackTest(
5+
'should generate manifest file with integrity in build',
6+
async ({ build }) => {
7+
const rsbuild = await build();
8+
const files = rsbuild.getDistFiles();
9+
const manifestContent = getFileContent(files, 'manifest.json');
10+
const manifest = JSON.parse(manifestContent) as ManifestData;
11+
manifest.allFiles.forEach((item) => {
12+
if (item.endsWith('.js')) {
13+
expect(manifest.integrity[item]).toBeTruthy();
14+
}
15+
});
16+
},
17+
);
18+
19+
rspackTest(
20+
'should generate manifest file with integrity in dev',
21+
async ({ dev }) => {
22+
const rsbuild = await dev();
23+
const files = rsbuild.getDistFiles();
24+
const manifestContent = getFileContent(files, 'manifest.json');
25+
const manifest = JSON.parse(manifestContent) as ManifestData;
26+
manifest.allFiles.forEach((item) => {
27+
if (item.endsWith('.js')) {
28+
expect(manifest.integrity[item]).toBeTruthy();
29+
}
30+
});
31+
},
32+
);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { defineConfig } from '@rsbuild/core';
2+
3+
export default defineConfig({
4+
output: {
5+
manifest: true,
6+
filenameHash: false,
7+
},
8+
security: {
9+
sri: {
10+
enable: true,
11+
},
12+
},
13+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import React from 'react';
2+
3+
console.log('hello!', React);

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
"reduce-configs": "^1.1.1",
9494
"rslog": "^1.3.0",
9595
"rspack-chain": "^1.4.1",
96-
"rspack-manifest-plugin": "5.1.0",
96+
"rspack-manifest-plugin": "5.2.0",
9797
"sirv": "^3.0.2",
9898
"stacktrace-parser": "^0.1.11",
9999
"style-loader": "3.3.4",

packages/core/src/plugins/manifest.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,13 @@ const generateManifest =
3434
const chunkEntries = new Map<string, FileDescriptor[]>();
3535
const licenseMap = new Map<string, string>();
3636
const publicPath = getPublicPathFromCompiler(compilation);
37+
const integrity: Record<string, string> = {};
3738

3839
const allFiles = files.map((file) => {
40+
if (file.integrity) {
41+
integrity[file.path] = file.integrity;
42+
}
43+
3944
if (file.chunk) {
4045
const entryNames = recursiveChunkEntryNames(file.chunk);
4146

@@ -144,6 +149,7 @@ const generateManifest =
144149
const manifestData: ManifestData = {
145150
allFiles,
146151
entries: manifestEntries,
152+
integrity,
147153
};
148154

149155
if (manifestOptions.generate) {

packages/core/src/types/config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,12 @@ export type ManifestData = {
11791179
entries: {
11801180
[entryName: string]: ManifestByEntry;
11811181
};
1182+
/**
1183+
* Subresource Integrity (SRI) hashes for emitted assets.
1184+
* The key is the asset file path, and the value is its integrity hash.
1185+
* This field is available only when the `security.sri` option is enabled.
1186+
*/
1187+
integrity: Record<string, string>;
11821188
};
11831189

11841190
export type ManifestObjectConfig = {

pnpm-lock.yaml

Lines changed: 5 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

website/docs/en/config/output/manifest.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ type ManifestList = {
9191
*/
9292
assets?: FilePath[];
9393
};
94+
/**
95+
* Subresource Integrity (SRI) hashes for emitted assets.
96+
* The key is the asset file path, and the value is its integrity hash.
97+
* This field is available only when the `security.sri` option is enabled.
98+
*/
99+
integrity: Record<string, string>;
94100
};
95101
};
96102
```

website/docs/zh/config/output/manifest.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ type ManifestList = {
9191
*/
9292
assets?: FilePath[];
9393
};
94+
/**
95+
* 所有产物文件的子资源完整性(SRI)哈希值
96+
* key 为文件路径,value 为对应的完整性哈希
97+
* 仅在启用 `security.sri` 选项时才会生成该字段
98+
*/
99+
integrity: Record<string, string>;
94100
};
95101
};
96102
```

0 commit comments

Comments
 (0)