Skip to content

Commit

Permalink
Rename app directory "components" to "modules"
Browse files Browse the repository at this point in the history
This is a pure refactoring. Not every reserved file in the app directory
represents a component module. Currently, `route` is the only exception,
but a future PR will introduce another one.
  • Loading branch information
unstubbable committed Sep 20, 2024
1 parent d2274c1 commit a26375c
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 130 deletions.
50 changes: 27 additions & 23 deletions crates/next-core/src/app_page_loader_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ use turbopack_ecmascript::{magic_identifier, text::TextContentFileSource, utils:

use crate::{
app_structure::{
get_metadata_route_name, AppPageLoaderTree, Components, GlobalMetadata, Metadata,
get_metadata_route_name, AppDirModules, AppPageLoaderTree, GlobalMetadata, Metadata,
MetadataItem, MetadataWithAltItem,
},
base_loader_tree::{BaseLoaderTreeBuilder, ComponentType},
base_loader_tree::{AppDirModuleType, BaseLoaderTreeBuilder},
next_app::{
metadata::{get_content_type, image::dynamic_image_metadata_source},
AppPage,
Expand Down Expand Up @@ -51,25 +51,25 @@ impl AppPageLoaderTreeBuilder {
}
}

async fn write_component(
async fn write_modules_entry(
&mut self,
component_type: ComponentType,
module_type: AppDirModuleType,
path: Option<Vc<FileSystemPath>>,
) -> Result<()> {
if let Some(path) = path {
if matches!(component_type, ComponentType::Page) {
if matches!(module_type, AppDirModuleType::Page) {
self.pages.push(path);
}

let tuple_code = self
.base
.create_component_tuple_code(component_type, path)
.create_module_tuple_code(module_type, path)
.await?;

writeln!(
self.loader_tree_code,
" {name}: {tuple_code},",
name = StringifyJs(component_type.name())
name = StringifyJs(module_type.name())
)?;
}
Ok(())
Expand Down Expand Up @@ -314,7 +314,7 @@ impl AppPageLoaderTreeBuilder {
page: app_page,
segment,
parallel_routes,
components,
modules,
global_metadata,
} = loader_tree;

Expand All @@ -324,10 +324,9 @@ impl AppPageLoaderTreeBuilder {
segment = StringifyJs(segment)
)?;

// Components need to be referenced first
let temp_loader_tree_code = take(&mut self.loader_tree_code);
// add components
let Components {

let AppDirModules {
page,
default,
error,
Expand All @@ -338,19 +337,24 @@ impl AppPageLoaderTreeBuilder {
not_found,
metadata,
route: _,
} = &components;
self.write_component(ComponentType::Layout, *layout).await?;
self.write_component(ComponentType::Page, *page).await?;
self.write_component(ComponentType::DefaultPage, *default)
} = &modules;

self.write_modules_entry(AppDirModuleType::Layout, *layout)
.await?;
self.write_modules_entry(AppDirModuleType::Page, *page)
.await?;
self.write_component(ComponentType::Error, *error).await?;
self.write_component(ComponentType::Loading, *loading)
self.write_modules_entry(AppDirModuleType::DefaultPage, *default)
.await?;
self.write_component(ComponentType::Template, *template)
self.write_modules_entry(AppDirModuleType::Error, *error)
.await?;
self.write_component(ComponentType::NotFound, *not_found)
self.write_modules_entry(AppDirModuleType::Loading, *loading)
.await?;
let components_code = replace(&mut self.loader_tree_code, temp_loader_tree_code);
self.write_modules_entry(AppDirModuleType::Template, *template)
.await?;
self.write_modules_entry(AppDirModuleType::NotFound, *not_found)
.await?;

let modules_code = replace(&mut self.loader_tree_code, temp_loader_tree_code);

// add parallel_routes
for (key, parallel_route) in parallel_routes.iter() {
Expand All @@ -360,7 +364,7 @@ impl AppPageLoaderTreeBuilder {
}
writeln!(self.loader_tree_code, "}}, {{")?;

self.loader_tree_code += &components_code;
self.loader_tree_code += &modules_code;

// Ensure global metadata being written only once at the root level
// Otherwise child pages will have redundant metadata
Expand All @@ -382,8 +386,8 @@ impl AppPageLoaderTreeBuilder {
) -> Result<AppPageLoaderTreeModule> {
let loader_tree = &*loader_tree.await?;

let components = &loader_tree.components;
if let Some(global_error) = components.global_error {
let modules = &loader_tree.modules;
if let Some(global_error) = modules.global_error {
let module = self.base.process_module(global_error);
self.base.inner_assets.insert(GLOBAL_ERROR.into(), module);
};
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 @@ -495,12 +495,12 @@ pub async fn parse_segment_config_from_loader_tree_internal(
config.apply_parallel_config(&tree)?;
}

let components = &loader_tree.components;
for component in [components.page, components.default, components.layout]
let modules = &loader_tree.modules;
for path in [modules.page, modules.default, modules.layout]
.into_iter()
.flatten()
{
let source = Vc::upcast(FileSource::new(component));
let source = Vc::upcast(FileSource::new(path));
config.apply_parent_config(&*parse_segment_config_from_source(source).await?);
}

Expand Down
Loading

0 comments on commit a26375c

Please sign in to comment.