Skip to content

Commit 117dd0f

Browse files
authored
fix(core): add hash to rsf{n} (#1203)
close #1202
1 parent c0e1f3f commit 117dd0f

File tree

3 files changed

+34
-39
lines changed

3 files changed

+34
-39
lines changed

packages/waku/src/lib/builder/build.ts

+12-13
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,12 @@ const analyzeEntries = async (rootDir: string, config: ResolvedConfig) => {
101101
const wakuMinimalClientDist = decodeFilePathFromAbsolute(
102102
joinPath(fileURLToFilePath(import.meta.url), '../../../minimal/client.js'),
103103
);
104-
const clientFileSet = new Set<string>([
105-
wakuClientDist,
106-
wakuMinimalClientDist,
104+
const clientFileMap = new Map<string, string>([
105+
// FIXME 'lib' should be the real hash
106+
[wakuClientDist, 'lib'],
107+
[wakuMinimalClientDist, 'lib'],
107108
]);
108-
const serverFileSet = new Set<string>();
109-
const fileHashMap = new Map<string, string>();
109+
const serverFileMap = new Map<string, string>();
110110
const moduleFileMap = new Map<string, string>(); // module id -> full path
111111
const pagesDirPath = joinPath(rootDir, config.srcDir, config.pagesDir);
112112
if (existsSync(pagesDirPath)) {
@@ -131,9 +131,8 @@ const analyzeEntries = async (rootDir: string, config: ResolvedConfig) => {
131131
plugins: [
132132
rscAnalyzePlugin({
133133
isClient: false,
134-
clientFileSet,
135-
serverFileSet,
136-
fileHashMap,
134+
clientFileMap,
135+
serverFileMap,
137136
}),
138137
rscManagedPlugin({ ...config, addEntriesToInput: true }),
139138
...deployPlugins(config),
@@ -161,8 +160,8 @@ const analyzeEntries = async (rootDir: string, config: ResolvedConfig) => {
161160
),
162161
);
163162
const clientEntryFiles = Object.fromEntries(
164-
Array.from(clientFileSet).map((fname, i) => [
165-
`${DIST_ASSETS}/rsc${i}-${fileHashMap.get(fname) || 'lib'}`, // FIXME 'lib' is a workaround to avoid `undefined`
163+
Array.from(clientFileMap).map(([fname, hash], i) => [
164+
`${DIST_ASSETS}/rsc${i}-${hash}`,
166165
fname,
167166
]),
168167
);
@@ -171,7 +170,7 @@ const analyzeEntries = async (rootDir: string, config: ResolvedConfig) => {
171170
{
172171
mode: 'production',
173172
plugins: [
174-
rscAnalyzePlugin({ isClient: true, serverFileSet }),
173+
rscAnalyzePlugin({ isClient: true, serverFileMap }),
175174
rscManagedPlugin({ ...config, addMainToInput: true }),
176175
...deployPlugins(config),
177176
],
@@ -194,8 +193,8 @@ const analyzeEntries = async (rootDir: string, config: ResolvedConfig) => {
194193
),
195194
);
196195
const serverEntryFiles = Object.fromEntries(
197-
Array.from(serverFileSet).map((fname, i) => [
198-
`${DIST_ASSETS}/rsf${i}`,
196+
Array.from(serverFileMap).map(([fname, hash], i) => [
197+
`${DIST_ASSETS}/rsf${i}-${hash}`,
199198
fname,
200199
]),
201200
);

packages/waku/src/lib/plugins/vite-plugin-rsc-analyze.ts

+9-11
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,12 @@ export function rscAnalyzePlugin(
7070
opts:
7171
| {
7272
isClient: true;
73-
serverFileSet: Set<string>;
73+
serverFileMap: Map<string, string>;
7474
}
7575
| {
7676
isClient: false;
77-
clientFileSet: Set<string>;
78-
serverFileSet: Set<string>;
79-
fileHashMap: Map<string, string>;
77+
clientFileMap: Map<string, string>;
78+
serverFileMap: Map<string, string>;
8079
},
8180
): Plugin {
8281
const rscTransform = rscTransformPlugin({
@@ -96,25 +95,24 @@ export function rscAnalyzePlugin(
9695
item.expression.type === 'StringLiteral'
9796
) {
9897
if (!opts.isClient && item.expression.value === 'use client') {
99-
opts.clientFileSet.add(id);
100-
opts.fileHashMap.set(id, await hash(code));
98+
opts.clientFileMap.set(id, await hash(code));
10199
} else if (item.expression.value === 'use server') {
102-
opts.serverFileSet.add(id);
100+
opts.serverFileMap.set(id, await hash(code));
103101
}
104102
}
105103
}
106104
if (
107105
!opts.isClient &&
108-
!opts.clientFileSet.has(id) &&
109-
!opts.serverFileSet.has(id) &&
106+
!opts.clientFileMap.has(id) &&
107+
!opts.serverFileMap.has(id) &&
110108
code.includes('use server') &&
111109
containsServerFunction(mod)
112110
) {
113-
opts.serverFileSet.add(id);
111+
opts.serverFileMap.set(id, await hash(code));
114112
}
115113
}
116114
// Avoid walking after the client boundary
117-
if (!opts.isClient && opts.clientFileSet.has(id)) {
115+
if (!opts.isClient && opts.clientFileMap.has(id)) {
118116
// TODO this isn't efficient. let's refactor it in the future.
119117
return (
120118
rscTransform as typeof rscTransform & { handler: undefined }

packages/waku/tests/vite-plugin-rsc-analyze.test.ts

+13-15
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ async function runTest(
3939
expectedClientFileSet: Set<string>,
4040
expectedServerFileSet: Set<string>,
4141
) {
42-
const clientFileSet = new Set<string>();
43-
const serverFileSet = new Set<string>();
44-
const fileHashMap = new Map<string, string>();
42+
const clientFileMap = new Map<string, string>();
43+
const serverFileMap = new Map<string, string>();
4544
await build({
4645
root: root,
4746
logLevel: 'silent',
@@ -57,28 +56,27 @@ async function runTest(
5756
isClient
5857
? rscAnalyzePlugin({
5958
isClient: true,
60-
serverFileSet,
59+
serverFileMap,
6160
})
6261
: rscAnalyzePlugin({
6362
isClient: false,
64-
clientFileSet,
65-
serverFileSet,
66-
fileHashMap,
63+
clientFileMap,
64+
serverFileMap,
6765
}),
6866
],
6967
});
7068
// remove the base path
71-
[...clientFileSet].forEach((value) => {
72-
clientFileSet.delete(value);
73-
clientFileSet.add(path.relative(root, value));
69+
[...clientFileMap].forEach(([value]) => {
70+
clientFileMap.delete(value);
71+
clientFileMap.set(path.relative(root, value), 'hash');
7472
});
75-
[...serverFileSet].forEach((value) => {
76-
serverFileSet.delete(value);
77-
serverFileSet.add(path.relative(root, value));
73+
[...serverFileMap].forEach(([value]) => {
74+
serverFileMap.delete(value);
75+
serverFileMap.set(path.relative(root, value), 'hash');
7876
});
7977

80-
expect(clientFileSet).toEqual(expectedClientFileSet);
81-
expect(serverFileSet).toEqual(expectedServerFileSet);
78+
expect(new Set(clientFileMap.keys())).toEqual(expectedClientFileSet);
79+
expect(new Set(serverFileMap.keys())).toEqual(expectedServerFileSet);
8280
}
8381

8482
describe('vite-plugin-rsc-analyze', () => {

0 commit comments

Comments
 (0)