Skip to content

Commit 359cb5c

Browse files
authored
fix(rsa): Use dist filenames when registering server references (#11417)
1 parent c93560f commit 359cb5c

File tree

5 files changed

+94
-74
lines changed

5 files changed

+94
-74
lines changed

packages/vite/src/plugins/__tests__/vite-plugin-rsc-transform-server.function-scope.test.ts

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ afterAll(() => {
1919
process.env.RWJS_CWD = RWJS_CWD
2020
})
2121

22-
function getPluginTransform() {
23-
const plugin = rscTransformUseServerPlugin()
22+
function getPluginTransform(serverEntryFiles: Record<string, string>) {
23+
const plugin = rscTransformUseServerPlugin('some/dist/path', serverEntryFiles)
2424

2525
if (typeof plugin.transform !== 'function') {
2626
throw new Error('Plugin does not have a transform function')
@@ -33,12 +33,14 @@ function getPluginTransform() {
3333
return plugin.transform.bind({} as TransformPluginContext)
3434
}
3535

36-
const pluginTransform = getPluginTransform()
36+
const id = 'rw-app/web/src/some/path/to/actions.ts'
37+
const pluginTransform = getPluginTransform({
38+
'rsa-actions.ts-0': id,
39+
})
3740

3841
describe('rscTransformUseServerPlugin function scoped "use server"', () => {
3942
describe('top-level exports', () => {
4043
it('should handle named function', async () => {
41-
const id = 'some/path/to/actions.ts'
4244
const input = `
4345
import fs from 'node:fs'
4446
@@ -65,12 +67,11 @@ describe('rscTransformUseServerPlugin function scoped "use server"', () => {
6567
await fs.promises.writeFile('settings.json', \`{ "delay": \${formData.get('delay')} }\`);
6668
}
6769
import { registerServerReference } from "react-server-dom-webpack/server";
68-
registerServerReference(formAction, "some/path/to/actions.ts", "formAction");"
70+
registerServerReference(formAction, "some/dist/path/assets/rsa-actions.ts-0.mjs", "formAction");"
6971
`)
7072
})
7173

7274
it('should handle arrow function', async () => {
73-
const id = 'some/path/to/actions.ts'
7475
const input = `
7576
import fs from 'node:fs'
7677
@@ -97,12 +98,11 @@ describe('rscTransformUseServerPlugin function scoped "use server"', () => {
9798
await fs.promises.writeFile('settings.json', \`{ "delay": \${formData.get('delay')} }\`);
9899
};
99100
import { registerServerReference } from "react-server-dom-webpack/server";
100-
registerServerReference(formAction, "some/path/to/actions.ts", "formAction");"
101+
registerServerReference(formAction, "some/dist/path/assets/rsa-actions.ts-0.mjs", "formAction");"
101102
`)
102103
})
103104

104105
it('should handle default exported named function', async () => {
105-
const id = 'some/path/to/actions.ts'
106106
const input = `
107107
import fs from 'node:fs'
108108
@@ -129,12 +129,11 @@ describe('rscTransformUseServerPlugin function scoped "use server"', () => {
129129
await fs.promises.writeFile('settings.json', \`{ "delay": \${formData.get('delay')} }\`);
130130
}
131131
import { registerServerReference } from "react-server-dom-webpack/server";
132-
registerServerReference(formAction, "some/path/to/actions.ts", "default");"
132+
registerServerReference(formAction, "some/dist/path/assets/rsa-actions.ts-0.mjs", "default");"
133133
`)
134134
})
135135

136136
it('should handle exports with two consts', async () => {
137-
const id = 'some/path/to/actions.ts'
138137
const input = `
139138
import fs from 'node:fs'
140139
@@ -162,13 +161,12 @@ describe('rscTransformUseServerPlugin function scoped "use server"', () => {
162161
await fs.promises.writeFile('settings.json', \`{ "delay": \${formData.get('delay')} }\`);
163162
};
164163
import { registerServerReference } from "react-server-dom-webpack/server";
165-
registerServerReference(fortyTwo, "some/path/to/actions.ts", "fortyTwo");
166-
registerServerReference(formAction, "some/path/to/actions.ts", "formAction");"
164+
registerServerReference(fortyTwo, "some/dist/path/assets/rsa-actions.ts-0.mjs", "fortyTwo");
165+
registerServerReference(formAction, "some/dist/path/assets/rsa-actions.ts-0.mjs", "formAction");"
167166
`)
168167
})
169168

170169
it('should handle named function and arrow function with separate export', async () => {
171-
const id = 'some/path/to/actions.ts'
172170
const input = `
173171
import fs from 'node:fs'
174172
@@ -213,15 +211,14 @@ describe('rscTransformUseServerPlugin function scoped "use server"', () => {
213211
await fs.promises.writeFile('settings.json', \`{ "delay": \${formData.get('delay')} }\`);
214212
};
215213
import { registerServerReference } from "react-server-dom-webpack/server";
216-
registerServerReference(formAction, "some/path/to/actions.ts", "formAction");
217-
registerServerReference(arrowAction, "some/path/to/actions.ts", "arrowAction");"
214+
registerServerReference(formAction, "some/dist/path/assets/rsa-actions.ts-0.mjs", "formAction");
215+
registerServerReference(arrowAction, "some/dist/path/assets/rsa-actions.ts-0.mjs", "arrowAction");"
218216
`)
219217
})
220218

221219
it.todo(
222220
"should handle named function and 'let' arrow function with separate export",
223221
async () => {
224-
const id = 'some/path/to/actions.ts'
225222
const input = `
226223
import fs from 'node:fs'
227224
@@ -280,14 +277,13 @@ describe('rscTransformUseServerPlugin function scoped "use server"', () => {
280277
// Not 'use server' anymore
281278
};
282279
import { registerServerReference } from "react-server-dom-webpack/server";
283-
registerServerReference(formAction, "some/path/to/actions.ts", "formAction");
284-
if (typeof letArrowFunction === "function") registerServerReference(letArrowAction, "some/path/to/actions.ts", "letArrowAction");"
280+
registerServerReference(formAction, "some/dist/path/assets/rsa-actions.ts-0.mjs", "formAction");
281+
if (typeof letArrowFunction === "function") registerServerReference(letArrowAction, "some/dist/path/assets/rsa-actions.ts-0.mjs", "letArrowAction");"
285282
`)
286283
},
287284
)
288285

289286
it('should handle separate renamed export', async () => {
290-
const id = 'some/path/to/actions.ts'
291287
const input = `
292288
import fs from 'node:fs'
293289
@@ -327,13 +323,12 @@ describe('rscTransformUseServerPlugin function scoped "use server"', () => {
327323
};
328324
export { formAction as fA, arrowAction };
329325
import { registerServerReference } from "react-server-dom-webpack/server";
330-
registerServerReference(formAction, "some/path/to/actions.ts", "fA");
331-
registerServerReference(arrowAction, "some/path/to/actions.ts", "arrowAction");"
326+
registerServerReference(formAction, "some/dist/path/assets/rsa-actions.ts-0.mjs", "fA");
327+
registerServerReference(arrowAction, "some/dist/path/assets/rsa-actions.ts-0.mjs", "arrowAction");"
332328
`)
333329
})
334330

335331
it.todo('should handle default exported arrow function', async () => {
336-
const id = 'some/path/to/actions.ts'
337332
const input = `
338333
import fs from 'node:fs'
339334
@@ -365,14 +360,13 @@ describe('rscTransformUseServerPlugin function scoped "use server"', () => {
365360
}
366361
367362
import {registerServerReference} from "react-server-dom-webpack/server";
368-
if (typeof formAction === "function") registerServerReference(formAction,"some/path/to/actions.ts","formAction");
363+
if (typeof formAction === "function") registerServerReference(formAction,"some/dist/path/assets/rsa-actions.ts-0.mjs","formAction");
369364
"
370365
`)
371366
})
372367

