Skip to content

Commit c217ebc

Browse files
committed
feat(rsc): configure module directive async validation
1 parent 49a0575 commit c217ebc

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,18 @@ export async function action() {
321321
).rejects.toThrow('non async function')
322322
})
323323

324+
it('supports separate async validation for inline and module directives', async () => {
325+
const { run } = createHarness([
326+
cacheDirective({ rejectNonAsyncModule: false }),
327+
])
328+
await expect(
329+
run(`"use cache"; export function Page() { return null }`),
330+
).resolves.toBeDefined()
331+
await expect(
332+
run(`export function getData() { "use cache"; return null }`),
333+
).rejects.toThrow('non async function')
334+
})
335+
324336
it.each(['this', 'super', 'arguments'] as const)(
325337
'rejects %s inside inline directive functions',
326338
async (expression) => {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ export type ServerFunctionDirective = {
5252
}) => void
5353
/** Reject synchronous annotated functions when enabled. */
5454
rejectNonAsyncFunction?: boolean
55+
/** Overrides synchronous-function validation for module-level directives. */
56+
rejectNonAsyncModule?: boolean
5557
/** Returns the runtime expression used to wrap the server function. */
5658
wrap: (context: ServerFunctionDirectiveContext) => string
5759
/** Selects which exports a module-level directive wraps and registers. */
@@ -326,6 +328,7 @@ export function vitePluginServerFunctionDirectives(options: Options): Plugin {
326328
filter: (name, meta) =>
327329
definition.filterExport?.({ name, id, meta }) ?? true,
328330
rejectNonAsyncFunction: definition.rejectNonAsyncFunction,
331+
rejectNonAsyncModule: definition.rejectNonAsyncModule,
329332
encode: (value) => {
330333
needsEncryptionRuntime = true
331334
return `__vite_rsc_encryption_runtime.encryptActionBoundArgs(${value})`

0 commit comments

Comments
 (0)