Skip to content

Commit

Permalink
fix: default export cases
Browse files Browse the repository at this point in the history
  • Loading branch information
yimingjfe committed Jan 7, 2025
1 parent 431ef7b commit f0aa42e
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 7 deletions.
Binary file not shown.
68 changes: 62 additions & 6 deletions packages/cli/flight-server-transform-plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ where
comments: Option<C>,
export_mappings: Vec<ExportInfo>,
runtime_path: String,
action_count: u8,
}

#[derive(Debug)]
Expand All @@ -67,6 +68,13 @@ struct ServerReferenceInfo {
local_name: String,
}

#[derive(Debug)]
struct VarDeclInsertInfo {
position: usize,
ident: Ident,
expr: Box<Expr>,
}

impl<C> TransformVisitor<C>
where
C: Comments,
Expand All @@ -81,6 +89,7 @@ where
comments,
export_mappings: vec![],
runtime_path,
action_count: 0,
}
}

Expand Down Expand Up @@ -494,8 +503,10 @@ where
let mut actions_to_insert: Vec<ServerReferenceInfo> = vec![];
let has_server_directive = self.directive == Some(Directive::Server);

let mut var_decls_to_insert: Vec<VarDeclInsertInfo> = vec![];

while i < module.body.len() {
match &module.body[i] {
match &mut module.body[i] {
ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl { decl, .. })) => {
match decl {
Decl::Fn(fn_decl) => {
Expand Down Expand Up @@ -553,14 +564,25 @@ where
}
}
}
ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultDecl(ExportDefaultDecl { decl, .. })) => {
ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultDecl(ExportDefaultDecl { ref mut decl, .. })) => {
match decl {
DefaultDecl::Fn(fn_expr) => {
DefaultDecl::Fn(ref mut fn_expr) => {
if has_server_directive || self.has_server_directive_in_function(&fn_expr.function) {
self.add_reference("default".to_string(), ReferenceType::Server);

let ident = if let Some(ref mut ident) = fn_expr.ident {
ident.clone()
} else {
let new_ident = format!("$$ACTION_{}", self.action_count);
self.action_count += 1;
let new_ident_ident = Ident::new(new_ident.into(), DUMMY_SP, Default::default());
fn_expr.ident = Some(new_ident_ident.clone());
new_ident_ident
};

actions_to_insert.push(ServerReferenceInfo {
position: i + 1,
ident: Ident::new("default".into(), DUMMY_SP, Default::default()),
ident,
local_name: "default".to_string(),
});
}
Expand All @@ -573,9 +595,23 @@ where
Expr::Arrow(arrow) => {
if has_server_directive || self.has_server_directive_in_arrow(arrow) {
self.add_reference("default".to_string(), ReferenceType::Server);

let new_ident = format!("$$ACTION_{}", self.action_count);
self.action_count += 1;
let ident = Ident::new(new_ident.into(), DUMMY_SP, Default::default());

let var_decl_info = VarDeclInsertInfo {
position: i,
ident: ident.clone(),
expr: expr.clone(),
};
var_decls_to_insert.push(var_decl_info);

*expr = Box::new(Expr::Ident(ident.clone()));

actions_to_insert.push(ServerReferenceInfo {
position: i + 1,
ident: Ident::new("default".into(), DUMMY_SP, Default::default()),
position: i,
ident,
local_name: "default".to_string(),
});
}
Expand Down Expand Up @@ -662,6 +698,26 @@ where
module.body.insert(info.position + offset + 1, ModuleItem::Stmt(server_ref));
}
}

for (offset, info) in var_decls_to_insert.into_iter().enumerate() {
let var_decl = ModuleItem::Stmt(Stmt::Decl(Decl::Var(Box::new(VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Const,
declare: false,
decls: vec![VarDeclarator {
span: DUMMY_SP,
name: Pat::Ident(BindingIdent {
id: info.ident,
type_ann: None,
}),
init: Some(info.expr),
definite: false,
}],
ctxt: Default::default(),
}))));

module.body.insert(info.position + offset + 1, var_decl);
}
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { z } from 'zod';

export default () => {
'use server';
return `default`;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* @modern-js-rsc-metadata
{"directive":null,"exportNames":[{"exportName":"default"}]}*/
import { registerServerReference } from "@modern-js/runtime/rsc/server";
import { z } from 'zod';

const $$ACTION_0 = () => {
'use server';
return `default`;
}

registerServerReference($$ACTION_0, module.id, "default");
export default $$ACTION_0;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { z } from 'zod';

export default async function(){
'use server';
return `default`;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* @modern-js-rsc-metadata
{"directive":null,"exportNames":[{"exportName":"default"}]}*/
import { registerServerReference } from "@modern-js/runtime/rsc/server";
import { z } from 'zod';

export default async function $$ACTION_0(){
'use server';
return `default`;
}

registerServerReference($$ACTION_0, module.id, "default");
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ async function qux() {
return `qux`;
}

export default async function increment() {
'use server';
return `increment`;
}

export async function Component() {
async function handleServerAction(formData) {
'use server';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* @modern-js-rsc-metadata
{"directive":null,"exportNames":[{"exportName":"foo"},{"exportName":"baz"},{"exportName":"qux"}]}*/
{"directive":null,"exportNames":[{"exportName":"foo"},{"exportName":"baz"},{"exportName":"qux"},{"exportName":"default"}]}*/
import { registerServerReference } from "@modern-js/runtime/rsc/server";
import { z } from 'zod';

Expand Down Expand Up @@ -27,6 +27,13 @@ async function qux() {

registerServerReference(qux, module.id, "qux");

export default async function increment() {
'use server';
return `increment`;
}

registerServerReference(increment, module.id, "default");

export async function Component() {
async function handleServerAction(formData) {
'use server';
Expand Down

0 comments on commit f0aa42e

Please sign in to comment.