Skip to content

Commit

Permalink
fix(turbopack): custom page extensions for _app
Browse files Browse the repository at this point in the history
  • Loading branch information
ForsakenHarmony committed Jan 17, 2024
1 parent 8d4e5be commit 0f4d67f
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 87 deletions.
12 changes: 11 additions & 1 deletion packages/next-swc/crates/next-api/src/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,14 @@ impl PagesProject {

#[turbo_tasks::function]
pub async fn routes(self: Vc<Self>) -> Result<Vc<Routes>> {
let pages_structure = self.pages_structure();
let PagesStructure {
api,
pages,
app: _,
document: _,
error: _,
} = &*self.pages_structure().await?;
} = &*pages_structure.await?;
let mut routes = IndexMap::new();

async fn add_page_to_routes(
Expand Down Expand Up @@ -150,6 +151,7 @@ impl PagesProject {
pathname,
original_name,
path,
pages_structure,
)),
}
})
Expand All @@ -163,13 +165,15 @@ impl PagesProject {
pathname,
original_name,
path,
pages_structure,
)),
data_endpoint: Vc::upcast(PageEndpoint::new(
PageEndpointType::Data,
self,
pathname,
original_name,
path,
pages_structure,
)),
};

Expand Down Expand Up @@ -201,6 +205,7 @@ impl PagesProject {
pathname_vc,
original_name,
path,
self.pages_structure(),
));
Ok(endpoint)
}
Expand Down Expand Up @@ -524,6 +529,7 @@ struct PageEndpoint {
pathname: Vc<String>,
original_name: Vc<String>,
path: Vc<FileSystemPath>,
pages_structure: Vc<PagesStructure>,
}

#[derive(Copy, Clone, Serialize, Deserialize, PartialEq, Eq, Debug, TaskInput, TraceRawVcs)]
Expand All @@ -543,13 +549,15 @@ impl PageEndpoint {
pathname: Vc<String>,
original_name: Vc<String>,
path: Vc<FileSystemPath>,
pages_structure: Vc<PagesStructure>,
) -> Vc<Self> {
PageEndpoint {
ty,
pages_project,
pathname,
original_name,
path,
pages_structure,
}
.cell()
}
Expand Down Expand Up @@ -661,6 +669,7 @@ impl PageEndpoint {
Vc::upcast(edge_module_context),
self.source(),
this.original_name,
this.pages_structure,
config.runtime,
this.pages_project.project().next_config(),
);
Expand Down Expand Up @@ -698,6 +707,7 @@ impl PageEndpoint {
Vc::upcast(module_context),
self.source(),
this.original_name,
this.pages_structure,
config.runtime,
this.pages_project.project().next_config(),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ async fn get_page_entry_for_file(
ssr_module_context,
source,
Vc::cell(original_name),
todo_get_pages_structure(),
NextRuntime::NodeJs,
next_config,
);
Expand All @@ -370,6 +371,10 @@ async fn get_page_entry_for_file(
.cell())
}

fn todo_get_pages_structure() -> Vc<PagesStructure> {
todo!("add pages structure to next-build");
}

