Skip to content

Commit

Permalink
add turbopack change
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed May 10, 2024
1 parent 258c460 commit 54d815e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 21 deletions.
25 changes: 25 additions & 0 deletions packages/next-swc/crates/next-core/src/next_edge/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -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()];
Expand Down Expand Up @@ -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())
Expand Down
36 changes: 19 additions & 17 deletions packages/next-swc/crates/next-core/src/next_import_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand Down
8 changes: 4 additions & 4 deletions packages/next-swc/crates/next-core/src/next_server/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,17 +232,17 @@ 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));
}
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 {
Expand Down

0 comments on commit 54d815e

Please sign in to comment.