Unverified Commit 0563fa77 authored by OJ Kwon's avatar OJ Kwon Committed by GitHub

refactor(next-core): separate options context for pages/api (#58148)

### What

I want to mimic webpack config's behavior (https://github.com/vercel/next.js/blob/9b4b0847ed4a1025e73bec16a9ee11766e632e14/packages/next/src/build/webpack-config.ts#L1381-L1385) to the pages/api in turbopack. PR attempt to create a new context corresponding to pages/api, and use it to create api chunks.

Currently it just creates a new context, there is no new behavior's being introduced yet. I'm hoping to connect dots with https://github.com/vercel/turbo/pull/6376 later.
parent 10630211
...@@ -319,6 +319,20 @@ impl PagesProject { ...@@ -319,6 +319,20 @@ impl PagesProject {
) )
} }
/// Returns a context specific to pages/api.
/// This mimics the current configuration in next-dev.
/// (https://github.com/vercel/next.js/blob/9b4b0847ed4a1025e73bec16a9ee11766e632e14/packages/next/src/build/webpack-config.ts#L1381-L1385)
#[turbo_tasks::function]
pub(super) fn api_module_context(self: Vc<Self>) -> Vc<ModuleAssetContext> {
ModuleAssetContext::new(
self.transitions(),
self.project().server_compile_time_info(),
self.api_module_options_context(),
self.ssr_resolve_options_context(),
Vc::cell("api".to_string()),
)
}
#[turbo_tasks::function] #[turbo_tasks::function]
pub(super) fn ssr_data_module_context(self: Vc<Self>) -> Vc<ModuleAssetContext> { pub(super) fn ssr_data_module_context(self: Vc<Self>) -> Vc<ModuleAssetContext> {
ModuleAssetContext::new( ModuleAssetContext::new(
...@@ -341,6 +355,17 @@ impl PagesProject { ...@@ -341,6 +355,17 @@ impl PagesProject {
) )
} }
#[turbo_tasks::function]
pub(super) fn edge_api_module_context(self: Vc<Self>) -> Vc<ModuleAssetContext> {
ModuleAssetContext::new(
Default::default(),
self.project().edge_compile_time_info(),
self.api_module_options_context(),
self.edge_ssr_resolve_options_context(),
Vc::cell("edge_api".to_string()),
)
}
#[turbo_tasks::function] #[turbo_tasks::function]
pub(super) fn edge_ssr_data_module_context(self: Vc<Self>) -> Vc<ModuleAssetContext> { pub(super) fn edge_ssr_data_module_context(self: Vc<Self>) -> Vc<ModuleAssetContext> {
ModuleAssetContext::new( ModuleAssetContext::new(
...@@ -366,6 +391,20 @@ impl PagesProject { ...@@ -366,6 +391,20 @@ impl PagesProject {
)) ))
} }
#[turbo_tasks::function]
async fn api_module_options_context(self: Vc<Self>) -> Result<Vc<ModuleOptionsContext>> {
let this = self.await?;
Ok(get_server_module_options_context(
self.project().project_path(),
self.project().execution_context(),
Value::new(ServerContextType::PagesApi {
pages_dir: self.pages_dir(),
}),
this.mode,
self.project().next_config(),
))
}
#[turbo_tasks::function] #[turbo_tasks::function]
async fn ssr_data_module_options_context(self: Vc<Self>) -> Result<Vc<ModuleOptionsContext>> { async fn ssr_data_module_options_context(self: Vc<Self>) -> Result<Vc<ModuleOptionsContext>> {
let this = self.await?; let this = self.await?;
...@@ -721,8 +760,8 @@ impl PageEndpoint { ...@@ -721,8 +760,8 @@ impl PageEndpoint {
.node_root() .node_root()
.join("server".to_string()), .join("server".to_string()),
this.pages_project.project().project_path(), this.pages_project.project().project_path(),
this.pages_project.ssr_module_context(), this.pages_project.api_module_context(),
this.pages_project.edge_ssr_module_context(), this.pages_project.edge_api_module_context(),
this.pages_project.project().server_chunking_context(), this.pages_project.project().server_chunking_context(),
this.pages_project.project().edge_chunking_context(), this.pages_project.project().edge_chunking_context(),
this.pages_project.ssr_runtime_entries(), this.pages_project.ssr_runtime_entries(),
......
...@@ -115,6 +115,7 @@ pub async fn get_edge_resolve_options_context( ...@@ -115,6 +115,7 @@ pub async fn get_edge_resolve_options_context(
ServerContextType::AppRoute { .. } ServerContextType::AppRoute { .. }
| ServerContextType::Pages { .. } | ServerContextType::Pages { .. }
| ServerContextType::PagesData { .. } | ServerContextType::PagesData { .. }
| ServerContextType::PagesApi { .. }
| ServerContextType::AppSSR { .. } | ServerContextType::AppSSR { .. }
| ServerContextType::Middleware { .. } => {} | ServerContextType::Middleware { .. } => {}
}; };
......
...@@ -285,7 +285,9 @@ pub async fn get_next_server_import_map( ...@@ -285,7 +285,9 @@ pub async fn get_next_server_import_map(
import_map.insert_exact_alias("next/dist/server/require-hook", external); import_map.insert_exact_alias("next/dist/server/require-hook", external);
match ty { match ty {
ServerContextType::Pages { .. } | ServerContextType::PagesData { .. } => { ServerContextType::Pages { .. }
| ServerContextType::PagesData { .. }
| ServerContextType::PagesApi { .. } => {
import_map.insert_exact_alias("react", external); import_map.insert_exact_alias("react", external);
import_map.insert_wildcard_alias("react/", external); import_map.insert_wildcard_alias("react/", external);
import_map.insert_exact_alias("react-dom", external); import_map.insert_exact_alias("react-dom", external);
...@@ -402,7 +404,9 @@ pub async fn get_next_edge_import_map( ...@@ -402,7 +404,9 @@ pub async fn get_next_edge_import_map(
let ty = ty.into_value(); let ty = ty.into_value();
match ty { match ty {
ServerContextType::Pages { .. } | ServerContextType::PagesData { .. } => {} ServerContextType::Pages { .. }
| ServerContextType::PagesData { .. }
| ServerContextType::PagesApi { .. } => {}
ServerContextType::AppSSR { .. } ServerContextType::AppSSR { .. }
| ServerContextType::AppRSC { .. } | ServerContextType::AppRSC { .. }
| ServerContextType::AppRoute { .. } => { | ServerContextType::AppRoute { .. } => {
...@@ -499,7 +503,7 @@ async fn insert_next_server_special_aliases( ...@@ -499,7 +503,7 @@ async fn insert_next_server_special_aliases(
NextRuntime::NodeJs => external_request_to_import_mapping(request), NextRuntime::NodeJs => external_request_to_import_mapping(request),
}; };
match ty { match ty {
ServerContextType::Pages { pages_dir } => { ServerContextType::Pages { pages_dir } | ServerContextType::PagesApi { pages_dir } => {
import_map.insert_exact_alias( import_map.insert_exact_alias(
"@opentelemetry/api", "@opentelemetry/api",
// TODO(WEB-625) this actually need to prefer the local version of // TODO(WEB-625) this actually need to prefer the local version of
...@@ -558,7 +562,9 @@ async fn insert_next_server_special_aliases( ...@@ -558,7 +562,9 @@ async fn insert_next_server_special_aliases(
// see https://github.com/vercel/next.js/blob/8013ef7372fc545d49dbd060461224ceb563b454/packages/next/src/build/webpack-config.ts#L1449-L1531 // see https://github.com/vercel/next.js/blob/8013ef7372fc545d49dbd060461224ceb563b454/packages/next/src/build/webpack-config.ts#L1449-L1531
match ty { match ty {
ServerContextType::Pages { .. } | ServerContextType::PagesData { .. } => { ServerContextType::Pages { .. }
| ServerContextType::PagesData { .. }
| ServerContextType::PagesApi { .. } => {
insert_exact_alias_map( insert_exact_alias_map(
import_map, import_map,
project_path, project_path,
......
...@@ -72,6 +72,9 @@ pub enum ServerContextType { ...@@ -72,6 +72,9 @@ pub enum ServerContextType {
Pages { Pages {
pages_dir: Vc<FileSystemPath>, pages_dir: Vc<FileSystemPath>,
}, },
PagesApi {
pages_dir: Vc<FileSystemPath>,
},
PagesData { PagesData {
pages_dir: Vc<FileSystemPath>, pages_dir: Vc<FileSystemPath>,
}, },
...@@ -133,6 +136,7 @@ pub async fn get_server_resolve_options_context( ...@@ -133,6 +136,7 @@ pub async fn get_server_resolve_options_context(
ServerContextType::AppRoute { .. } ServerContextType::AppRoute { .. }
| ServerContextType::Pages { .. } | ServerContextType::Pages { .. }
| ServerContextType::PagesData { .. } | ServerContextType::PagesData { .. }
| ServerContextType::PagesApi { .. }
| ServerContextType::AppSSR { .. } | ServerContextType::AppSSR { .. }
| ServerContextType::Middleware { .. } => {} | ServerContextType::Middleware { .. } => {}
}; };
...@@ -147,7 +151,9 @@ pub async fn get_server_resolve_options_context( ...@@ -147,7 +151,9 @@ pub async fn get_server_resolve_options_context(
NextNodeSharedRuntimeResolvePlugin::new(project_path, Value::new(ty)); NextNodeSharedRuntimeResolvePlugin::new(project_path, Value::new(ty));
let plugins = match ty { let plugins = match ty {
ServerContextType::Pages { .. } | ServerContextType::PagesData { .. } => { ServerContextType::Pages { .. }
| ServerContextType::PagesData { .. }
| ServerContextType::PagesApi { .. } => {
vec![ vec![
Vc::upcast(module_feature_report_resolve_plugin), Vc::upcast(module_feature_report_resolve_plugin),
Vc::upcast(external_cjs_modules_plugin), Vc::upcast(external_cjs_modules_plugin),
...@@ -325,7 +331,9 @@ pub async fn get_server_module_options_context( ...@@ -325,7 +331,9 @@ pub async fn get_server_module_options_context(
)); ));
let module_options_context = match ty.into_value() { let module_options_context = match ty.into_value() {
ServerContextType::Pages { .. } | ServerContextType::PagesData { .. } => { ServerContextType::Pages { .. }
| ServerContextType::PagesData { .. }
| ServerContextType::PagesApi { .. } => {
let mut base_source_transforms: Vec<Vc<TransformPlugin>> = vec![ let mut base_source_transforms: Vec<Vc<TransformPlugin>> = vec![
styled_components_transform_plugin, styled_components_transform_plugin,
styled_jsx_transform_plugin, styled_jsx_transform_plugin,
......
...@@ -31,7 +31,9 @@ pub async fn get_next_server_transforms_rules( ...@@ -31,7 +31,9 @@ pub async fn get_next_server_transforms_rules(
rules.push(get_next_font_transform_rule()); rules.push(get_next_font_transform_rule());
let (is_server_components, pages_dir) = match context_ty { let (is_server_components, pages_dir) = match context_ty {
ServerContextType::Pages { pages_dir } => (false, Some(pages_dir)), ServerContextType::Pages { pages_dir } | ServerContextType::PagesApi { pages_dir } => {
(false, Some(pages_dir))
}
ServerContextType::PagesData { pages_dir } => { ServerContextType::PagesData { pages_dir } => {
rules.push( rules.push(
get_next_pages_transforms_rule(pages_dir, ExportFilter::StripDefaultExport).await?, get_next_pages_transforms_rule(pages_dir, ExportFilter::StripDefaultExport).await?,
...@@ -74,6 +76,7 @@ pub async fn get_next_server_internal_transforms_rules( ...@@ -74,6 +76,7 @@ pub async fn get_next_server_internal_transforms_rules(
match context_ty { match context_ty {
ServerContextType::Pages { .. } => {} ServerContextType::Pages { .. } => {}
ServerContextType::PagesApi { .. } => {}
ServerContextType::PagesData { .. } => {} ServerContextType::PagesData { .. } => {}
ServerContextType::AppSSR { .. } => {} ServerContextType::AppSSR { .. } => {}
ServerContextType::AppRSC { ServerContextType::AppRSC {
......
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