Unverified Commit f641d9cc authored by OJ Kwon's avatar OJ Kwon Committed by GitHub

feat(next-core): apply rsc transform in turbopack (#59629)

### What?

Turbopack does not apply same transform for the react server components,
which makes missing lot of compilation error validation and custom
comments. PR refactors transform to be used in next-swc / turbopack
both, then apply it into turbopack.

There are still some of test cases are not passing, might need further
digging for the transform condition.


Closes PACK-2155

---------
Co-authored-by: 's avatarTim Neutkens <tim@timneutkens.nl>
parent f60c609b
This diff is collapsed.
...@@ -12,6 +12,8 @@ members = [ ...@@ -12,6 +12,8 @@ members = [
"packages/next-swc/crates/next-transform-font", "packages/next-swc/crates/next-transform-font",
"packages/next-swc/crates/next-transform-dynamic", "packages/next-swc/crates/next-transform-dynamic",
"packages/next-swc/crates/next-transform-strip-page-exports", "packages/next-swc/crates/next-transform-strip-page-exports",
"packages/next-swc/crates/next-transform-react-server-components",
"packages/next-swc/crates/next-visitor-cjs-finder",
] ]
[workspace.lints.clippy] [workspace.lints.clippy]
...@@ -34,6 +36,8 @@ next-swc = { path = "packages/next-swc/crates/core" } ...@@ -34,6 +36,8 @@ next-swc = { path = "packages/next-swc/crates/core" }
next-transform-font = { path = "packages/next-swc/crates/next-transform-font" } next-transform-font = { path = "packages/next-swc/crates/next-transform-font" }
next-transform-dynamic = { path = "packages/next-swc/crates/next-transform-dynamic" } next-transform-dynamic = { path = "packages/next-swc/crates/next-transform-dynamic" }
next-transform-strip-page-exports = { path = "packages/next-swc/crates/next-transform-strip-page-exports" } next-transform-strip-page-exports = { path = "packages/next-swc/crates/next-transform-strip-page-exports" }
next-transform-react-server-components = { path = "packages/next-swc/crates/next-transform-react-server-components" }
next-visitor-cjs-finder = { path = "packages/next-swc/crates/next-visitor-cjs-finder" }
# SWC crates # SWC crates
swc_core = { version = "0.87.10", features = [ swc_core = { version = "0.87.10", features = [
......
...@@ -26,8 +26,10 @@ serde_json = "1" ...@@ -26,8 +26,10 @@ serde_json = "1"
sha1 = "0.10.1" sha1 = "0.10.1"
tracing = { version = "0.1.37" } tracing = { version = "0.1.37" }
next-visitor-cjs-finder = { workspace = true }
next-transform-dynamic = { workspace = true } next-transform-dynamic = { workspace = true }
next-transform-font = { workspace = true } next-transform-font = { workspace = true }
next-transform-react-server-components = { workspace = true }
turbopack-binding = { workspace = true, features = [ turbopack-binding = { workspace = true, features = [
"__swc_core", "__swc_core",
......
...@@ -34,11 +34,11 @@ DEALINGS IN THE SOFTWARE. ...@@ -34,11 +34,11 @@ DEALINGS IN THE SOFTWARE.
use std::{cell::RefCell, path::PathBuf, rc::Rc, sync::Arc}; use std::{cell::RefCell, path::PathBuf, rc::Rc, sync::Arc};
use auto_cjs::contains_cjs;
use either::Either; use either::Either;
use fxhash::FxHashSet; use fxhash::FxHashSet;
use next_transform_dynamic::{next_dynamic, NextDynamicMode}; use next_transform_dynamic::{next_dynamic, NextDynamicMode};
use next_transform_font::next_font_loaders; use next_transform_font::next_font_loaders;
use next_visitor_cjs_finder::contains_cjs;
use serde::Deserialize; use serde::Deserialize;
use turbopack_binding::swc::{ use turbopack_binding::swc::{
core::{ core::{
...@@ -56,7 +56,6 @@ use turbopack_binding::swc::{ ...@@ -56,7 +56,6 @@ use turbopack_binding::swc::{
}; };
pub mod amp_attributes; pub mod amp_attributes;
mod auto_cjs;
pub mod cjs_optimizer; pub mod cjs_optimizer;
pub mod disallow_re_export_all_in_page; pub mod disallow_re_export_all_in_page;
mod import_analyzer; mod import_analyzer;
...@@ -66,7 +65,6 @@ pub mod optimize_barrel; ...@@ -66,7 +65,6 @@ pub mod optimize_barrel;
pub mod optimize_server_react; pub mod optimize_server_react;
pub mod page_config; pub mod page_config;
pub mod pure; pub mod pure;
pub mod react_server_components;
pub mod server_actions; pub mod server_actions;
pub mod shake_exports; pub mod shake_exports;
...@@ -101,7 +99,7 @@ pub struct TransformOptions { ...@@ -101,7 +99,7 @@ pub struct TransformOptions {
pub prefer_esm: bool, pub prefer_esm: bool,
#[serde(default)] #[serde(default)]
pub server_components: Option<react_server_components::Config>, pub server_components: Option<next_transform_react_server_components::Config>,
#[serde(default)] #[serde(default)]
pub styled_jsx: Option<turbopack_binding::swc::custom_transform::styled_jsx::visitor::Config>, pub styled_jsx: Option<turbopack_binding::swc::custom_transform::styled_jsx::visitor::Config>,
...@@ -194,7 +192,7 @@ where ...@@ -194,7 +192,7 @@ where
disallow_re_export_all_in_page::disallow_re_export_all_in_page(opts.is_page_file), disallow_re_export_all_in_page::disallow_re_export_all_in_page(opts.is_page_file),
match &opts.server_components { match &opts.server_components {
Some(config) if config.truthy() => Some(config) if config.truthy() =>
Either::Left(react_server_components::server_components( Either::Left(next_transform_react_server_components::server_components(
file.name.clone(), file.name.clone(),
config.clone(), config.clone(),
comments.clone(), comments.clone(),
...@@ -236,7 +234,7 @@ where ...@@ -236,7 +234,7 @@ where
Some(config) if config.truthy() => match config { Some(config) if config.truthy() => match config {
// Always enable the Server Components mode for both // Always enable the Server Components mode for both
// server and client layers. // server and client layers.
react_server_components::Config::WithOptions(config) => config.is_react_server_layer, next_transform_react_server_components::Config::WithOptions(config) => config.is_react_server_layer,
_ => false, _ => false,
}, },
_ => false, _ => false,
......
...@@ -63,6 +63,7 @@ turbo-tasks-fs = { workspace = true } ...@@ -63,6 +63,7 @@ turbo-tasks-fs = { workspace = true }
next-transform-strip-page-exports = { workspace = true } next-transform-strip-page-exports = { workspace = true }
next-transform-font = { workspace = true } next-transform-font = { workspace = true }
next-transform-dynamic = { workspace = true } next-transform-dynamic = { workspace = true }
next-transform-react-server-components = { workspace = true }
swc_core = { workspace = true, features = [ swc_core = { workspace = true, features = [
"ecma_ast", "ecma_ast",
......
...@@ -11,7 +11,9 @@ use crate::{ ...@@ -11,7 +11,9 @@ use crate::{
next_shared::transforms::{ next_shared::transforms::{
get_next_dynamic_transform_rule, get_next_font_transform_rule, get_next_image_rule, get_next_dynamic_transform_rule, get_next_font_transform_rule, get_next_image_rule,
get_next_modularize_imports_rule, get_next_pages_transforms_rule, get_next_modularize_imports_rule, get_next_pages_transforms_rule,
get_server_actions_transform_rule, server_actions::ActionsTransform, get_server_actions_transform_rule,
next_react_server_components::get_next_react_server_components_transform_rule,
server_actions::ActionsTransform,
}, },
}; };
...@@ -60,6 +62,11 @@ pub async fn get_next_server_transforms_rules( ...@@ -60,6 +62,11 @@ pub async fn get_next_server_transforms_rules(
ActionsTransform::Server, ActionsTransform::Server,
mdx_rs, mdx_rs,
)); ));
rules.push(get_next_react_server_components_transform_rule(
true, mdx_rs,
));
if let Some(client_transition) = client_transition { if let Some(client_transition) = client_transition {
rules.push(get_next_css_client_reference_transforms_rule( rules.push(get_next_css_client_reference_transforms_rule(
client_transition, client_transition,
......
...@@ -2,6 +2,7 @@ pub(crate) mod emotion; ...@@ -2,6 +2,7 @@ pub(crate) mod emotion;
pub(crate) mod modularize_imports; pub(crate) mod modularize_imports;
pub(crate) mod next_dynamic; pub(crate) mod next_dynamic;
pub(crate) mod next_font; pub(crate) mod next_font;
pub(crate) mod next_react_server_components;
pub(crate) mod next_strip_page_exports; pub(crate) mod next_strip_page_exports;
pub(crate) mod relay; pub(crate) mod relay;
pub(crate) mod server_actions; pub(crate) mod server_actions;
......
use std::path::PathBuf;
use anyhow::Result;
use async_trait::async_trait;
use next_transform_react_server_components::{server_components, Config, Options};
use swc_core::{
common::{util::take::Take, FileName},
ecma::{
ast::{Module, Program},
visit::FoldWith,
},
};
use turbo_tasks::Vc;
use turbopack_binding::turbopack::{
ecmascript::{CustomTransformer, EcmascriptInputTransform, TransformContext},
turbopack::module_options::{ModuleRule, ModuleRuleEffect},
};
use super::module_rule_match_js_no_url;
/// Returns a rule which applies the Next.js react server components transform.
pub fn get_next_react_server_components_transform_rule(
is_react_server_layer: bool,
enable_mdx_rs: bool,
) -> ModuleRule {
let transformer =
EcmascriptInputTransform::Plugin(Vc::cell(Box::new(NextJsReactServerComponents {
is_react_server_layer,
}) as _));
ModuleRule::new(
module_rule_match_js_no_url(enable_mdx_rs),
vec![ModuleRuleEffect::AddEcmascriptTransforms(Vc::cell(vec![
transformer,
]))],
)
}
#[derive(Debug)]
struct NextJsReactServerComponents {
is_react_server_layer: bool,
}
#[async_trait]
impl CustomTransformer for NextJsReactServerComponents {
async fn transform(&self, program: &mut Program, ctx: &TransformContext<'_>) -> Result<()> {
let p = std::mem::replace(program, Program::Module(Module::dummy()));
let mut visitor = server_components(
FileName::Custom(ctx.file_path_str.to_string()),
Config::WithOptions(Options {
is_react_server_layer: self.is_react_server_layer,
}),
ctx.comments,
Some(PathBuf::from(ctx.file_path.parent().await?.path.clone())),
);
*program = p.fold_with(&mut visitor);
Ok(())
}
}
[package]
name = "next-transform-react-server-components"
version = "0.1.0"
description = "TBD"
license = "MPL-2.0"
edition = "2021"
[lib]
bench = false
[lints]
workspace = true
[dependencies]
serde = { workspace = true }
serde_json = { workspace = true, features = ["preserve_order"] }
regex = "1.5"
anyhow = { workspace = true }
swc_core = { workspace = true }
next-visitor-cjs-finder = { workspace = true }
\ No newline at end of file
[package]
name = "next-visitor-cjs-finder"
version = "0.1.0"
description = "TBD"
license = "MPL-2.0"
edition = "2021"
[lib]
bench = false
[lints]
workspace = true
[dependencies]
swc_core = { workspace = true }
\ No newline at end of file
use turbopack_binding::swc::core::ecma::{ use swc_core::ecma::{
ast::*, ast::*,
visit::{Visit, VisitWith}, visit::{Visit, VisitWith},
}; };
pub(crate) fn contains_cjs(m: &Module) -> bool { pub fn contains_cjs(m: &Module) -> bool {
let mut v = CjsFinder::default(); let mut v = CjsFinder::default();
m.visit_with(&mut v); m.visit_with(&mut v);
v.found && !v.is_esm v.found && !v.is_esm
......
...@@ -280,7 +280,7 @@ describe('Error overlay - RSC build errors', () => { ...@@ -280,7 +280,7 @@ describe('Error overlay - RSC build errors', () => {
expect(await session.hasRedbox(true)).toBe(true) expect(await session.hasRedbox(true)).toBe(true)
expect(await session.getRedboxSource()).toInclude( expect(await session.getRedboxSource()).toInclude(
`You're importing a component that needs ${api}. It only works in a Client Component but none of its parents are marked with "use client", so they're Server Components by default.` `You're importing a component that needs ${api}. It only works in a Client Component but none of its parents are marked with "use client", so they're Server Components`
) )
await cleanup() await cleanup()
...@@ -348,25 +348,29 @@ describe('Error overlay - RSC build errors', () => { ...@@ -348,25 +348,29 @@ describe('Error overlay - RSC build errors', () => {
) )
expect(await session.hasRedbox(true)).toBe(true) expect(await session.hasRedbox(true)).toBe(true)
await check(() => session.getRedboxSource(), /must be a Client Component/) await check(
() => session.getRedboxSource(),
/must be a Client \n| Component/
)
expect( expect(
next.normalizeTestDirContent(await session.getRedboxSource()) next.normalizeTestDirContent(await session.getRedboxSource())
).toMatchInlineSnapshot( ).toMatchInlineSnapshot(
next.normalizeSnapshot(` `
"./app/server-with-errors/error-file/error.js "./app/server-with-errors/error-file/error.js
ReactServerComponentsError: Error:
x TEST_DIR/app/server-with-errors/error-file/error.js must be a Client Component. Add the "use client" directive the top
./app/server-with-errors/error-file/error.js must be a Client Component. Add the "use client" directive the top of the file to resolve this issue. | of the file to resolve this issue.
Learn more: https://nextjs.org/docs/getting-started/react-essentials#client-components | Learn more: https://nextjs.org/docs/getting-started/react-essentials#client-components
|
|
,-[TEST_DIR/app/server-with-errors/error-file/error.js:1:1] ,-[TEST_DIR/app/server-with-errors/error-file/error.js:1:1]
1 | export default function Error() {} 1 | export default function Error() {}
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
\`---- \`----
Import path: Import trace for requested module:
./app/server-with-errors/error-file/error.js" ./app/server-with-errors/error-file/error.js"
`) `
) )
await cleanup() await cleanup()
......
...@@ -53,15 +53,15 @@ describe('Error Overlay for server components compiler errors in pages', () => { ...@@ -53,15 +53,15 @@ describe('Error Overlay for server components compiler errors in pages', () => {
() => session.getRedboxSource(), () => session.getRedboxSource(),
/That only works in a Server Component/ /That only works in a Server Component/
) )
expect(
next.normalizeTestDirContent(await session.getRedboxSource())
).toMatchInlineSnapshot(
next.normalizeSnapshot(`
"./components/Comp.js
ReactServerComponentsError:
You're importing a component that needs next/headers. That only works in a Server Component which is not supported in the pages/ directory. Read more: https://nextjs.org/docs/getting-started/react-essentials#server-components
expect(next.normalizeTestDirContent(await session.getRedboxSource()))
.toMatchInlineSnapshot(`
"./components/Comp.js
Error:
x You're importing a component that needs next/headers. That only works in a Server Component which is not supported in the pages/ directory. Read more: https://nextjs.org/docs/getting-started/
| react-essentials#server-components
|
|
,-[TEST_DIR/components/Comp.js:1:1] ,-[TEST_DIR/components/Comp.js:1:1]
1 | import { cookies } from 'next/headers' 1 | import { cookies } from 'next/headers'
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...@@ -74,7 +74,6 @@ describe('Error Overlay for server components compiler errors in pages', () => { ...@@ -74,7 +74,6 @@ describe('Error Overlay for server components compiler errors in pages', () => {
./components/Comp.js ./components/Comp.js
./pages/index.js" ./pages/index.js"
`) `)
)
await cleanup() await cleanup()
}) })
...@@ -98,15 +97,15 @@ describe('Error Overlay for server components compiler errors in pages', () => { ...@@ -98,15 +97,15 @@ describe('Error Overlay for server components compiler errors in pages', () => {
() => session.getRedboxSource(), () => session.getRedboxSource(),
/That only works in a Server Component/ /That only works in a Server Component/
) )
expect(
next.normalizeTestDirContent(await session.getRedboxSource())
).toMatchInlineSnapshot(
next.normalizeSnapshot(`
"./components/Comp.js
ReactServerComponentsError:
You're importing a component that needs server-only. That only works in a Server Component which is not supported in the pages/ directory. Read more: https://nextjs.org/docs/getting-started/react-essentials#server-components
expect(next.normalizeTestDirContent(await session.getRedboxSource()))
.toMatchInlineSnapshot(`
"./components/Comp.js
Error:
x You're importing a component that needs server-only. That only works in a Server Component which is not supported in the pages/ directory. Read more: https://nextjs.org/docs/getting-started/
| react-essentials#server-components
|
|
,-[TEST_DIR/components/Comp.js:1:1] ,-[TEST_DIR/components/Comp.js:1:1]
1 | import 'server-only' 1 | import 'server-only'
: ^^^^^^^^^^^^^^^^^^^^ : ^^^^^^^^^^^^^^^^^^^^
...@@ -119,7 +118,6 @@ describe('Error Overlay for server components compiler errors in pages', () => { ...@@ -119,7 +118,6 @@ describe('Error Overlay for server components compiler errors in pages', () => {
./components/Comp.js ./components/Comp.js
./pages/index.js" ./pages/index.js"
`) `)
)
await cleanup() await cleanup()
}) })
......
...@@ -1126,11 +1126,8 @@ ...@@ -1126,11 +1126,8 @@
"Error overlay - RSC build errors should error for invalid undefined module retuning from next dynamic", "Error overlay - RSC build errors should error for invalid undefined module retuning from next dynamic",
"Error overlay - RSC build errors should error when page component export is not valid", "Error overlay - RSC build errors should error when page component export is not valid",
"Error overlay - RSC build errors should error when page component export is not valid on initial load", "Error overlay - RSC build errors should error when page component export is not valid on initial load",
"Error overlay - RSC build errors should freeze parent resolved metadata to avoid mutating in generateMetadata" "Error overlay - RSC build errors should freeze parent resolved metadata to avoid mutating in generateMetadata",
],
"failed": [
"Error overlay - RSC build errors should allow to use and handle rsc poisoning client-only", "Error overlay - RSC build errors should allow to use and handle rsc poisoning client-only",
"Error overlay - RSC build errors should allow to use and handle rsc poisoning server-only",
"Error overlay - RSC build errors should error when Component from react is used in server component", "Error overlay - RSC build errors should error when Component from react is used in server component",
"Error overlay - RSC build errors should error when PureComponent from react is used in server component", "Error overlay - RSC build errors should error when PureComponent from react is used in server component",
"Error overlay - RSC build errors should error when createContext from react is used in server component", "Error overlay - RSC build errors should error when createContext from react is used in server component",
...@@ -1152,7 +1149,10 @@ ...@@ -1152,7 +1149,10 @@
"Error overlay - RSC build errors should error when useSyncExternalStore from react is used in server component", "Error overlay - RSC build errors should error when useSyncExternalStore from react is used in server component",
"Error overlay - RSC build errors should error when useTransition from react is used in server component", "Error overlay - RSC build errors should error when useTransition from react is used in server component",
"Error overlay - RSC build errors should throw an error when \"Component\" is imported in server components", "Error overlay - RSC build errors should throw an error when \"Component\" is imported in server components",
"Error overlay - RSC build errors should throw an error when \"use client\" is on the top level but after other expressions", "Error overlay - RSC build errors should throw an error when \"use client\" is on the top level but after other expressions"
],
"failed": [
"Error overlay - RSC build errors should allow to use and handle rsc poisoning server-only",
"Error overlay - RSC build errors should throw an error when error file is a server component", "Error overlay - RSC build errors should throw an error when error file is a server component",
"Error overlay - RSC build errors should throw an error when error file is a server component with empty error file", "Error overlay - RSC build errors should throw an error when error file is a server component with empty error file",
"Error overlay - RSC build errors should throw an error when getServerSideProps is used", "Error overlay - RSC build errors should throw an error when getServerSideProps is used",
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