Commit 2c80812e authored by Will Binns-Smith's avatar Will Binns-Smith Committed by Janka Uryga

Turbopack: Allow client components from foreign code in app routes (#64751)

This extends #64520 to cover cases where client components originate
from node_modules (foreign code).

Test Plan: Extended the integration test to cover this

Closes #64412
Fixes PACK-3014

---------
Co-authored-by: 's avatarOJ Kwon <1210596+kwonoj@users.noreply.github.com>
parent 64731138
...@@ -108,15 +108,8 @@ pub async fn get_edge_resolve_options_context( ...@@ -108,15 +108,8 @@ pub async fn get_edge_resolve_options_context(
.map(ToString::to_string), .map(ToString::to_string),
); );
match ty { if ty.supports_react_server() {
ServerContextType::AppRSC { .. } => custom_conditions.push("react-server".to_string()), custom_conditions.push("react-server".to_string());
ServerContextType::AppRoute { .. }
| ServerContextType::Pages { .. }
| ServerContextType::PagesData { .. }
| ServerContextType::PagesApi { .. }
| ServerContextType::AppSSR { .. }
| ServerContextType::Middleware { .. }
| ServerContextType::Instrumentation { .. } => {}
}; };
let resolve_options_context = ResolveOptionsContext { let resolve_options_context = ResolveOptionsContext {
......
...@@ -728,7 +728,7 @@ async fn rsc_aliases( ...@@ -728,7 +728,7 @@ async fn rsc_aliases(
} }
if runtime == NextRuntime::Edge { if runtime == NextRuntime::Edge {
if matches!(ty, ServerContextType::AppRSC { .. }) { if ty.supports_react_server() {
alias["react"] = format!("next/dist/compiled/react{react_channel}/react.react-server"); alias["react"] = format!("next/dist/compiled/react{react_channel}/react.react-server");
alias["react-dom"] = alias["react-dom"] =
format!("next/dist/compiled/react-dom{react_channel}/react-dom.react-server"); format!("next/dist/compiled/react-dom{react_channel}/react-dom.react-server");
......
...@@ -99,6 +99,17 @@ pub enum ServerContextType { ...@@ -99,6 +99,17 @@ pub enum ServerContextType {
Instrumentation, Instrumentation,
} }
impl ServerContextType {
pub fn supports_react_server(&self) -> bool {
matches!(
self,
ServerContextType::AppRSC { .. }
| ServerContextType::AppRoute { .. }
| ServerContextType::PagesApi { .. }
)
}
}
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn get_server_resolve_options_context( pub async fn get_server_resolve_options_context(
project_path: Vc<FileSystemPath>, project_path: Vc<FileSystemPath>,
...@@ -152,18 +163,10 @@ pub async fn get_server_resolve_options_context( ...@@ -152,18 +163,10 @@ pub async fn get_server_resolve_options_context(
.map(ToString::to_string), .map(ToString::to_string),
); );
match ty { if ty.supports_react_server() {
ServerContextType::AppRSC { .. } custom_conditions.push("react-server".to_string());
| ServerContextType::AppRoute { .. }
| ServerContextType::PagesApi { .. }
| ServerContextType::Middleware { .. } => {
custom_conditions.push("react-server".to_string())
}
ServerContextType::Pages { .. }
| ServerContextType::PagesData { .. }
| ServerContextType::AppSSR { .. }
| ServerContextType::Instrumentation { .. } => {}
}; };
let external_cjs_modules_plugin = ExternalCjsModulesResolvePlugin::new( let external_cjs_modules_plugin = ExternalCjsModulesResolvePlugin::new(
project_path, project_path,
project_path.root(), project_path.root(),
...@@ -325,7 +328,7 @@ pub async fn get_server_module_options_context( ...@@ -325,7 +328,7 @@ pub async fn get_server_module_options_context(
let mut foreign_next_server_rules = let mut foreign_next_server_rules =
get_next_server_transforms_rules(next_config, ty.into_value(), mode, true, next_runtime) get_next_server_transforms_rules(next_config, ty.into_value(), mode, true, next_runtime)
.await?; .await?;
let internal_custom_rules = let mut internal_custom_rules =
get_next_server_internal_transforms_rules(ty.into_value(), *next_config.mdx_rs().await?) get_next_server_internal_transforms_rules(ty.into_value(), *next_config.mdx_rs().await?)
.await?; .await?;
...@@ -622,10 +625,16 @@ pub async fn get_server_module_options_context( ...@@ -622,10 +625,16 @@ pub async fn get_server_module_options_context(
ecmascript_client_reference_transition_name, ecmascript_client_reference_transition_name,
} => { } => {
next_server_rules.extend(source_transform_rules); next_server_rules.extend(source_transform_rules);
let mut common_next_server_rules = vec![
get_next_react_server_components_transform_rule(next_config, true, Some(app_dir))
.await?,
];
if let Some(ecmascript_client_reference_transition_name) = if let Some(ecmascript_client_reference_transition_name) =
ecmascript_client_reference_transition_name ecmascript_client_reference_transition_name
{ {
next_server_rules.push(get_ecma_transform_rule( common_next_server_rules.push(get_ecma_transform_rule(
Box::new(ClientDirectiveTransformer::new( Box::new(ClientDirectiveTransformer::new(
ecmascript_client_reference_transition_name, ecmascript_client_reference_transition_name,
)), )),
...@@ -634,10 +643,8 @@ pub async fn get_server_module_options_context( ...@@ -634,10 +643,8 @@ pub async fn get_server_module_options_context(
)); ));
} }
next_server_rules.push( next_server_rules.extend(common_next_server_rules.iter().cloned());
get_next_react_server_components_transform_rule(next_config, true, Some(app_dir)) internal_custom_rules.extend(common_next_server_rules);
.await?,
);
let module_options_context = ModuleOptionsContext { let module_options_context = ModuleOptionsContext {
esm_url_rewrite_behavior: Some(UrlRewriteBehavior::Full), esm_url_rewrite_behavior: Some(UrlRewriteBehavior::Full),
......
...@@ -8,6 +8,7 @@ describe('referencing a client component in an app route', () => { ...@@ -8,6 +8,7 @@ describe('referencing a client component in an app route', () => {
it('responds without error', async () => { it('responds without error', async () => {
expect(JSON.parse(await next.render('/runtime'))).toEqual({ expect(JSON.parse(await next.render('/runtime'))).toEqual({
clientComponent: 'function', clientComponent: 'function',
myModuleClientComponent: 'function',
}) })
}) })
}) })
import { NextResponse } from 'next/server' import { NextResponse } from 'next/server'
import { ClientComponent } from '../../ClientComponent' import { ClientComponent } from '../../ClientComponent'
import { MyModuleClientComponent } from 'my-module/MyModuleClientComponent'
export function GET() { export function GET() {
return NextResponse.json({ return NextResponse.json({
clientComponent: typeof ClientComponent, clientComponent: typeof ClientComponent,
myModuleClientComponent: typeof MyModuleClientComponent,
}) })
} }
'use client'
export function MyModuleClientComponent() {
return <div>MyModuleClientComponent</div>
}
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