From 54d815ef62f0918ec0b53a99b5f711381bdd4563 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Tue, 7 May 2024 22:25:57 +0200 Subject: [PATCH] add turbopack change --- .../crates/next-core/src/next_edge/context.rs | 25 +++++++++++++ .../crates/next-core/src/next_import_map.rs | 36 ++++++++++--------- .../next-core/src/next_server/context.rs | 8 ++--- 3 files changed, 48 insertions(+), 21 deletions(-) diff --git a/packages/next-swc/crates/next-core/src/next_edge/context.rs b/packages/next-swc/crates/next-core/src/next_edge/context.rs index a3b11a95add37..5767b1ebd36f6 100644 --- a/packages/next-swc/crates/next-core/src/next_edge/context.rs +++ b/packages/next-swc/crates/next-core/src/next_edge/context.rs @@ -25,6 +25,7 @@ use crate::{ next_import_map::get_next_edge_import_map, next_server::context::ServerContextType, next_shared::resolve::{ + get_invalid_client_only_resolve_plugin, get_invalid_styled_jsx_resolve_plugin, ModuleFeatureReportResolvePlugin, NextSharedRuntimeResolvePlugin, UnsupportedModulesResolvePlugin, }, @@ -98,6 +99,29 @@ pub async fn get_edge_resolve_options_context( get_next_edge_import_map(project_path, ty, next_config, execution_context); let ty = ty.into_value(); + let invalid_client_only_resolve_plugin = get_invalid_client_only_resolve_plugin(project_path); + let invalid_styled_jsx_client_only_resolve_plugin = + get_invalid_styled_jsx_resolve_plugin(project_path); + + let plugins = match ty { + ServerContextType::Pages { .. } => { + vec![] + } + ServerContextType::PagesData { .. } + | ServerContextType::PagesApi { .. } + | ServerContextType::AppRSC { .. } + | ServerContextType::AppRoute { .. } + | ServerContextType::Middleware { .. } + | ServerContextType::Instrumentation => { + vec![ + Vc::upcast(invalid_client_only_resolve_plugin), + Vc::upcast(invalid_styled_jsx_client_only_resolve_plugin), + ] + } + ServerContextType::AppSSR { .. } => { + vec![] + } + }; // https://github.com/vercel/next.js/blob/bf52c254973d99fed9d71507a2e818af80b8ade7/packages/next/src/build/webpack-config.ts#L96-L102 let mut custom_conditions = vec![mode.await?.condition().to_string()]; @@ -137,6 +161,7 @@ pub async fn get_edge_resolve_options_context( foreign_code_context_condition(next_config, project_path).await?, resolve_options_context.clone().cell(), )], + plugins, ..resolve_options_context } .cell()) diff --git a/packages/next-swc/crates/next-core/src/next_import_map.rs b/packages/next-swc/crates/next-core/src/next_import_map.rs index 26c1e67c70117..799ebbdc9bf51 100644 --- a/packages/next-swc/crates/next-core/src/next_import_map.rs +++ b/packages/next-swc/crates/next-core/src/next_import_map.rs @@ -613,6 +613,7 @@ async fn insert_next_server_special_aliases( | ServerContextType::PagesApi { .. } | ServerContextType::AppRSC { .. } | ServerContextType::AppRoute { .. } + | ServerContextType::Middleware { .. } | ServerContextType::Instrumentation => { insert_exact_alias_map( import_map, @@ -636,23 +637,24 @@ async fn insert_next_server_special_aliases( "next/dist/compiled/client-only" => "next/dist/compiled/client-only/index".to_string(), }, ); - } - // Potential the bundle introduced into middleware and api can be poisoned by - // client-only but not being used, so we disabled the `client-only` erroring - // on these layers. `server-only` is still available. - ServerContextType::Middleware => { - insert_exact_alias_map( - import_map, - project_path, - indexmap! { - "server-only" => "next/dist/compiled/server-only/empty".to_string(), - "client-only" => "next/dist/compiled/client-only/index".to_string(), - "next/dist/compiled/server-only" => "next/dist/compiled/server-only/empty".to_string(), - "next/dist/compiled/client-only" => "next/dist/compiled/client-only/index".to_string(), - "next/dist/compiled/client-only/error" => "next/dist/compiled/client-only/index".to_string(), - }, - ); - } + } /* Potential the bundle introduced into middleware and api can be poisoned by + * client-only but not being used, so we disabled the `client-only` erroring + * on these layers. `server-only` is still available. + * ServerContextType::Middleware => { + * insert_exact_alias_map( + * import_map, + * project_path, + * indexmap! { + * "server-only" => "next/dist/compiled/server-only/empty".to_string(), + * "client-only" => "next/dist/compiled/client-only/index".to_string(), + * "next/dist/compiled/server-only" => + * "next/dist/compiled/server-only/empty".to_string(), + * "next/dist/compiled/client-only" => + * "next/dist/compiled/client-only/index".to_string(), + * "next/dist/compiled/client-only/error" => + * "next/dist/compiled/client-only/index".to_string(), }, + * ); + * } */ } import_map.insert_exact_alias( diff --git a/packages/next-swc/crates/next-core/src/next_server/context.rs b/packages/next-swc/crates/next-core/src/next_server/context.rs index aaeefdf883b8a..6679fe8b79437 100644 --- a/packages/next-swc/crates/next-core/src/next_server/context.rs +++ b/packages/next-swc/crates/next-core/src/next_server/context.rs @@ -232,6 +232,7 @@ pub async fn get_server_resolve_options_context( | ServerContextType::PagesApi { .. } | ServerContextType::AppRSC { .. } | ServerContextType::AppRoute { .. } + | ServerContextType::Middleware { .. } | ServerContextType::Instrumentation => { plugins.push(Vc::upcast(invalid_client_only_resolve_plugin)); plugins.push(Vc::upcast(invalid_styled_jsx_client_only_resolve_plugin)); @@ -239,10 +240,9 @@ pub async fn get_server_resolve_options_context( ServerContextType::AppSSR { .. } => { //[TODO] Build error in this context makes rsc-build-error.ts fail which expects runtime error code // looks like webpack and turbopack have different order, webpack runs rsc transform first, turbopack triggers resolve plugin first. - } - ServerContextType::Middleware => { - //noop - } + } /* ServerContextType::Middleware => { + * //noop + * } */ } let resolve_options_context = ResolveOptionsContext {