Skip to content

Commit

Permalink
fix(directive-functions-plugin): handle anonymous default exports (#3447
Browse files Browse the repository at this point in the history
)
  • Loading branch information
schiller-manuel authored Feb 16, 2025
1 parent dfd5831 commit c6a2409
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
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 };"
`)
})
})

0 comments on commit c6a2409

Please sign in to comment.