-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored module IDs optimization (#68846)
### What? Refactored module ID strategies implementation to walk modules graph individually for each endpoint. Based on feedback from [next.js#68408](#68408) and [turbo#8912](vercel/turborepo#8912). Comments marked with `NOTE(LichuAcu)` are intended to make reviewing easier and will be removed before merging.
- Loading branch information
lichu acuña
authored
Aug 23, 2024
1 parent
ba99599
commit 9ecd1a2
Showing
21 changed files
with
558 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use anyhow::Result; | ||
use turbo_tasks::Vc; | ||
use turbopack_core::chunk::{ | ||
global_module_id_strategy::{ | ||
children_modules_idents, merge_preprocessed_module_ids, PreprocessedChildrenIdents, | ||
}, | ||
module_id_strategies::{GlobalModuleIdStrategy, ModuleIdStrategy}, | ||
}; | ||
|
||
use crate::{ | ||
project::Project, | ||
route::{Endpoint, Route}, | ||
}; | ||
|
||
#[turbo_tasks::value] | ||
pub struct GlobalModuleIdStrategyBuilder; | ||
|
||
// NOTE(LichuAcu) To access all entrypoints, we need to access an instance of `Project`, but | ||
// `Project` is not available in `turbopack-core`, so we need need this | ||
// `GlobalModuleIdStrategyBuilder` in `next-api`. | ||
#[turbo_tasks::value_impl] | ||
impl GlobalModuleIdStrategyBuilder { | ||
#[turbo_tasks::function] | ||
pub async fn build(project: Vc<Project>) -> Result<Vc<Box<dyn ModuleIdStrategy>>> { | ||
let mut preprocessed_module_ids = Vec::new(); | ||
|
||
preprocessed_module_ids.push(children_modules_idents(project.client_main_modules())); | ||
|
||
let entrypoints = project.entrypoints().await?; | ||
|
||
preprocessed_module_ids.push(preprocess_module_ids(entrypoints.pages_error_endpoint)); | ||
preprocessed_module_ids.push(preprocess_module_ids(entrypoints.pages_app_endpoint)); | ||
preprocessed_module_ids.push(preprocess_module_ids(entrypoints.pages_document_endpoint)); | ||
|
||
for (_, route) in entrypoints.routes.iter() { | ||
match route { | ||
Route::Page { | ||
html_endpoint, | ||
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)); | ||
} | ||
Route::AppPage(page_routes) => { | ||
for page_route in page_routes { | ||
preprocessed_module_ids | ||
.push(preprocess_module_ids(page_route.html_endpoint)); | ||
preprocessed_module_ids | ||
.push(preprocess_module_ids(page_route.rsc_endpoint)); | ||
} | ||
} | ||
Route::AppRoute { | ||
original_name: _, | ||
endpoint, | ||
} => { | ||
preprocessed_module_ids.push(preprocess_module_ids(*endpoint)); | ||
} | ||
Route::Conflict => { | ||
tracing::info!("WARN: conflict"); | ||
} | ||
} | ||
} | ||
|
||
let module_id_map = merge_preprocessed_module_ids(preprocessed_module_ids).await?; | ||
|
||
Ok(Vc::upcast( | ||
GlobalModuleIdStrategy::new(module_id_map).await?, | ||
)) | ||
} | ||
} | ||
|
||
// NOTE(LichuAcu) We can't move this function to `turbopack-core` because we need access to | ||
// `Endpoint`, which is not available there. | ||
#[turbo_tasks::function] | ||
async fn preprocess_module_ids( | ||
endpoint: Vc<Box<dyn Endpoint>>, | ||
) -> Result<Vc<PreprocessedChildrenIdents>> { | ||
let root_modules = endpoint.root_modules(); | ||
Ok(children_modules_idents(root_modules)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.