From 0e78798f37ce9d68b56e4dcb4ca0011e9e1d4959 Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Fri, 18 Aug 2023 22:41:32 +0200 Subject: [PATCH] Fix renamed export of Server Actions (#54241) This fixes the compilation of `export { action as renamed }` syntax. Previously it's compiled as `export var action = ...` and with this fix, it will be `export var renamed = ...`. Closes #54229. --- packages/next-swc/crates/core/src/server_actions.rs | 4 +++- .../tests/fixture/server-actions/client/4/input.js | 4 ++++ .../tests/fixture/server-actions/client/4/output.js | 3 +++ test/e2e/app-dir/actions/app/client/actions.js | 5 +++++ test/e2e/app-dir/actions/app/client/page.js | 11 ++++++++++- 5 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 packages/next-swc/crates/core/tests/fixture/server-actions/client/4/input.js create mode 100644 packages/next-swc/crates/core/tests/fixture/server-actions/client/4/output.js diff --git a/packages/next-swc/crates/core/src/server_actions.rs b/packages/next-swc/crates/core/src/server_actions.rs index 5c7254a598c14..ec607f174f01d 100644 --- a/packages/next-swc/crates/core/src/server_actions.rs +++ b/packages/next-swc/crates/core/src/server_actions.rs @@ -948,7 +948,9 @@ impl VisitMut for ServerActions { declare: false, decls: vec![VarDeclarator { span: DUMMY_SP, - name: Pat::Ident(ident.into()), + name: Pat::Ident( + Ident::new(export_name.clone().into(), DUMMY_SP).into(), + ), init: Some(Box::new(Expr::Call(CallExpr { span: DUMMY_SP, callee: Callee::Expr(Box::new(Expr::Ident( diff --git a/packages/next-swc/crates/core/tests/fixture/server-actions/client/4/input.js b/packages/next-swc/crates/core/tests/fixture/server-actions/client/4/input.js new file mode 100644 index 0000000000000..6a1ad321a38ed --- /dev/null +++ b/packages/next-swc/crates/core/tests/fixture/server-actions/client/4/input.js @@ -0,0 +1,4 @@ +'use server' + +async function foo() {} +export { foo as bar } diff --git a/packages/next-swc/crates/core/tests/fixture/server-actions/client/4/output.js b/packages/next-swc/crates/core/tests/fixture/server-actions/client/4/output.js new file mode 100644 index 0000000000000..799e3b427ffc5 --- /dev/null +++ b/packages/next-swc/crates/core/tests/fixture/server-actions/client/4/output.js @@ -0,0 +1,3 @@ +/* __next_internal_action_entry_do_not_use__ bar */ import { createActionProxy } from "private-next-rsc-action-proxy"; +import createServerReference from "private-next-rsc-action-client-wrapper"; +export var bar = createServerReference("ac840dcaf5e8197cb02b7f3a43c119b7a770b272"); diff --git a/test/e2e/app-dir/actions/app/client/actions.js b/test/e2e/app-dir/actions/app/client/actions.js index 452028bc865c4..9cb7445eaa8ee 100644 --- a/test/e2e/app-dir/actions/app/client/actions.js +++ b/test/e2e/app-dir/actions/app/client/actions.js @@ -26,3 +26,8 @@ export default async function (value) { export async function redirectAction(path) { redirect(path) } + +const original = async () => { + console.log('action') +} +export { original as renamed } diff --git a/test/e2e/app-dir/actions/app/client/page.js b/test/e2e/app-dir/actions/app/client/page.js index eb288ff1f6ab4..e841265bb1013 100644 --- a/test/e2e/app-dir/actions/app/client/page.js +++ b/test/e2e/app-dir/actions/app/client/page.js @@ -2,7 +2,13 @@ import { useState } from 'react' -import double, { inc, dec, redirectAction, getHeaders } from './actions' +import double, { + inc, + dec, + redirectAction, + getHeaders, + renamed, +} from './actions' import { test } from './actions-lib' export default function Counter() { @@ -16,6 +22,9 @@ export default function Counter() { console.log(inc) const newCount = await inc(count) setCount(newCount) + + // test renamed action + renamed() }} > +1