Unverified Commit f348241e authored by Donny/강동윤's avatar Donny/강동윤 Committed by GitHub

refactor(turbopack): Use `ResolvedVc<T>` for struct fields in `next-api`, final part (#73367)

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the PR.
- Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to understand the PR)
- When linking to a Slack thread, you might want to share details of the conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->
parent ea08b0fe
......@@ -529,14 +529,14 @@ impl NapiRoute {
} => NapiRoute {
pathname,
r#type: "page",
html_endpoint: convert_endpoint(html_endpoint),
data_endpoint: convert_endpoint(data_endpoint),
html_endpoint: convert_endpoint(*html_endpoint),
data_endpoint: convert_endpoint(*data_endpoint),
..Default::default()
},
Route::PageApi { endpoint } => NapiRoute {
pathname,
r#type: "page-api",
endpoint: convert_endpoint(endpoint),
endpoint: convert_endpoint(*endpoint),
..Default::default()
},
Route::AppPage(pages) => NapiRoute {
......@@ -561,7 +561,7 @@ impl NapiRoute {
pathname,
original_name: Some(original_name),
r#type: "app-route",
endpoint: convert_endpoint(endpoint),
endpoint: convert_endpoint(*endpoint),
..Default::default()
},
Route::Conflict => NapiRoute {
......
......@@ -711,24 +711,24 @@ pub fn app_entry_point_to_route(
root_layouts,
} => Route::AppRoute {
original_name: page.to_string(),
endpoint: Vc::upcast(
endpoint: ResolvedVc::upcast(
AppEndpoint {
ty: AppEndpointType::Route { path, root_layouts },
app_project,
page,
}
.cell(),
.resolved_cell(),
),
},
AppEntrypoint::AppMetadata { page, metadata } => Route::AppRoute {
original_name: page.to_string(),
endpoint: Vc::upcast(
endpoint: ResolvedVc::upcast(
AppEndpoint {
ty: AppEndpointType::Metadata { metadata },
app_project,
page,
}
.cell(),
.resolved_cell(),
),
},
}
......
......@@ -47,11 +47,11 @@ impl GlobalModuleIdStrategyBuilder {
html_endpoint,
data_endpoint,
} => {
preprocessed_module_ids.push(preprocess_module_ids(*html_endpoint));
preprocessed_module_ids.push(preprocess_module_ids(*data_endpoint));
preprocessed_module_ids.push(preprocess_module_ids(**html_endpoint));
preprocessed_module_ids.push(preprocess_module_ids(**data_endpoint));
}
Route::PageApi { endpoint } => {
preprocessed_module_ids.push(preprocess_module_ids(*endpoint));
preprocessed_module_ids.push(preprocess_module_ids(**endpoint));
}
Route::AppPage(page_routes) => {
for page_route in page_routes {
......@@ -65,7 +65,7 @@ impl GlobalModuleIdStrategyBuilder {
original_name: _,
endpoint,
} => {
preprocessed_module_ids.push(preprocess_module_ids(*endpoint));
preprocessed_module_ids.push(preprocess_module_ids(**endpoint));
}
Route::Conflict => {
tracing::info!("WARN: conflict");
......
use std::future::IntoFuture;
use anyhow::{bail, Context, Result};
use futures::future::BoxFuture;
use next_core::{
all_assets_from_entries, create_page_loader_entry_module, get_asset_path_from_pathname,
get_edge_resolve_options_context,
......@@ -105,7 +106,11 @@ impl PagesProject {
async fn add_page_to_routes(
routes: &mut FxIndexMap<RcStr, Route>,
page: Vc<PagesStructureItem>,
make_route: impl Fn(Vc<RcStr>, Vc<RcStr>, Vc<PagesStructureItem>) -> Route,
make_route: impl Fn(
Vc<RcStr>,
Vc<RcStr>,
Vc<PagesStructureItem>,
) -> BoxFuture<'static, Result<Route>>,
) -> Result<()> {
let PagesStructureItem {
next_router_path,
......@@ -115,7 +120,7 @@ impl PagesProject {
let pathname: RcStr = format!("/{}", next_router_path.await?.path).into();
let pathname_vc = Vc::cell(pathname.clone());
let original_name = Vc::cell(format!("/{}", original_path.await?.path).into());
let route = make_route(pathname_vc, original_name, page);
let route = make_route(pathname_vc, original_name, page).await?;
routes.insert(pathname, route);
Ok(())
}
......@@ -123,7 +128,11 @@ impl PagesProject {
async fn add_dir_to_routes(
routes: &mut FxIndexMap<RcStr, Route>,
dir: Vc<PagesDirectoryStructure>,
make_route: impl Fn(Vc<RcStr>, Vc<RcStr>, Vc<PagesStructureItem>) -> Route,
make_route: impl Fn(
Vc<RcStr>,
Vc<RcStr>,
Vc<PagesStructureItem>,
) -> BoxFuture<'static, Result<Route>>,
) -> Result<()> {
let mut queue = vec![dir];
while let Some(dir) = queue.pop() {
......@@ -145,37 +154,55 @@ impl PagesProject {
if let Some(api) = *api {
add_dir_to_routes(&mut routes, *api, |pathname, original_name, page| {
Route::PageApi {
endpoint: Vc::upcast(PageEndpoint::new(
PageEndpointType::Api,
self,
pathname,
original_name,
page,
pages_structure,
)),
}
Box::pin(async move {
Ok(Route::PageApi {
endpoint: ResolvedVc::upcast(
PageEndpoint::new(
PageEndpointType::Api,
self,
pathname,
original_name,
page,
pages_structure,
)
.to_resolved()
.await?,
),
})
})
})
.await?;
}
let make_page_route = |pathname, original_name, page| Route::Page {
html_endpoint: Vc::upcast(PageEndpoint::new(
PageEndpointType::Html,
self,
pathname,
original_name,
page,
pages_structure,
)),
data_endpoint: Vc::upcast(PageEndpoint::new(
PageEndpointType::Data,
self,
pathname,
original_name,
page,
pages_structure,
)),
let make_page_route = |pathname, original_name, page| -> BoxFuture<_> {
Box::pin(async move {
Ok(Route::Page {
html_endpoint: ResolvedVc::upcast(
PageEndpoint::new(
PageEndpointType::Html,
self,
pathname,
original_name,
page,
pages_structure,
)
.to_resolved()
.await?,
),
data_endpoint: ResolvedVc::upcast(
PageEndpoint::new(
PageEndpointType::Data,
self,
pathname,
original_name,
page,
pages_structure,
)
.to_resolved()
.await?,
),
})
})
};
if let Some(pages) = *pages {
......
use anyhow::Result;
use serde::{Deserialize, Serialize};
use turbo_rcstr::RcStr;
use turbo_tasks::{debug::ValueDebugFormat, trace::TraceRawVcs, Completion, FxIndexMap, Vc};
use turbo_tasks::{
debug::ValueDebugFormat, trace::TraceRawVcs, Completion, FxIndexMap, ResolvedVc, Vc,
};
use turbopack_core::module::Modules;
use crate::paths::ServerPath;
......@@ -30,43 +32,28 @@ impl AppPageRoute {
#[derive(Clone, Debug)]
pub enum Route {
Page {
html_endpoint: Vc<Box<dyn Endpoint>>,
data_endpoint: Vc<Box<dyn Endpoint>>,
html_endpoint: ResolvedVc<Box<dyn Endpoint>>,
data_endpoint: ResolvedVc<Box<dyn Endpoint>>,
},
PageApi {
endpoint: Vc<Box<dyn Endpoint>>,
endpoint: ResolvedVc<Box<dyn Endpoint>>,
},
AppPage(Vec<AppPageRoute>),
AppRoute {
original_name: String,
endpoint: Vc<Box<dyn Endpoint>>,
endpoint: ResolvedVc<Box<dyn Endpoint>>,
},
Conflict,
}
impl Route {
pub async fn resolve(&mut self) -> Result<()> {
match self {
Route::Page {
html_endpoint,
data_endpoint,
} => {
*html_endpoint = html_endpoint.resolve().await?;
*data_endpoint = data_endpoint.resolve().await?;
if let Route::AppPage(routes) = self {
for route in routes {
route.resolve().await?;
}
Route::PageApi { endpoint } => {
*endpoint = endpoint.resolve().await?;
}
Route::AppPage(routes) => {
for route in routes {
route.resolve().await?;
}
}
Route::AppRoute { endpoint, .. } => {
*endpoint = endpoint.resolve().await?;
}
Route::Conflict => {}
}
Ok(())
}
}
......
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