373368
describe('without "use server"', () => {
374369
it('should not register named function', async () => {
375-
const id = 'some/path/to/actions.ts'
376370
const input = `
377371
import fs from 'node:fs'
378372
@@ -401,7 +395,6 @@ describe('rscTransformUseServerPlugin function scoped "use server"', () => {
401395
})
402396

403397
it('should not register arrow function', async () => {
404-
const id = 'some/path/to/actions.ts'
405398
const input = `
406399
import fs from 'node:fs'
407400
@@ -430,7 +423,6 @@ describe('rscTransformUseServerPlugin function scoped "use server"', () => {
430423
})
431424

432425
it('should not register default exported named function', async () => {
433-
const id = 'some/path/to/actions.ts'
434426
const input = `
435427
import fs from 'node:fs'
436428
@@ -459,7 +451,6 @@ describe('rscTransformUseServerPlugin function scoped "use server"', () => {
459451
})
460452

461453
it('should not register exports with two consts', async () => {
462-
const id = 'some/path/to/actions.ts'
463454
const input = `
464455
import fs from 'node:fs'
465456
@@ -483,12 +474,11 @@ describe('rscTransformUseServerPlugin function scoped "use server"', () => {
483474
await fs.promises.writeFile('settings.json', \`{ "delay": \${formData.get('delay')} }\`);
484475
};
485476
import { registerServerReference } from "react-server-dom-webpack/server";
486-
registerServerReference(formAction, "some/path/to/actions.ts", "formAction");"
477+
registerServerReference(formAction, "some/dist/path/assets/rsa-actions.ts-0.mjs", "formAction");"
487478
`)
488479
})
489480

490481
it('should not register named function and arrow function with separate export', async () => {
491-
const id = 'some/path/to/actions.ts'
492482
const input = `
493483
import fs from 'node:fs'
494484
@@ -525,7 +515,6 @@ describe('rscTransformUseServerPlugin function scoped "use server"', () => {
525515
})
526516

527517
it('should not register separate renamed export', async () => {
528-
const id = 'some/path/to/actions.ts'
529518
const input = `
530519
import fs from 'node:fs'
531520
@@ -564,7 +553,6 @@ describe('rscTransformUseServerPlugin function scoped "use server"', () => {
564553
})
565554

566555
it('should not register default exported arrow function', async () => {
567-
const id = 'some/path/to/actions.ts'
568556
const input = `
569557
import fs from 'node:fs'
570558
@@ -597,7 +585,11 @@ describe('rscTransformUseServerPlugin function scoped "use server"', () => {
597585

598586
describe('actions inside components', async () => {
599587
it('should handle self-contained named function inside default exported component', async () => {
600-
const id = 'some/path/to/Component.tsx'
588+
const id = 'rw-app/web/src/some/path/to/Component.tsx'
589+
const pluginTransform = getPluginTransform({
590+
'rsa-Component.tsx-0': id,
591+
})
592+
601593
const input = `
602594
import fs from 'node:fs'
603595
@@ -640,7 +632,7 @@ describe('rscTransformUseServerPlugin function scoped "use server"', () => {
640632
641633
await fs.promises.writeFile('settings.json', \`{ "delay": \${formData.get('delay')} }\`);
642634
}
643-
registerServerReference(__rwjs__rsa0_formAction, "some/path/to/Component.tsx", "__rwjs__rsa0_formAction");"
635+
registerServerReference(__rwjs__rsa0_formAction, "some/dist/path/assets/rsa-Component.tsx-0.mjs", "__rwjs__rsa0_formAction");"
644636
`)
645637
})
646638

0 commit comments

Comments
 (0)