/// Computes the pathname for a given path.
#[turbo_tasks::function]
async fn pathname_from_path(next_router_path: Vc<FileSystemPath>) -> Result<Vc<String>> {
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

54 changes: 2 additions & 52 deletions packages/next-swc/crates/next-core/src/next_import_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,32 +66,7 @@ pub async fn get_next_client_import_map(
.await?;

match ty.into_value() {
ClientContextType::Pages { pages_dir } => {
insert_alias_to_alternatives(
&mut import_map,
format!("{VIRTUAL_PACKAGE_NAME}/pages/_app"),
vec![
request_to_import_mapping(pages_dir, "./_app"),
request_to_import_mapping(pages_dir, "next/app"),
],
);
insert_alias_to_alternatives(
&mut import_map,
format!("{VIRTUAL_PACKAGE_NAME}/pages/_document"),
vec![
request_to_import_mapping(pages_dir, "./_document"),
request_to_import_mapping(pages_dir, "next/document"),
],
);
insert_alias_to_alternatives(
&mut import_map,
format!("{VIRTUAL_PACKAGE_NAME}/pages/_error"),
vec![
request_to_import_mapping(pages_dir, "./_error"),
request_to_import_mapping(pages_dir, "next/error"),
],
);
}
ClientContextType::Pages { .. } => {}
ClientContextType::App { app_dir } => {
let react_flavor =
if *next_config.enable_ppr().await? || *next_config.enable_taint().await? {
Expand Down Expand Up @@ -522,32 +497,7 @@ async fn insert_next_server_special_aliases(
);

match ty {
ServerContextType::Pages { pages_dir } | ServerContextType::PagesApi { pages_dir } => {
insert_alias_to_alternatives(
import_map,
format!("{VIRTUAL_PACKAGE_NAME}/pages/_app"),
vec![
request_to_import_mapping(pages_dir, "./_app"),
external_if_node(pages_dir, "next/app"),
],
);
insert_alias_to_alternatives(
import_map,
format!("{VIRTUAL_PACKAGE_NAME}/pages/_document"),
vec![
request_to_import_mapping(pages_dir, "./_document"),
external_if_node(pages_dir, "next/document"),
],
);
insert_alias_to_alternatives(
import_map,
format!("{VIRTUAL_PACKAGE_NAME}/pages/_error"),
vec![
request_to_import_mapping(pages_dir, "./_error"),
external_if_node(pages_dir, "next/error"),
],
);
}
ServerContextType::Pages { .. } | ServerContextType::PagesApi { .. } => {}
ServerContextType::PagesData { .. } => {}
// the logic closely follows the one in createRSCAliases in webpack-config.ts
ServerContextType::AppSSR { app_dir }
Expand Down
71 changes: 55 additions & 16 deletions packages/next-swc/crates/next-core/src/next_pages/page_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ use turbopack_binding::{
core::{
asset::{Asset, AssetContent},
context::AssetContext,
file_source::FileSource,
module::Module,
reference_type::{EntryReferenceSubType, ReferenceType},
reference_type::{EntryReferenceSubType, InnerAssets, ReferenceType},
source::Source,
virtual_source::VirtualSource,
},
Expand All @@ -26,6 +27,7 @@ use turbopack_binding::{
use crate::{
next_config::NextConfig,
next_edge::entry::wrap_edge_entry,
pages_structure::{PagesStructure, PagesStructureItem},
util::{file_content_rope, load_next_js_template, NextRuntime},
};

Expand All @@ -37,6 +39,7 @@ pub async fn create_page_ssr_entry_module(
ssr_module_context: Vc<Box<dyn AssetContext>>,
source: Vc<Box<dyn Source>>,
next_original_name: Vc<String>,
pages_structure: Vc<PagesStructure>,
runtime: NextRuntime,
next_config: Vc<NextConfig>,
) -> Result<Vc<Box<dyn EcmascriptChunkPlaceable>>> {
Expand Down Expand Up @@ -67,21 +70,18 @@ pub async fn create_page_ssr_entry_module(

const INNER: &str = "INNER_PAGE";

const INNER_DOCUMENT: &str = "INNER_DOCUMENT";
const INNER_APP: &str = "INNER_APP";

let mut replacements = indexmap! {
"VAR_DEFINITION_PAGE" => definition_page.clone(),
"VAR_DEFINITION_PATHNAME" => definition_pathname.clone(),
"VAR_USERLAND" => INNER.to_string(),
};

if reference_type == ReferenceType::Entry(EntryReferenceSubType::Page) {
replacements.insert(
"VAR_MODULE_DOCUMENT",
"@vercel/turbopack-next/pages/_document".to_string(),
);
replacements.insert(
"VAR_MODULE_APP",
"@vercel/turbopack-next/pages/_app".to_string(),
);
replacements.insert("VAR_MODULE_DOCUMENT", INNER_DOCUMENT.to_string());
replacements.insert("VAR_MODULE_APP", INNER_APP.to_string());
}

// Load the file from the next.js codebase.
Expand Down Expand Up @@ -118,12 +118,25 @@ pub async fn create_page_ssr_entry_module(
));
}

let mut inner_assets = indexmap! {
INNER.to_string() => ssr_module,
};

if reference_type == ReferenceType::Entry(EntryReferenceSubType::Page) {
inner_assets.insert(
INNER_DOCUMENT.to_string(),
process_global_item(pages_structure.document(), ssr_module_context),
);
inner_assets.insert(
INNER_APP.to_string(),
process_global_item(pages_structure.app(), ssr_module_context),
);
}

let mut ssr_module = ssr_module_context
.process(
source,
Value::new(ReferenceType::Internal(Vc::cell(indexmap! {
INNER.to_string() => ssr_module,
}))),
Value::new(ReferenceType::Internal(Vc::cell(inner_assets))),
)
.module();

Expand All @@ -135,6 +148,7 @@ pub async fn create_page_ssr_entry_module(
ssr_module,
definition_page.clone(),
definition_pathname.clone(),
pages_structure,
next_config,
);
} else {
Expand All @@ -156,17 +170,39 @@ pub async fn create_page_ssr_entry_module(
Ok(ssr_module)
}

#[turbo_tasks::function]
async fn process_global_item(
item: Vc<PagesStructureItem>,
module_context: Vc<Box<dyn AssetContext>>,
) -> Result<Vc<Box<dyn Module>>> {
let source = Vc::upcast(FileSource::new(item.project_path()));

let module = module_context
.process(
source,
Value::new(ReferenceType::Internal(InnerAssets::empty())),
)
.module();

Ok(module)
}

#[turbo_tasks::function]
async fn wrap_edge_page(
context: Vc<Box<dyn AssetContext>>,
project_root: Vc<FileSystemPath>,
entry: Vc<Box<dyn Module>>,
page: String,
pathname: String,
pages_structure: Vc<PagesStructure>,
next_config: Vc<NextConfig>,
) -> Result<Vc<Box<dyn Module>>> {
const INNER: &str = "INNER_PAGE_ENTRY";

const INNER_DOCUMENT: &str = "INNER_DOCUMENT";
const INNER_APP: &str = "INNER_APP";
const INNER_ERROR: &str = "INNER_ERROR";

let next_config = &*next_config.await?;

// TODO(WEB-1824): add build support
Expand All @@ -188,9 +224,9 @@ async fn wrap_edge_page(
"VAR_USERLAND" => INNER.to_string(),
"VAR_PAGE" => pathname.clone(),
"VAR_BUILD_ID" => build_id.to_string(),
"VAR_MODULE_DOCUMENT" => "@vercel/turbopack-next/pages/_document".to_string(),
"VAR_MODULE_APP" => "@vercel/turbopack-next/pages/_app".to_string(),
"VAR_MODULE_GLOBAL_ERROR" => "@vercel/turbopack-next/pages/_error".to_string(),
"VAR_MODULE_DOCUMENT" => INNER_DOCUMENT.to_string(),
"VAR_MODULE_APP" => INNER_APP.to_string(),
"VAR_MODULE_GLOBAL_ERROR" => INNER_ERROR.to_string(),
},
indexmap! {
"pagesType" => StringifyJs("pages").to_string(),
Expand All @@ -210,7 +246,10 @@ async fn wrap_edge_page(
.await?;

let inner_assets = indexmap! {
INNER.to_string() => entry
INNER.to_string() => entry,
INNER_DOCUMENT.to_string() => process_global_item(pages_structure.document(), context),
INNER_APP.to_string() => process_global_item(pages_structure.app(), context),
INNER_ERROR.to_string() => process_global_item(pages_structure.error(), context),
};

let wrapped = context
Expand Down
Loading

0 comments on commit 0f4d67f

Please sign in to comment.