Skip to content
Merged
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
9 changes: 9 additions & 0 deletions packages/directive-functions-plugin/src/compilers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,15 @@ const safeRemoveExports = (ast: babel.types.File) => {
const insertIndex = programBody.findIndex(
(node) => node === path.node.declaration,
)
// do not remove export if it is an anonymous function / class, otherwise this would produce a syntax error
if (
babel.types.isFunctionDeclaration(path.node.declaration) ||
babel.types.isClassDeclaration(path.node.declaration)
) {
if (!path.node.declaration.id) {
return
}
}
programBody.splice(insertIndex, 0, path.node.declaration as any)
}
}
Expand Down
46 changes: 46 additions & 0 deletions packages/directive-functions-plugin/tests/compiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -710,4 +710,50 @@ describe('server function compilation', () => {
export { serverFnConstWithImport_1, serverFnNamedWithImport_1 };"
`)
})
test('async function with anonymous default export', () => {
const code = `
async function bytesSignupServerFn({ email }: { email: string }) {
'use server'
return 'test'
}
export default function () {
return null;
}
`

const client = compileDirectives({ ...clientConfig, code })
const ssr = compileDirectives({ ...ssrConfig, code })
const server = compileDirectives({
...serverConfig,
code,
filename:
ssr.directiveFnsById[Object.keys(ssr.directiveFnsById)[0]!]!
.extractedFilename,
})

expect(client.compiledResult.code).toMatchInlineSnapshot(`
"export default function () {
return null;
}"
`)
expect(ssr.compiledResult.code).toMatchInlineSnapshot(`
"export default function () {
return null;
}"
`)
expect(server.compiledResult.code).toMatchInlineSnapshot(`
"import { createServerRpc } from "my-rpc-lib-server";
const bytesSignupServerFn_1 = createServerRpc("test_ts--bytesSignupServerFn_1", async function ({
email
}: {
email: string;
}) {
return 'test';
});
export default function () {
return null;
}
export { bytesSignupServerFn_1 };"
`)
})
})