Skip to content

Commit 76f1d41

Browse files
committed
fix(rsc): expose wrapped directive hoists
1 parent 8595871 commit 76f1d41

4 files changed

Lines changed: 36 additions & 18 deletions

File tree

packages/plugin-rsc/src/plugins/server-function-directives.test.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,15 @@ export async function getData() {
109109
expect(result?.code).toMatchInlineSnapshot(`
110110
"/* __vite_rsc_server_function_directives__ */
111111
import * as $$ReactServer from "/rsc-runtime.js";
112+
export const $$hoist_e9c2205b6101_0_getData = /* #__PURE__ */ $$ReactServer.registerServerReference(cache($$hoist_e9c2205b6101_0_getData$$impl, "use cache", "inline"), "53eb073e2100", "$$hoist_e9c2205b6101_0_getData");
112113
113-
export const getData = /* #__PURE__ */ $$ReactServer.registerServerReference(cache($$hoist_e9c2205b6101_0_getData, "use cache", "inline"), "53eb073e2100", "$$hoist_e9c2205b6101_0_getData");
114+
export const getData = $$hoist_e9c2205b6101_0_getData;
114115
115-
;export async function $$hoist_e9c2205b6101_0_getData() {
116+
;async function $$hoist_e9c2205b6101_0_getData$$impl() {
116117
"use cache";
117118
return 1;
118119
};
119-
/* #__PURE__ */ Object.defineProperty($$hoist_e9c2205b6101_0_getData, "name", { value: "getData" });
120+
/* #__PURE__ */ Object.defineProperty($$hoist_e9c2205b6101_0_getData$$impl, "name", { value: "getData" });
120121
"
121122
`)
122123
expect(
@@ -147,16 +148,17 @@ export async function outer(value) {
147148
"/* __vite_rsc_server_function_directives__ */
148149
import * as $$ReactServer from "/rsc-runtime.js";
149150
import * as __vite_rsc_encryption_runtime from "/encryption-runtime.js";
151+
export const $$hoist_ab3ae7af371a_0_cached = /* #__PURE__ */ $$ReactServer.registerServerReference((($$wrapped) => async ($$encoded, ...$$args) => $$wrapped(...await __vite_rsc_encryption_runtime.decryptActionBoundArgs($$encoded), ...$$args))(cache($$hoist_ab3ae7af371a_0_cached$$impl)), "53eb073e2100", "$$hoist_ab3ae7af371a_0_cached");
150152
151153
export async function outer(value) {
152-
return /* #__PURE__ */ $$ReactServer.registerServerReference((($$wrapped) => async ($$encoded, ...$$args) => $$wrapped(...await __vite_rsc_encryption_runtime.decryptActionBoundArgs($$encoded), ...$$args))(cache($$hoist_ab3ae7af371a_0_cached)), "53eb073e2100", "$$hoist_ab3ae7af371a_0_cached").bind(null, __vite_rsc_encryption_runtime.encryptActionBoundArgs([value]));
154+
return $$hoist_ab3ae7af371a_0_cached.bind(null, __vite_rsc_encryption_runtime.encryptActionBoundArgs([value]));
153155
}
154156
155-
;export async function $$hoist_ab3ae7af371a_0_cached(value) {
157+
;async function $$hoist_ab3ae7af371a_0_cached$$impl(value) {
156158
"use cache";
157159
return value;
158160
};
159-
/* #__PURE__ */ Object.defineProperty($$hoist_ab3ae7af371a_0_cached, "name", { value: "cached" });
161+
/* #__PURE__ */ Object.defineProperty($$hoist_ab3ae7af371a_0_cached$$impl, "name", { value: "cached" });
160162
"
161163
`)
162164
expect(wrap).toHaveBeenCalledWith(

packages/plugin-rsc/src/plugins/server-function-directives.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ export function vitePluginServerFunctionDirectives(options: Options): Plugin {
347347
return `__vite_rsc_encryption_runtime.encryptActionBoundArgs(${value})`
348348
},
349349
stableName: true,
350+
exportWrappedHoist: !useServerBoundary,
350351
detectUseServerModule: false,
351352
rejectForbiddenExpressions: true,
352353
})

packages/plugin-rsc/src/transforms/hoist.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export function transformHoistInlineDirective(
3535
encode?: (value: string) => string
3636
decode?: (value: string) => string
3737
noExport?: boolean
38+
exportWrappedHoist?: boolean
3839
stableName?: boolean
3940
rejectForbiddenExpressions?: boolean
4041
},
@@ -163,32 +164,44 @@ export function transformHoistInlineDirective(
163164
const newName =
164165
`$$hoist_${nameKey}` + (originalName ? `_${originalName}` : '')
165166
names.push(newName)
167+
const implementationName = options.exportWrappedHoist
168+
? `${newName}$$impl`
169+
: newName
166170
output.update(
167171
node.start,
168172
node.body.start,
169-
`\n;${options.noExport ? '' : 'export '}${
173+
`\n;${options.noExport || options.exportWrappedHoist ? '' : 'export '}${
170174
node.async ? 'async ' : ''
171-
}function${node.generator ? '*' : ''} ${newName}(${newParams}) `,
175+
}function${node.generator ? '*' : ''} ${implementationName}(${newParams}) `,
172176
)
173177
output.appendLeft(
174178
node.end,
175-
`;\n/* #__PURE__ */ Object.defineProperty(${newName}, "name", { value: ${JSON.stringify(
179+
`;\n/* #__PURE__ */ Object.defineProperty(${implementationName}, "name", { value: ${JSON.stringify(
176180
originalName,
177181
)} });\n`,
178182
)
179183
output.move(node.start, node.end, input.length)
180184

181185
// replace original declartion with action register + bind
182-
let newCode = `/* #__PURE__ */ ${runtime(newName, newName, {
183-
directiveMatch: match,
184-
hasBoundArgs: bindVars.length > 0,
185-
parameters: {
186-
count: node.params.length,
187-
hasRest: node.params.some(
188-
(parameter) => parameter.type === 'RestElement',
189-
),
186+
let wrappedCode = `/* #__PURE__ */ ${runtime(
187+
implementationName,
188+
newName,
189+
{
190+
directiveMatch: match,
191+
hasBoundArgs: bindVars.length > 0,
192+
parameters: {
193+
count: node.params.length,
194+
hasRest: node.params.some(
195+
(parameter) => parameter.type === 'RestElement',
196+
),
197+
},
190198
},
191-
})}`
199+
)}`
200+
let newCode = wrappedCode
201+
if (options.exportWrappedHoist) {
202+
output.prepend(`export const ${newName} = ${wrappedCode};\n`)
203+
newCode = newName
204+
}
192205
if (bindVars.length > 0) {
193206
const bindArgs = options.encode
194207
? options.encode('[' + bindVars.map((b) => b.expr).join(', ') + ']')

packages/plugin-rsc/src/transforms/server-action.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export function transformServerActionServer(
2626
encode?: (value: string) => string
2727
decode?: (value: string) => string
2828
stableName?: boolean
29+
exportWrappedHoist?: boolean
2930
preserveModuleDirective?: boolean
3031
detectUseServerModule?: boolean
3132
rejectForbiddenExpressions?: boolean
@@ -80,6 +81,7 @@ export function transformServerActionServer(
8081
encode: options.encode,
8182
decode: options.decode,
8283
stableName: options.stableName,
84+
exportWrappedHoist: options.exportWrappedHoist,
8385
rejectForbiddenExpressions: options.rejectForbiddenExpressions,
8486
})
8587
}

0 commit comments

Comments
 (0)