Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions packages/plugin-rsc/src/transforms/hoist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,35 @@ async function cached(value, { offset }, ...rest) {
])
})

it('exposes closure captures to the runtime', async () => {
const input = `
function Page({ tenant }, label) {
async function capturing(query) {
"use cache";
return [tenant.id, tenant.region, label, query];
}
async function standalone() {
"use cache";
return 1;
}
}
`
const ast = await parseAstAsync(input)
const bindVars: Record<string, string[]> = {}
transformHoistInlineDirective(input, ast, {
directive: 'use cache',
runtime: (value, name, meta) => {
bindVars[name] = meta.bindVars
return value
},
})

expect(bindVars).toEqual({
$$hoist_0_capturing: ['tenant', 'label'],
$$hoist_1_standalone: [],
})
})

it('ignores strings outside a function directive prologue', async () => {
const input = `
async function initialized() {
Expand Down
8 changes: 7 additions & 1 deletion packages/plugin-rsc/src/transforms/hoist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export type TransformHoistInlineDirectiveMeta = {
directiveMatch: RegExpMatchArray
/** Original source function before closure captures are added as parameters. */
valueNode: ArrowFunctionExpression | FunctionDeclaration | FunctionExpression
/** Closure captures, in the order they are bound ahead of the original parameters. */
bindVars: string[]
}

export type TransformHoistInlineDirectiveResult = {
Expand Down Expand Up @@ -226,7 +228,11 @@ export function transformHoistInlineDirective(
const runtimeCode = `/* #__PURE__ */ ${runtime(
implementationName,
newName,
{ directiveMatch: match, valueNode: node },
{
directiveMatch: match,
valueNode: node,
bindVars: bindVars.map((b) => b.root),
},
)}`
if (options.hoistRuntime) {
runtimeHoists.push(
Expand Down
Loading