Skip to content

Commit

Permalink
Fix swc compiling of client components when directive appears later t…
Browse files Browse the repository at this point in the history
…han exports (#54392)

When `"use client"` directive appears after other statements, it should
be ignored instead of treat as client components. There's a bug inside
swc transform that we should mark the directives detection is "finished"
after other non string literals directives AST nodes are detected before
the first directive

x-ref:
#54358 (comment)
  • Loading branch information
huozhi committed Aug 22, 2023
1 parent 96a5934 commit 0277ef0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/next-swc/crates/core/src/react_server_components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ impl<C: Comments> ReactServerComponents<C> {
},
})
}
finished_directives = true;
}
ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl { decl, .. })) => {
match decl {
Expand All @@ -240,18 +241,21 @@ impl<C: Comments> ReactServerComponents<C> {
}
_ => {}
}
finished_directives = true;
}
ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultDecl(ExportDefaultDecl {
decl: _,
..
})) => {
self.export_names.push("default".to_string());
finished_directives = true;
}
ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultExpr(ExportDefaultExpr {
expr: _,
..
})) => {
self.export_names.push("default".to_string());
finished_directives = true;
}
ModuleItem::ModuleDecl(ModuleDecl::ExportAll(_)) => {
self.export_names.push("*".to_string());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default function () {
return null
}

// prettier-ignore
'use client'
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function() {
return null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

x NEXT_RSC_ERR_CLIENT_DIRECTIVE
,-[input.js:5:1]
5 | // prettier-ignore
6 | 'use client'
: ^^^^^^^^^^^^
`----

0 comments on commit 0277ef0

Please sign in to comment.