Skip to content

Commit

Permalink
Rename loader_tree to app_page_loader_tree
Browse files Browse the repository at this point in the history
This is a pure refactoring in preparation for introducing an
`app_route_loader_tree` in a future PR.

The `app_page_loader_tree` collects information from the app directory
for app _page_ entries, whereas the upcoming `app_route_loader_tree`
will collect information from the app directory tailored to app _route_
entries.
  • Loading branch information
unstubbable committed Sep 20, 2024
1 parent 9e38728 commit a74d31c
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 44 deletions.
8 changes: 4 additions & 4 deletions crates/next-api/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use next_core::{
all_assets_from_entries,
app_segment_config::NextSegmentConfig,
app_structure::{
get_entrypoints, Entrypoint as AppEntrypoint, Entrypoints as AppEntrypoints, LoaderTree,
MetadataItem,
get_entrypoints, AppPageLoaderTree, Entrypoint as AppEntrypoint,
Entrypoints as AppEntrypoints, MetadataItem,
},
get_edge_resolve_options_context, get_next_package,
next_app::{
Expand Down Expand Up @@ -696,7 +696,7 @@ enum AppPageEndpointType {
enum AppEndpointType {
Page {
ty: AppPageEndpointType,
loader_tree: Vc<LoaderTree>,
loader_tree: Vc<AppPageLoaderTree>,
},
Route {
path: Vc<FileSystemPath>,
Expand All @@ -717,7 +717,7 @@ struct AppEndpoint {
#[turbo_tasks::value_impl]
impl AppEndpoint {
#[turbo_tasks::function]
fn app_page_entry(&self, loader_tree: Vc<LoaderTree>) -> Vc<AppEntry> {
fn app_page_entry(&self, loader_tree: Vc<AppPageLoaderTree>) -> Vc<AppEntry> {
get_app_page_entry(
self.app_project.rsc_module_context(),
self.app_project.edge_rsc_module_context(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use turbopack_ecmascript::{magic_identifier, text::TextContentFileSource, utils:

use crate::{
app_structure::{
get_metadata_route_name, Components, GlobalMetadata, LoaderTree, Metadata, MetadataItem,
MetadataWithAltItem,
get_metadata_route_name, AppPageLoaderTree, Components, GlobalMetadata, Metadata,
MetadataItem, MetadataWithAltItem,
},
next_app::{
metadata::{get_content_type, image::dynamic_image_metadata_source},
Expand All @@ -29,7 +29,7 @@ use crate::{
next_image::module::{BlurPlaceholderMode, StructuredImageModuleType},
};

pub struct LoaderTreeBuilder {
pub struct AppPageLoaderTreeBuilder {
inner_assets: IndexMap<RcStr, Vc<Box<dyn Module>>>,
counter: usize,
imports: Vec<RcStr>,
Expand Down Expand Up @@ -66,13 +66,13 @@ impl ComponentType {
}
}

impl LoaderTreeBuilder {
impl AppPageLoaderTreeBuilder {
fn new(
module_asset_context: Vc<ModuleAssetContext>,
server_component_transition: Vc<Box<dyn Transition>>,
base_path: Option<RcStr>,
) -> Self {
LoaderTreeBuilder {
AppPageLoaderTreeBuilder {
inner_assets: IndexMap::new(),
counter: 0,
imports: Vec::new(),
Expand Down Expand Up @@ -354,10 +354,10 @@ impl LoaderTreeBuilder {
Ok(())
}

async fn walk_tree(&mut self, loader_tree: &LoaderTree, root: bool) -> Result<()> {
async fn walk_tree(&mut self, loader_tree: &AppPageLoaderTree, root: bool) -> Result<()> {
use std::fmt::Write;

let LoaderTree {
let AppPageLoaderTree {
page: app_page,
segment,
parallel_routes,
Expand Down Expand Up @@ -423,7 +423,10 @@ impl LoaderTreeBuilder {
Ok(())
}

async fn build(mut self, loader_tree: Vc<LoaderTree>) -> Result<LoaderTreeModule> {
async fn build(
mut self,
loader_tree: Vc<AppPageLoaderTree>,
) -> Result<AppPageLoaderTreeModule> {
let loader_tree = &*loader_tree.await?;

let components = &loader_tree.components;
Expand All @@ -437,7 +440,7 @@ impl LoaderTreeBuilder {
};

self.walk_tree(loader_tree, true).await?;
Ok(LoaderTreeModule {
Ok(AppPageLoaderTreeModule {
imports: self.imports,
loader_tree_code: self.loader_tree_code.into(),
inner_assets: self.inner_assets,
Expand All @@ -446,21 +449,21 @@ impl LoaderTreeBuilder {
}
}

pub struct LoaderTreeModule {
pub struct AppPageLoaderTreeModule {
pub imports: Vec<RcStr>,
pub loader_tree_code: RcStr,
pub inner_assets: IndexMap<RcStr, Vc<Box<dyn Module>>>,
pub pages: Vec<Vc<FileSystemPath>>,
}

impl LoaderTreeModule {
impl AppPageLoaderTreeModule {
pub async fn build(
loader_tree: Vc<LoaderTree>,
loader_tree: Vc<AppPageLoaderTree>,
module_asset_context: Vc<ModuleAssetContext>,
server_component_transition: Vc<Box<dyn Transition>>,
base_path: Option<RcStr>,
) -> Result<Self> {
LoaderTreeBuilder::new(module_asset_context, server_component_transition, base_path)
AppPageLoaderTreeBuilder::new(module_asset_context, server_component_transition, base_path)
.build(loader_tree)
.await
}
Expand Down
6 changes: 3 additions & 3 deletions crates/next-core/src/app_segment_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use turbopack_ecmascript::{
EcmascriptInputTransforms, EcmascriptModuleAssetType,
};

use crate::{app_structure::LoaderTree, util::NextRuntime};
use crate::{app_structure::AppPageLoaderTree, util::NextRuntime};

#[derive(Default, PartialEq, Eq, Clone, Copy, Debug, TraceRawVcs, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
Expand Down Expand Up @@ -468,7 +468,7 @@ fn parse_config_value(

#[turbo_tasks::function]
pub async fn parse_segment_config_from_loader_tree(
loader_tree: Vc<LoaderTree>,
loader_tree: Vc<AppPageLoaderTree>,
) -> Result<Vc<NextSegmentConfig>> {
let loader_tree = &*loader_tree.await?;

Expand All @@ -478,7 +478,7 @@ pub async fn parse_segment_config_from_loader_tree(
}

pub async fn parse_segment_config_from_loader_tree_internal(
loader_tree: &LoaderTree,
loader_tree: &AppPageLoaderTree,
) -> Result<NextSegmentConfig> {
let mut config = NextSegmentConfig::default();

Expand Down
32 changes: 16 additions & 16 deletions crates/next-core/src/app_structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,15 +398,15 @@ async fn get_directory_tree_internal(

#[turbo_tasks::value]
#[derive(Debug, Clone)]
pub struct LoaderTree {
pub struct AppPageLoaderTree {
pub page: AppPage,
pub segment: RcStr,
pub parallel_routes: IndexMap<RcStr, LoaderTree>,
pub parallel_routes: IndexMap<RcStr, AppPageLoaderTree>,
pub components: Components,
pub global_metadata: Vc<GlobalMetadata>,
}

impl LoaderTree {
impl AppPageLoaderTree {
/// Returns true if there's a page match in this loader tree.
pub fn has_page(&self) -> bool {
if &*self.segment == "__PAGE__" {
Expand Down Expand Up @@ -485,7 +485,7 @@ impl LoaderTree {
pub enum Entrypoint {
AppPage {
pages: Vec<AppPage>,
loader_tree: Vc<LoaderTree>,
loader_tree: Vc<AppPageLoaderTree>,
},
AppRoute {
page: AppPage,
Expand Down Expand Up @@ -558,7 +558,7 @@ fn add_app_page(
app_dir: Vc<FileSystemPath>,
result: &mut IndexMap<AppPath, Entrypoint>,
page: AppPage,
loader_tree: Vc<LoaderTree>,
loader_tree: Vc<AppPageLoaderTree>,
) {
let mut e = match result.entry(page.clone().into()) {
Entry::Occupied(e) => e,
Expand Down Expand Up @@ -752,7 +752,7 @@ impl Issue for DuplicateParallelRouteIssue {
}
}

fn page_path_except_parallel(loader_tree: &LoaderTree) -> Option<AppPage> {
fn page_path_except_parallel(loader_tree: &AppPageLoaderTree) -> Option<AppPage> {
if loader_tree.page.iter().any(|v| {
matches!(
v,
Expand All @@ -777,7 +777,7 @@ fn page_path_except_parallel(loader_tree: &LoaderTree) -> Option<AppPage> {

fn check_duplicate(
duplicate: &mut FxHashMap<AppPath, AppPage>,
loader_tree: &LoaderTree,
loader_tree: &AppPageLoaderTree,
app_dir: Vc<FileSystemPath>,
) {
let page_path = page_path_except_parallel(loader_tree);
Expand Down Expand Up @@ -806,7 +806,7 @@ async fn directory_tree_to_loader_tree(
app_page: AppPage,
// the page this loader tree is constructed for
for_app_path: AppPath,
) -> Result<Vc<Option<Vc<LoaderTree>>>> {
) -> Result<Vc<Option<Vc<AppPageLoaderTree>>>> {
let plain_tree = &*directory_tree.into_plain().await?;

let tree = directory_tree_to_loader_tree_internal(
Expand All @@ -829,7 +829,7 @@ fn directory_tree_to_loader_tree_internal(
app_page: AppPage,
// the page this loader tree is constructed for
for_app_path: AppPath,
) -> Result<Option<LoaderTree>> {
) -> Result<Option<AppPageLoaderTree>> {
let app_path = AppPath::from(app_page.clone());

if !for_app_path.contains(&app_path) {
Expand All @@ -854,7 +854,7 @@ fn directory_tree_to_loader_tree_internal(
);
}

let mut tree = LoaderTree {
let mut tree = AppPageLoaderTree {
page: app_page.clone(),
segment: directory_name.clone(),
parallel_routes: IndexMap::new(),
Expand All @@ -874,7 +874,7 @@ fn directory_tree_to_loader_tree_internal(
{
tree.parallel_routes.insert(
"children".into(),
LoaderTree {
AppPageLoaderTree {
page: app_page.clone(),
segment: "__PAGE__".into(),
parallel_routes: IndexMap::new(),
Expand Down Expand Up @@ -1017,8 +1017,8 @@ fn default_route_tree(
global_metadata: Vc<GlobalMetadata>,
app_page: AppPage,
default_component: Option<Vc<FileSystemPath>>,
) -> LoaderTree {
LoaderTree {
) -> AppPageLoaderTree {
AppPageLoaderTree {
page: app_page.clone(),
segment: "__DEFAULT__".into(),
parallel_routes: IndexMap::new(),
Expand Down Expand Up @@ -1168,15 +1168,15 @@ async fn directory_tree_to_entrypoints_internal_untraced(

// Next.js has this logic in "collect-app-paths", where the root not-found page
// is considered as its own entry point.
let not_found_tree = LoaderTree {
let not_found_tree = AppPageLoaderTree {
page: app_page.clone(),
segment: directory_name.clone(),
parallel_routes: indexmap! {
"children".into() => LoaderTree {
"children".into() => AppPageLoaderTree {
page: app_page.clone(),
segment: "/_not-found".into(),
parallel_routes: indexmap! {
"children".into() => LoaderTree {
"children".into() => AppPageLoaderTree {
page: app_page.clone(),
segment: "__PAGE__".into(),
parallel_routes: IndexMap::new(),
Expand Down
2 changes: 1 addition & 1 deletion crates/next-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
#![feature(arbitrary_self_types)]
#![feature(iter_intersperse)]

mod app_page_loader_tree;
pub mod app_segment_config;
pub mod app_structure;
mod bootstrap;
mod embed_js;
mod emit;
pub mod hmr_entry;
pub mod instrumentation;
mod loader_tree;
pub mod middleware;
pub mod mode;
pub mod next_app;
Expand Down
10 changes: 5 additions & 5 deletions crates/next-core/src/next_app/app_page_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use turbopack_ecmascript::utils::StringifyJs;

use super::app_entry::AppEntry;
use crate::{
app_structure::LoaderTree,
loader_tree::{LoaderTreeModule, GLOBAL_ERROR},
app_page_loader_tree::{AppPageLoaderTreeModule, GLOBAL_ERROR},
app_structure::AppPageLoaderTree,
next_app::{AppPage, AppPath},
next_config::NextConfig,
next_edge::entry::wrap_edge_entry,
Expand All @@ -32,7 +32,7 @@ use crate::{
pub async fn get_app_page_entry(
nodejs_context: Vc<ModuleAssetContext>,
edge_context: Vc<ModuleAssetContext>,
loader_tree: Vc<LoaderTree>,
loader_tree: Vc<AppPageLoaderTree>,
page: AppPage,
project_root: Vc<FileSystemPath>,
next_config: Vc<NextConfig>,
Expand All @@ -48,15 +48,15 @@ pub async fn get_app_page_entry(
let server_component_transition = Vc::upcast(NextServerComponentTransition::new());

let base_path = next_config.await?.base_path.clone();
let loader_tree = LoaderTreeModule::build(
let loader_tree = AppPageLoaderTreeModule::build(
loader_tree,
module_asset_context,
server_component_transition,
base_path,
)
.await?;

let LoaderTreeModule {
let AppPageLoaderTreeModule {
inner_assets,
imports,
loader_tree_code,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ interface Options {
basePath: string
}

// [NOTE] For turbopack
// refer loader_tree's write_static|dynamic_metadata for corresponding features
// [NOTE] For turbopack, refer to app_page_loader_tree's write_metadata_item for
// corresponding features.
async function nextMetadataImageLoader(
this: webpack.LoaderContext<Options>,
content: Buffer
Expand Down

0 comments on commit a74d31c

Please sign in to comment.