Unverified Commit 0277ef09 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Fix swc compiling of client components when directive appears later than 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:
https://github.com/vercel/next.js/pull/54358#discussion_r1300740950
parent 96a59344
...@@ -222,6 +222,7 @@ impl<C: Comments> ReactServerComponents<C> { ...@@ -222,6 +222,7 @@ impl<C: Comments> ReactServerComponents<C> {
}, },
}) })
} }
finished_directives = true;
} }
ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl { decl, .. })) => { ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl { decl, .. })) => {
match decl { match decl {
...@@ -240,18 +241,21 @@ impl<C: Comments> ReactServerComponents<C> { ...@@ -240,18 +241,21 @@ impl<C: Comments> ReactServerComponents<C> {
} }
_ => {} _ => {}
} }
finished_directives = true;
} }
ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultDecl(ExportDefaultDecl { ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultDecl(ExportDefaultDecl {
decl: _, decl: _,
.. ..
})) => { })) => {
self.export_names.push("default".to_string()); self.export_names.push("default".to_string());
finished_directives = true;
} }
ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultExpr(ExportDefaultExpr { ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultExpr(ExportDefaultExpr {
expr: _, expr: _,
.. ..
})) => { })) => {
self.export_names.push("default".to_string()); self.export_names.push("default".to_string());
finished_directives = true;
} }
ModuleItem::ModuleDecl(ModuleDecl::ExportAll(_)) => { ModuleItem::ModuleDecl(ModuleDecl::ExportAll(_)) => {
self.export_names.push("*".to_string()); self.export_names.push("*".to_string());
......
export default function () {
return null
}
// prettier-ignore
'use client'
x NEXT_RSC_ERR_CLIENT_DIRECTIVE
,-[input.js:5:1]
5 | // prettier-ignore
6 | 'use client'
: ^^^^^^^^^^^^
`----
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment