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 {
)
}
/// 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]
pub(super) fn ssr_data_module_context(self: Vc<Self>) -> Vc<ModuleAssetContext> {
ModuleAssetContext::new(
......@@ -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]
pub(super) fn edge_ssr_data_module_context(self: Vc<Self>) -> Vc<ModuleAssetContext> {
ModuleAssetContext::new(
......@@ -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]
async fn ssr_data_module_options_context(self: Vc<Self>) -> Result<Vc<ModuleOptionsContext>> {
let this = self.await?;
......@@ -721,8 +760,8 @@ impl PageEndpoint {
.node_root()
.join("server".to_string()),
this.pages_project.project().project_path(),
this.pages_project.ssr_module_context(),
this.pages_project.edge_ssr_module_context(),
this.pages_project.api_module_context(),
this.pages_project.edge_api_module_context(),
this.pages_project.project().server_chunking_context(),
this.pages_project.project().edge_chunking_context(),
this.pages_project.ssr_runtime_entries(),
......
......@@ -115,6 +115,7 @@ pub async fn get_edge_resolve_options_context(
ServerContextType::AppRoute { .. }
| ServerContextType::Pages { .. }
| ServerContextType::PagesData { .. }
| ServerContextType::PagesApi { .. }
| ServerContextType::AppSSR { .. }
| ServerContextType::Middleware { .. } => {}
};
......
......@@ -285,7 +285,9 @@ pub async fn get_next_server_import_map(
import_map.insert_exact_alias("next/dist/server/require-hook", external);
match ty {
ServerContextType::Pages { .. } | ServerContextType::PagesData { .. } => {
ServerContextType::Pages { .. }
| ServerContextType::PagesData { .. }
| ServerContextType::PagesApi { .. } => {
import_map.insert_exact_alias("react", external);
import_map.insert_wildcard_alias("react/", external);
import_map.insert_exact_alias("react-dom", external);
......@@ -402,7 +404,9 @@ pub async fn get_next_edge_import_map(
let ty = ty.into_value();
match ty {
ServerContextType::Pages { .. } | ServerContextType::PagesData { .. } => {}
ServerContextType::Pages { .. }
| ServerContextType::PagesData { .. }
| ServerContextType::PagesApi { .. } => {}
ServerContextType::AppSSR { .. }
| ServerContextType::AppRSC { .. }
| ServerContextType::AppRoute { .. } => {
......@@ -499,7 +503,7 @@ async fn insert_next_server_special_aliases(
NextRuntime::NodeJs => external_request_to_import_mapping(request),
};
match ty {
ServerContextType::Pages { pages_dir } => {
ServerContextType::Pages { pages_dir } | ServerContextType::PagesApi { pages_dir } => {
import_map.insert_exact_alias(
"@opentelemetry/api",
// TODO(WEB-625) this actually need to prefer the local version of
......@@ -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
match ty {
ServerContextType::Pages { .. } | ServerContextType::PagesData { .. } => {
ServerContextType::Pages { .. }
| ServerContextType::PagesData { .. }
| ServerContextType::PagesApi { .. } => {
insert_exact_alias_map(
import_map,
project_path,
......
......@@ -72,6 +72,9 @@ pub enum ServerContextType {
Pages {
pages_dir: Vc<FileSystemPath>,
},
PagesApi {
pages_dir: Vc<FileSystemPath>,
},
PagesData {
pages_dir: Vc<FileSystemPath>,
},
......@@ -133,6 +136,7 @@ pub async fn get_server_resolve_options_context(
ServerContextType::AppRoute { .. }
| ServerContextType::Pages { .. }
| ServerContextType::PagesData { .. }
| ServerContextType::PagesApi { .. }
| ServerContextType::AppSSR { .. }
| ServerContextType::Middleware { .. } => {}
};
......@@ -147,7 +151,9 @@ pub async fn get_server_resolve_options_context(
NextNodeSharedRuntimeResolvePlugin::new(project_path, Value::new(ty));
let plugins = match ty {
ServerContextType::Pages { .. } | ServerContextType::PagesData { .. } => {
ServerContextType::Pages { .. }
| ServerContextType::PagesData { .. }
| ServerContextType::PagesApi { .. } => {
vec![
Vc::upcast(module_feature_report_resolve_plugin),
Vc::upcast(external_cjs_modules_plugin),
......@@ -325,7 +331,9 @@ pub async fn get_server_module_options_context(
));
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![
styled_components_transform_plugin,
styled_jsx_transform_plugin,
......
......@@ -31,7 +31,9 @@ pub async fn get_next_server_transforms_rules(
rules.push(get_next_font_transform_rule());
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 } => {
rules.push(
get_next_pages_transforms_rule(pages_dir, ExportFilter::StripDefaultExport).await?,
......@@ -74,6 +76,7 @@ pub async fn get_next_server_internal_transforms_rules(
match context_ty {
ServerContextType::Pages { .. } => {}
ServerContextType::PagesApi { .. } => {}
ServerContextType::PagesData { .. } => {}
ServerContextType::AppSSR { .. } => {}
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