Unverified Commit 679a398e authored by Leah's avatar Leah Committed by GitHub

fix(turbopack): write manifests for edge app pages and use proper edge entry (#56675)

### Description

- We previously didn't write to the shared `middleware-manifest.json` for edge app pages
- We also need to manually write to the `_ENTRIES` object in turbopack entries because it currently doesn't have a way to change the output type like webpack: https://webpack.js.org/configuration/output/#type-assign-properties
- Also need to write a bunch of manifests as JS files to be able to load them from the edge runtime.
- Some import map fixes were needed as well.

This only fixes dev, build will need more work.

Closes WEB-1698
parent dc7b5d80
...@@ -242,13 +242,17 @@ impl AppProject { ...@@ -242,13 +242,17 @@ impl AppProject {
ECMASCRIPT_CLIENT_TRANSITION_NAME.to_string(), ECMASCRIPT_CLIENT_TRANSITION_NAME.to_string(),
Vc::upcast(NextEcmascriptClientReferenceTransition::new( Vc::upcast(NextEcmascriptClientReferenceTransition::new(
self.client_transition(), self.client_transition(),
self.ssr_transition(), self.edge_ssr_transition(),
)), )),
), ),
( (
"next-dynamic".to_string(), "next-dynamic".to_string(),
Vc::upcast(NextDynamicTransition::new(self.client_transition())), Vc::upcast(NextDynamicTransition::new(self.client_transition())),
), ),
(
"next-ssr".to_string(),
Vc::upcast(self.edge_ssr_transition()),
),
] ]
.into_iter() .into_iter()
.collect(); .collect();
...@@ -296,6 +300,18 @@ impl AppProject { ...@@ -296,6 +300,18 @@ impl AppProject {
)) ))
} }
#[turbo_tasks::function]
async fn edge_ssr_resolve_options_context(self: Vc<Self>) -> Result<Vc<ResolveOptionsContext>> {
let this = self.await?;
Ok(get_edge_resolve_options_context(
self.project().project_path(),
Value::new(self.ssr_ty()),
this.mode,
self.project().next_config(),
self.project().execution_context(),
))
}
#[turbo_tasks::function] #[turbo_tasks::function]
fn ssr_transition(self: Vc<Self>) -> Vc<ContextTransition> { fn ssr_transition(self: Vc<Self>) -> Vc<ContextTransition> {
ContextTransition::new( ContextTransition::new(
...@@ -306,6 +322,16 @@ impl AppProject { ...@@ -306,6 +322,16 @@ impl AppProject {
) )
} }
#[turbo_tasks::function]
fn edge_ssr_transition(self: Vc<Self>) -> Vc<ContextTransition> {
ContextTransition::new(
self.project().edge_compile_time_info(),
self.ssr_module_options_context(),
self.edge_ssr_resolve_options_context(),
Vc::cell("edge_ssr".to_string()),
)
}
#[turbo_tasks::function] #[turbo_tasks::function]
async fn runtime_entries(self: Vc<Self>) -> Result<Vc<RuntimeEntries>> { async fn runtime_entries(self: Vc<Self>) -> Result<Vc<RuntimeEntries>> {
let this = self.await?; let this = self.await?;
...@@ -460,6 +486,7 @@ impl AppEndpoint { ...@@ -460,6 +486,7 @@ impl AppEndpoint {
loader_tree, loader_tree,
self.page.clone(), self.page.clone(),
self.app_project.project().project_path(), self.app_project.project().project_path(),
self.app_project.project().next_config(),
) )
} }
...@@ -519,6 +546,8 @@ impl AppEndpoint { ...@@ -519,6 +546,8 @@ impl AppEndpoint {
let mut server_assets = vec![]; let mut server_assets = vec![];
let mut client_assets = vec![]; let mut client_assets = vec![];
// assets to add to the middleware manifest (to be loaded in the edge runtime).
let mut middleware_assets = vec![];
let app_entry = app_entry.await?; let app_entry = app_entry.await?;
...@@ -579,11 +608,20 @@ impl AppEndpoint { ...@@ -579,11 +608,20 @@ impl AppEndpoint {
.entry(Vc::upcast(app_entry.rsc_entry)) .entry(Vc::upcast(app_entry.rsc_entry))
.await?; .await?;
let runtime = app_entry.config.await?.runtime.unwrap_or_default();
if ssr_and_client { if ssr_and_client {
let ssr_chunking_context = match runtime {
NextRuntime::NodeJs => {
Vc::upcast(this.app_project.project().server_chunking_context())
}
NextRuntime::Edge => this.app_project.project().edge_chunking_context(),
};
let client_references_chunks = get_app_client_references_chunks( let client_references_chunks = get_app_client_references_chunks(
client_reference_types, client_reference_types,
this.app_project.project().client_chunking_context(), this.app_project.project().client_chunking_context(),
this.app_project.project().server_chunking_context(), ssr_chunking_context,
); );
let client_references_chunks_ref = client_references_chunks.await?; let client_references_chunks_ref = client_references_chunks.await?;
...@@ -655,13 +693,31 @@ impl AppEndpoint { ...@@ -655,13 +693,31 @@ impl AppEndpoint {
client_references, client_references,
client_references_chunks, client_references_chunks,
this.app_project.project().client_chunking_context(), this.app_project.project().client_chunking_context(),
Vc::upcast(this.app_project.project().server_chunking_context()), ssr_chunking_context,
this.app_project this.app_project
.project() .project()
.next_config() .next_config()
.computed_asset_prefix(), .computed_asset_prefix(),
runtime,
); );
server_assets.push(entry_manifest); server_assets.push(entry_manifest);
if runtime == NextRuntime::Edge {
middleware_assets.push(entry_manifest);
// as the edge runtime doesn't support chunk loading we need to add all client
// references to the middleware manifest so they get loaded during runtime
// initialization
let client_references_chunks = &*client_references_chunks.await?;
for (ty, chunks) in client_references_chunks {
if matches!(ty, ClientReferenceType::EcmascriptClientReference(..)) {
let ssr_chunks = &*chunks.ssr_chunks.await?;
middleware_assets.extend(ssr_chunks);
}
}
}
} }
fn create_app_paths_manifest( fn create_app_paths_manifest(
...@@ -791,36 +847,54 @@ impl AppEndpoint { ...@@ -791,36 +847,54 @@ impl AppEndpoint {
); );
server_assets.extend(files.await?.iter().copied()); server_assets.extend(files.await?.iter().copied());
// the next-edge-ssr-loader templates expect the manifests to be stored in
// global variables defined in these files
//
// they are created in `setup-dev-bundler.ts`
let mut file_paths_from_root = vec![
"server/server-reference-manifest.js".to_string(),
"server/middleware-build-manifest.js".to_string(),
"server/middleware-react-loadable-manifest.js".to_string(),
"server/next-font-manifest.js".to_string(),
];
let node_root_value = node_root.await?; let node_root_value = node_root.await?;
let files_paths_from_root = files
.await? let middleware_paths_from_root = middleware_assets
.iter() .iter()
.map(move |&file| { .map({
let node_root_value = node_root_value.clone();
move |&file| {
let node_root_value = node_root_value.clone(); let node_root_value = node_root_value.clone();
async move { async move {
Ok(node_root_value Ok(node_root_value
.get_path_to(&*file.ident().path().await?) .get_path_to(&*file.ident().path().await?)
.map(|path| path.to_string())) .map(|path| path.to_string()))
} }
}
}) })
.try_flat_join() .try_flat_join()
.await?; .await?;
let server_path_value = server_path.await?; file_paths_from_root.extend(middleware_paths_from_root);
let files_paths_from_server = files
let rsc_paths_from_root = files
.await? .await?
.iter() .iter()
.map(move |&file| { .map(move |&file| {
let server_path_value = server_path_value.clone(); let node_root_value = node_root_value.clone();
async move { async move {
Ok(server_path_value Ok(node_root_value
.get_path_to(&*file.ident().path().await?) .get_path_to(&*file.ident().path().await?)
.map(|path| path.to_string())) .map(|path| path.to_string()))
} }
}) })
.try_flat_join() .try_flat_join()
.await?; .await?;
let base_file = files_paths_from_server[0].to_string();
file_paths_from_root.extend(rsc_paths_from_root);
let entry_file = "app-edge-has-no-entrypoint".to_string();
// create middleware manifest // create middleware manifest
// TODO(alexkirsz) This should be shared with next build. // TODO(alexkirsz) This should be shared with next build.
...@@ -831,7 +905,7 @@ impl AppEndpoint { ...@@ -831,7 +905,7 @@ impl AppEndpoint {
..Default::default() ..Default::default()
}; };
let edge_function_definition = EdgeFunctionDefinition { let edge_function_definition = EdgeFunctionDefinition {
files: files_paths_from_root, files: file_paths_from_root,
name: app_entry.pathname.to_string(), name: app_entry.pathname.to_string(),
page: app_entry.original_name.clone(), page: app_entry.original_name.clone(),
regions: app_entry regions: app_entry
...@@ -870,7 +944,7 @@ impl AppEndpoint { ...@@ -870,7 +944,7 @@ impl AppEndpoint {
ty, ty,
&app_entry.pathname, &app_entry.pathname,
&app_entry.original_name, &app_entry.original_name,
base_file, entry_file,
)?; )?;
server_assets.push(app_paths_manifest_output); server_assets.push(app_paths_manifest_output);
......
...@@ -612,6 +612,7 @@ impl PageEndpoint { ...@@ -612,6 +612,7 @@ impl PageEndpoint {
self.source(), self.source(),
this.original_name, this.original_name,
config.runtime, config.runtime,
this.pages_project.project().next_config(),
); );
let mut evaluatable_assets = edge_runtime_entries.await?.clone_value(); let mut evaluatable_assets = edge_runtime_entries.await?.clone_value();
...@@ -645,6 +646,7 @@ impl PageEndpoint { ...@@ -645,6 +646,7 @@ impl PageEndpoint {
self.source(), self.source(),
this.original_name, this.original_name,
config.runtime, config.runtime,
this.pages_project.project().next_config(),
); );
let asset_path = get_asset_path_from_pathname(&this.pathname.await?, ".js"); let asset_path = get_asset_path_from_pathname(&this.pathname.await?, ".js");
...@@ -926,8 +928,19 @@ impl PageEndpoint { ...@@ -926,8 +928,19 @@ impl PageEndpoint {
} }
server_assets.extend(files_value.iter().copied()); server_assets.extend(files_value.iter().copied());
// the next-edge-ssr-loader templates expect the manifests to be stored in
// global variables defined in these files
//
// they are created in `setup-dev-bundler.ts`
let mut file_paths_from_root = vec![
"server/server-reference-manifest.js".to_string(),
"server/middleware-build-manifest.js".to_string(),
"server/middleware-react-loadable-manifest.js".to_string(),
"server/next-font-manifest.js".to_string(),
];
let node_root_value = node_root.await?; let node_root_value = node_root.await?;
let files_paths_from_root: Vec<String> = files_value let middleware_paths_from_root: Vec<String> = files_value
.iter() .iter()
.map(move |&file| { .map(move |&file| {
let node_root_value = node_root_value.clone(); let node_root_value = node_root_value.clone();
...@@ -940,6 +953,8 @@ impl PageEndpoint { ...@@ -940,6 +953,8 @@ impl PageEndpoint {
.try_flat_join() .try_flat_join()
.await?; .await?;
file_paths_from_root.extend(middleware_paths_from_root);
let pathname = this.pathname.await?; let pathname = this.pathname.await?;
let named_regex = get_named_middleware_regex(&pathname); let named_regex = get_named_middleware_regex(&pathname);
let matchers = MiddlewareMatcher { let matchers = MiddlewareMatcher {
...@@ -949,7 +964,7 @@ impl PageEndpoint { ...@@ -949,7 +964,7 @@ impl PageEndpoint {
}; };
let original_name = this.original_name.await?; let original_name = this.original_name.await?;
let edge_function_definition = EdgeFunctionDefinition { let edge_function_definition = EdgeFunctionDefinition {
files: files_paths_from_root, files: file_paths_from_root,
name: pathname.to_string(), name: pathname.to_string(),
page: original_name.to_string(), page: original_name.to_string(),
regions: None, regions: None,
......
...@@ -20,6 +20,7 @@ use next_core::{ ...@@ -20,6 +20,7 @@ use next_core::{
get_server_module_options_context, get_server_resolve_options_context, get_server_module_options_context, get_server_resolve_options_context,
get_server_runtime_entries, ServerContextType, get_server_runtime_entries, ServerContextType,
}, },
util::NextRuntime,
}; };
use turbo_tasks::{TryJoinIterExt, Value, Vc}; use turbo_tasks::{TryJoinIterExt, Value, Vc};
use turbopack_binding::{ use turbopack_binding::{
...@@ -137,6 +138,8 @@ pub async fn get_app_entries( ...@@ -137,6 +138,8 @@ pub async fn get_app_entries(
Vc::cell("ssr".to_string()), Vc::cell("ssr".to_string()),
); );
transitions.insert("next-ssr".to_string(), Vc::upcast(ssr_transition));
const ECMASCRIPT_CLIENT_TRANSITION_NAME: &str = "next-ecmascript-client-reference"; const ECMASCRIPT_CLIENT_TRANSITION_NAME: &str = "next-ecmascript-client-reference";
transitions.insert( transitions.insert(
...@@ -191,15 +194,16 @@ pub async fn get_app_entries( ...@@ -191,15 +194,16 @@ pub async fn get_app_entries(
Ok(match entrypoint { Ok(match entrypoint {
Entrypoint::AppPage { page, loader_tree } => get_app_page_entry( Entrypoint::AppPage { page, loader_tree } => get_app_page_entry(
rsc_context, rsc_context,
// TODO add edge support // TODO(WEB-1824): add edge support
rsc_context, rsc_context,
*loader_tree, *loader_tree,
page.clone(), page.clone(),
project_root, project_root,
next_config,
), ),
Entrypoint::AppRoute { page, path } => get_app_route_entry( Entrypoint::AppRoute { page, path } => get_app_route_entry(
rsc_context, rsc_context,
// TODO add edge support // TODO(WEB-1824): add edge support
rsc_context, rsc_context,
Vc::upcast(FileSource::new(*path)), Vc::upcast(FileSource::new(*path)),
page.clone(), page.clone(),
...@@ -207,7 +211,7 @@ pub async fn get_app_entries( ...@@ -207,7 +211,7 @@ pub async fn get_app_entries(
), ),
Entrypoint::AppMetadata { page, metadata } => get_app_metadata_route_entry( Entrypoint::AppMetadata { page, metadata } => get_app_metadata_route_entry(
rsc_context, rsc_context,
// TODO add edge support // TODO(WEB-1824): add edge support
rsc_context, rsc_context,
project_root, project_root,
page.clone(), page.clone(),
...@@ -260,6 +264,7 @@ pub async fn compute_app_entries_chunks( ...@@ -260,6 +264,7 @@ pub async fn compute_app_entries_chunks(
build_manifest: &mut BuildManifest, build_manifest: &mut BuildManifest,
app_paths_manifest: &mut AppPathsManifest, app_paths_manifest: &mut AppPathsManifest,
all_chunks: &mut Vec<Vc<Box<dyn OutputAsset>>>, all_chunks: &mut Vec<Vc<Box<dyn OutputAsset>>>,
runtime: NextRuntime,
) -> Result<()> { ) -> Result<()> {
let client_relative_path_ref = client_relative_path.await?; let client_relative_path_ref = client_relative_path.await?;
...@@ -356,6 +361,7 @@ pub async fn compute_app_entries_chunks( ...@@ -356,6 +361,7 @@ pub async fn compute_app_entries_chunks(
client_chunking_context, client_chunking_context,
ssr_chunking_context, ssr_chunking_context,
next_config.computed_asset_prefix(), next_config.computed_asset_prefix(),
runtime,
); );
all_chunks.push(entry_manifest); all_chunks.push(entry_manifest);
......
...@@ -20,6 +20,7 @@ use next_core::{ ...@@ -20,6 +20,7 @@ use next_core::{
}, },
next_server::{get_server_chunking_context, get_server_compile_time_info}, next_server::{get_server_chunking_context, get_server_compile_time_info},
url_node::get_sorted_routes, url_node::get_sorted_routes,
util::NextRuntime,
{self}, {self},
}; };
use serde::Serialize; use serde::Serialize;
...@@ -310,7 +311,8 @@ pub(crate) async fn next_build(options: TransientInstance<BuildOptions>) -> Resu ...@@ -310,7 +311,8 @@ pub(crate) async fn next_build(options: TransientInstance<BuildOptions>) -> Resu
let app_client_references_chunks = get_app_client_references_chunks( let app_client_references_chunks = get_app_client_references_chunks(
app_client_reference_tys, app_client_reference_tys,
client_chunking_context, client_chunking_context,
server_chunking_context, // TODO(WEB-1824): add edge support
Vc::upcast(server_chunking_context),
); );
let app_client_references_chunks_ref = app_client_references_chunks.await?; let app_client_references_chunks_ref = app_client_references_chunks.await?;
...@@ -339,6 +341,8 @@ pub(crate) async fn next_build(options: TransientInstance<BuildOptions>) -> Resu ...@@ -339,6 +341,8 @@ pub(crate) async fn next_build(options: TransientInstance<BuildOptions>) -> Resu
&mut build_manifest, &mut build_manifest,
&mut app_paths_manifest, &mut app_paths_manifest,
&mut all_chunks, &mut all_chunks,
// TODO(WEB-1824): add edge support
NextRuntime::NodeJs,
) )
.await?; .await?;
......
...@@ -161,6 +161,7 @@ pub async fn get_page_entries( ...@@ -161,6 +161,7 @@ pub async fn get_page_entries(
pages_structure, pages_structure,
project_root, project_root,
next_router_root, next_router_root,
next_config,
) )
.await?; .await?;
...@@ -178,6 +179,7 @@ async fn get_page_entries_for_root_directory( ...@@ -178,6 +179,7 @@ async fn get_page_entries_for_root_directory(
pages_structure: Vc<PagesStructure>, pages_structure: Vc<PagesStructure>,
project_root: Vc<FileSystemPath>, project_root: Vc<FileSystemPath>,
next_router_root: Vc<FileSystemPath>, next_router_root: Vc<FileSystemPath>,
next_config: Vc<NextConfig>,
) -> Result<Vec<Vc<PageEntry>>> { ) -> Result<Vec<Vc<PageEntry>>> {
let PagesStructure { let PagesStructure {
app, app,
...@@ -200,6 +202,7 @@ async fn get_page_entries_for_root_directory( ...@@ -200,6 +202,7 @@ async fn get_page_entries_for_root_directory(
app.next_router_path, app.next_router_path,
app.original_path, app.original_path,
PathType::PagesPage, PathType::PagesPage,
next_config,
)); ));
// This only makes sense on the server. // This only makes sense on the server.
...@@ -213,6 +216,7 @@ async fn get_page_entries_for_root_directory( ...@@ -213,6 +216,7 @@ async fn get_page_entries_for_root_directory(
document.next_router_path, document.next_router_path,
document.original_path, document.original_path,
PathType::PagesPage, PathType::PagesPage,
next_config,
)); ));
// This only makes sense on both the client and the server, but they should map // This only makes sense on both the client and the server, but they should map
...@@ -227,6 +231,7 @@ async fn get_page_entries_for_root_directory( ...@@ -227,6 +231,7 @@ async fn get_page_entries_for_root_directory(
error.next_router_path, error.next_router_path,
error.original_path, error.original_path,
PathType::PagesPage, PathType::PagesPage,
next_config,
)); ));
if let Some(api) = api { if let Some(api) = api {
...@@ -238,6 +243,7 @@ async fn get_page_entries_for_root_directory( ...@@ -238,6 +243,7 @@ async fn get_page_entries_for_root_directory(
next_router_root, next_router_root,
&mut entries, &mut entries,
PathType::PagesApi, PathType::PagesApi,
next_config,
) )
.await?; .await?;
} }
...@@ -251,6 +257,7 @@ async fn get_page_entries_for_root_directory( ...@@ -251,6 +257,7 @@ async fn get_page_entries_for_root_directory(
next_router_root, next_router_root,
&mut entries, &mut entries,
PathType::PagesPage, PathType::PagesPage,
next_config,
) )
.await?; .await?;
} }
...@@ -267,6 +274,7 @@ async fn get_page_entries_for_directory( ...@@ -267,6 +274,7 @@ async fn get_page_entries_for_directory(
next_router_root: Vc<FileSystemPath>, next_router_root: Vc<FileSystemPath>,
entries: &mut Vec<Vc<PageEntry>>, entries: &mut Vec<Vc<PageEntry>>,
path_type: PathType, path_type: PathType,
next_config: Vc<NextConfig>,
) -> Result<()> { ) -> Result<()> {
let PagesDirectoryStructure { let PagesDirectoryStructure {
ref items, ref items,
...@@ -289,6 +297,7 @@ async fn get_page_entries_for_directory( ...@@ -289,6 +297,7 @@ async fn get_page_entries_for_directory(
next_router_path, next_router_path,
original_path, original_path,
path_type, path_type,
next_config,
)); ));
} }
...@@ -301,6 +310,7 @@ async fn get_page_entries_for_directory( ...@@ -301,6 +310,7 @@ async fn get_page_entries_for_directory(
next_router_root, next_router_root,
entries, entries,
path_type, path_type,
next_config,
) )
.await?; .await?;
} }
...@@ -329,6 +339,7 @@ async fn get_page_entry_for_file( ...@@ -329,6 +339,7 @@ async fn get_page_entry_for_file(
next_router_path: Vc<FileSystemPath>, next_router_path: Vc<FileSystemPath>,
next_original_path: Vc<FileSystemPath>, next_original_path: Vc<FileSystemPath>,
path_type: PathType, path_type: PathType,
next_config: Vc<NextConfig>,
) -> Result<Vc<PageEntry>> { ) -> Result<Vc<PageEntry>> {
let reference_type = Value::new(ReferenceType::Entry(match path_type { let reference_type = Value::new(ReferenceType::Entry(match path_type {
PathType::PagesPage => EntryReferenceSubType::Page, PathType::PagesPage => EntryReferenceSubType::Page,
...@@ -347,6 +358,7 @@ async fn get_page_entry_for_file( ...@@ -347,6 +358,7 @@ async fn get_page_entry_for_file(
source, source,
Vc::cell(original_name), Vc::cell(original_name),
NextRuntime::NodeJs, NextRuntime::NodeJs,
next_config,
); );
let client_module = create_page_loader_entry_module(client_module_context, source, pathname); let client_module = create_page_loader_entry_module(client_module_context, source, pathname);
......
...@@ -11,18 +11,15 @@ use turbo_tasks::{trace::TraceRawVcs, TryJoinIterExt, ValueDefault, Vc}; ...@@ -11,18 +11,15 @@ use turbo_tasks::{trace::TraceRawVcs, TryJoinIterExt, ValueDefault, Vc};
use turbo_tasks_fs::FileSystemPath; use turbo_tasks_fs::FileSystemPath;
use turbopack_binding::turbopack::{ use turbopack_binding::turbopack::{
core::{ core::{
context::AssetContext,
file_source::FileSource, file_source::FileSource,
ident::AssetIdent, ident::AssetIdent,
issue::{Issue, IssueExt, IssueSeverity, IssueSource, OptionIssueSource}, issue::{Issue, IssueExt, IssueSeverity, IssueSource, OptionIssueSource},
module::Module,
reference_type::{EcmaScriptModulesReferenceSubType, ReferenceType},
source::Source, source::Source,
}, },
ecmascript::{ ecmascript::{
analyzer::{graph::EvalContext, ConstantNumber, ConstantValue, JsValue}, analyzer::{graph::EvalContext, ConstantNumber, ConstantValue, JsValue},
parse::ParseResult, parse::{parse, ParseResult},
EcmascriptModuleAsset, EcmascriptInputTransforms, EcmascriptModuleAssetType,
}, },
}; };
...@@ -210,20 +207,28 @@ impl Issue for NextSegmentConfigParsingIssue { ...@@ -210,20 +207,28 @@ impl Issue for NextSegmentConfigParsingIssue {
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn parse_segment_config_from_source( pub async fn parse_segment_config_from_source(
module: Vc<Box<dyn Module>>,
source: Vc<Box<dyn Source>>, source: Vc<Box<dyn Source>>,
) -> Result<Vc<NextSegmentConfig>> { ) -> Result<Vc<NextSegmentConfig>> {
let Some(ecmascript_asset) = let path = source.ident().path().await?;
Vc::try_resolve_downcast_type::<EcmascriptModuleAsset>(module).await?
else { let result = &*parse(
return Ok(Default::default()); source,
}; turbo_tasks::Value::new(
if path.path.ends_with(".ts") || path.path.ends_with(".tsx") {
EcmascriptModuleAssetType::Typescript
} else {
EcmascriptModuleAssetType::Ecmascript
},
),
EcmascriptInputTransforms::empty(),
)
.await?;
let ParseResult::Ok { let ParseResult::Ok {
program: Program::Module(module_ast), program: Program::Module(module_ast),
eval_context, eval_context,
.. ..
} = &*ecmascript_asset.parse().await? } = result
else { else {
return Ok(Default::default()); return Ok(Default::default());
}; };
...@@ -245,7 +250,7 @@ pub async fn parse_segment_config_from_source( ...@@ -245,7 +250,7 @@ pub async fn parse_segment_config_from_source(
}; };
if let Some(init) = decl.init.as_ref() { if let Some(init) = decl.init.as_ref() {
parse_config_value(module, source, &mut config, ident, init, eval_context); parse_config_value(source, &mut config, ident, init, eval_context);
} }
} }
} }
...@@ -258,7 +263,6 @@ fn issue_source(source: Vc<Box<dyn Source>>, span: Span) -> Vc<IssueSource> { ...@@ -258,7 +263,6 @@ fn issue_source(source: Vc<Box<dyn Source>>, span: Span) -> Vc<IssueSource> {
} }
fn parse_config_value( fn parse_config_value(
module: Vc<Box<dyn Module>>,
source: Vc<Box<dyn Source>>, source: Vc<Box<dyn Source>>,
config: &mut NextSegmentConfig, config: &mut NextSegmentConfig,
ident: &Ident, ident: &Ident,
...@@ -269,7 +273,7 @@ fn parse_config_value( ...@@ -269,7 +273,7 @@ fn parse_config_value(
let invalid_config = |detail: &str, value: &JsValue| { let invalid_config = |detail: &str, value: &JsValue| {
let (explainer, hints) = value.explain(2, 0); let (explainer, hints) = value.explain(2, 0);
NextSegmentConfigParsingIssue { NextSegmentConfigParsingIssue {
ident: module.ident(), ident: source.ident(),
detail: Vc::cell(format!("{detail} Got {explainer}.{hints}")), detail: Vc::cell(format!("{detail} Got {explainer}.{hints}")),
source: issue_source(source, span), source: issue_source(source, span),
} }
...@@ -397,7 +401,6 @@ fn parse_config_value( ...@@ -397,7 +401,6 @@ fn parse_config_value(
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn parse_segment_config_from_loader_tree( pub async fn parse_segment_config_from_loader_tree(
loader_tree: Vc<LoaderTree>, loader_tree: Vc<LoaderTree>,
context: Vc<Box<dyn AssetContext>>,
) -> Result<Vc<NextSegmentConfig>> { ) -> Result<Vc<NextSegmentConfig>> {
let loader_tree = loader_tree.await?; let loader_tree = loader_tree.await?;
let components = loader_tree.components.await?; let components = loader_tree.components.await?;
...@@ -406,7 +409,7 @@ pub async fn parse_segment_config_from_loader_tree( ...@@ -406,7 +409,7 @@ pub async fn parse_segment_config_from_loader_tree(
.parallel_routes .parallel_routes
.values() .values()
.copied() .copied()
.map(|tree| parse_segment_config_from_loader_tree(tree, context)) .map(parse_segment_config_from_loader_tree)
.try_join() .try_join()
.await?; .await?;
for tree in parallel_configs { for tree in parallel_configs {
...@@ -417,18 +420,8 @@ pub async fn parse_segment_config_from_loader_tree( ...@@ -417,18 +420,8 @@ pub async fn parse_segment_config_from_loader_tree(
.flatten() .flatten()
{ {
let source = Vc::upcast(FileSource::new(component)); let source = Vc::upcast(FileSource::new(component));
config.apply_parent_config( config.apply_parent_config(&*parse_segment_config_from_source(source).await?);
&*parse_segment_config_from_source(
context.process(
source,
turbo_tasks::Value::new(ReferenceType::EcmaScriptModules(
EcmaScriptModulesReferenceSubType::Undefined,
)),
),
source,
)
.await?,
);
} }
Ok(config.cell()) Ok(config.cell())
} }
...@@ -3,7 +3,6 @@ use indexmap::IndexMap; ...@@ -3,7 +3,6 @@ use indexmap::IndexMap;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use turbo_tasks::{debug::ValueDebugFormat, trace::TraceRawVcs, TryJoinIterExt, Vc}; use turbo_tasks::{debug::ValueDebugFormat, trace::TraceRawVcs, TryJoinIterExt, Vc};
use turbopack_binding::turbopack::{ use turbopack_binding::turbopack::{
build::BuildChunkingContext,
core::{chunk::ChunkingContextExt, output::OutputAssets}, core::{chunk::ChunkingContextExt, output::OutputAssets},
ecmascript::chunk::EcmascriptChunkingContext, ecmascript::chunk::EcmascriptChunkingContext,
}; };
...@@ -32,7 +31,7 @@ pub struct ClientReferencesChunks(IndexMap<ClientReferenceType, ClientReferenceC ...@@ -32,7 +31,7 @@ pub struct ClientReferencesChunks(IndexMap<ClientReferenceType, ClientReferenceC
pub async fn get_app_client_references_chunks( pub async fn get_app_client_references_chunks(
app_client_reference_types: Vc<ClientReferenceTypes>, app_client_reference_types: Vc<ClientReferenceTypes>,
client_chunking_context: Vc<Box<dyn EcmascriptChunkingContext>>, client_chunking_context: Vc<Box<dyn EcmascriptChunkingContext>>,
ssr_chunking_context: Vc<BuildChunkingContext>, ssr_chunking_context: Vc<Box<dyn EcmascriptChunkingContext>>,
) -> Result<Vc<ClientReferencesChunks>> { ) -> Result<Vc<ClientReferencesChunks>> {
let app_client_references_chunks: IndexMap<_, _> = app_client_reference_types let app_client_references_chunks: IndexMap<_, _> = app_client_reference_types
.await? .await?
......
...@@ -25,6 +25,8 @@ use crate::{ ...@@ -25,6 +25,8 @@ use crate::{
loader_tree::LoaderTreeModule, loader_tree::LoaderTreeModule,
mode::NextMode, mode::NextMode,
next_app::{AppPage, AppPath}, next_app::{AppPage, AppPath},
next_config::NextConfig,
next_edge::entry::wrap_edge_entry,
next_server_component::NextServerComponentTransition, next_server_component::NextServerComponentTransition,
parse_segment_config_from_loader_tree, parse_segment_config_from_loader_tree,
util::{file_content_rope, load_next_js_template, NextRuntime}, util::{file_content_rope, load_next_js_template, NextRuntime},
...@@ -38,8 +40,9 @@ pub async fn get_app_page_entry( ...@@ -38,8 +40,9 @@ pub async fn get_app_page_entry(
loader_tree: Vc<LoaderTree>, loader_tree: Vc<LoaderTree>,
page: AppPage, page: AppPage,
project_root: Vc<FileSystemPath>, project_root: Vc<FileSystemPath>,
next_config: Vc<NextConfig>,
) -> Result<Vc<AppEntry>> { ) -> Result<Vc<AppEntry>> {
let config = parse_segment_config_from_loader_tree(loader_tree, Vc::upcast(nodejs_context)); let config = parse_segment_config_from_loader_tree(loader_tree);
let is_edge = matches!(config.await?.runtime, Some(NextRuntime::Edge)); let is_edge = matches!(config.await?.runtime, Some(NextRuntime::Edge));
let context = if is_edge { let context = if is_edge {
edge_context edge_context
...@@ -109,7 +112,13 @@ pub async fn get_app_page_entry( ...@@ -109,7 +112,13 @@ pub async fn get_app_page_entry(
); );
if is_edge { if is_edge {
rsc_entry = wrap_edge_entry(context, project_root, rsc_entry).await?; rsc_entry = wrap_edge_page(
Vc::upcast(context),
project_root,
rsc_entry,
page,
next_config,
);
}; };
let Some(rsc_entry) = let Some(rsc_entry) =
...@@ -127,21 +136,52 @@ pub async fn get_app_page_entry( ...@@ -127,21 +136,52 @@ pub async fn get_app_page_entry(
.cell()) .cell())
} }
async fn wrap_edge_entry( #[turbo_tasks::function]
context: Vc<ModuleAssetContext>, async fn wrap_edge_page(
context: Vc<Box<dyn AssetContext>>,
project_root: Vc<FileSystemPath>, project_root: Vc<FileSystemPath>,
entry: Vc<Box<dyn Module>>, entry: Vc<Box<dyn Module>>,
page: AppPage,
next_config: Vc<NextConfig>,
) -> Result<Vc<Box<dyn Module>>> { ) -> Result<Vc<Box<dyn Module>>> {
const INNER: &str = "INNER_RSC_ENTRY"; const INNER: &str = "INNER_PAGE_ENTRY";
let next_config = &*next_config.await?;
// TODO(WEB-1824): add build support
let build_id = "development";
let dev = true;
// TODO(timneutkens): remove this
let is_server_component = true;
let server_actions_body_size_limit = &next_config.experimental.server_actions_body_size_limit;
let sri_enabled = !dev
&& next_config
.experimental
.sri
.as_ref()
.map(|sri| sri.algorithm.as_ref())
.is_some();
let source = load_next_js_template( let source = load_next_js_template(
"edge-app-route.js", "edge-ssr-app.js",
project_root, project_root,
indexmap! { indexmap! {
"VAR_USERLAND" => INNER.to_string(), "VAR_USERLAND" => INNER.to_string(),
"VAR_PAGE" => page.to_string(),
"VAR_BUILD_ID" => build_id.to_string(),
},
indexmap! {
"sriEnabled" => serde_json::Value::Bool(sri_enabled).to_string(),
"nextConfig" => serde_json::to_string(next_config)?,
"isServerComponent" => serde_json::Value::Bool(is_server_component).to_string(),
"dev" => serde_json::Value::Bool(dev).to_string(),
"serverActionsBodySizeLimit" => serde_json::to_string(server_actions_body_size_limit)?,
},
indexmap! {
"incrementalCacheHandler" => None,
}, },
indexmap! {},
indexmap! {},
) )
.await?; .await?;
...@@ -149,8 +189,15 @@ async fn wrap_edge_entry( ...@@ -149,8 +189,15 @@ async fn wrap_edge_entry(
INNER.to_string() => entry INNER.to_string() => entry
}; };
Ok(context.process( let wrapped = context.process(
Vc::upcast(source), Vc::upcast(source),
Value::new(ReferenceType::Internal(Vc::cell(inner_assets))), Value::new(ReferenceType::Internal(Vc::cell(inner_assets))),
);
Ok(wrap_edge_entry(
context,
project_root,
wrapped,
AppPath::from(page).to_string(),
)) ))
} }
use std::io::Write;
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use indexmap::indexmap; use indexmap::indexmap;
use indoc::writedoc;
use turbo_tasks::{Value, ValueToString, Vc}; use turbo_tasks::{Value, ValueToString, Vc};
use turbopack_binding::{ use turbopack_binding::{
turbo::tasks_fs::{rope::RopeBuilder, File, FileSystemPath}, turbo::tasks_fs::FileSystemPath,
turbopack::{ turbopack::{
core::{ core::{
asset::AssetContent,
context::AssetContext, context::AssetContext,
module::Module, module::Module,
reference_type::{EntryReferenceSubType, ReferenceType}, reference_type::{EntryReferenceSubType, ReferenceType},
source::Source, source::Source,
virtual_source::VirtualSource,
}, },
ecmascript::{chunk::EcmascriptChunkPlaceable, utils::StringifyJs}, ecmascript::chunk::EcmascriptChunkPlaceable,
turbopack::ModuleAssetContext, turbopack::ModuleAssetContext,
}, },
}; };
use crate::{ use crate::{
next_app::{AppEntry, AppPage, AppPath}, next_app::{AppEntry, AppPage, AppPath},
next_edge::entry::wrap_edge_entry,
parse_segment_config_from_source, parse_segment_config_from_source,
util::{load_next_js_template, NextRuntime}, util::{load_next_js_template, NextRuntime},
}; };
...@@ -35,13 +31,7 @@ pub async fn get_app_route_entry( ...@@ -35,13 +31,7 @@ pub async fn get_app_route_entry(
page: AppPage, page: AppPage,
project_root: Vc<FileSystemPath>, project_root: Vc<FileSystemPath>,
) -> Result<Vc<AppEntry>> { ) -> Result<Vc<AppEntry>> {
let config = parse_segment_config_from_source( let config = parse_segment_config_from_source(source);
nodejs_context.process(
source,
Value::new(ReferenceType::Entry(EntryReferenceSubType::AppRoute)),
),
source,
);
let is_edge = matches!(config.await?.runtime, Some(NextRuntime::Edge)); let is_edge = matches!(config.await?.runtime, Some(NextRuntime::Edge));
let context = if is_edge { let context = if is_edge {
edge_context edge_context
...@@ -92,7 +82,12 @@ pub async fn get_app_route_entry( ...@@ -92,7 +82,12 @@ pub async fn get_app_route_entry(
); );
if is_edge { if is_edge {
rsc_entry = wrap_edge_entry(context, project_root, rsc_entry, pathname.clone()); rsc_entry = wrap_edge_route(
Vc::upcast(context),
project_root,
rsc_entry,
pathname.clone(),
);
} }
let Some(rsc_entry) = let Some(rsc_entry) =
...@@ -111,39 +106,33 @@ pub async fn get_app_route_entry( ...@@ -111,39 +106,33 @@ pub async fn get_app_route_entry(
} }
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn wrap_edge_entry( async fn wrap_edge_route(
context: Vc<ModuleAssetContext>, context: Vc<Box<dyn AssetContext>>,
project_root: Vc<FileSystemPath>, project_root: Vc<FileSystemPath>,
entry: Vc<Box<dyn Module>>, entry: Vc<Box<dyn Module>>,
pathname: String, pathname: String,
) -> Result<Vc<Box<dyn Module>>> { ) -> Result<Vc<Box<dyn Module>>> {
let mut source = RopeBuilder::default(); const INNER: &str = "INNER_ROUTE_ENTRY";
writedoc!(
source, let source = load_next_js_template(
r#" "edge-app-route.js",
import {{ EdgeRouteModuleWrapper }} from 'next/dist/esm/server/web/edge-route-module-wrapper' project_root,
import * as module from "MODULE" indexmap! {
"VAR_USERLAND" => INNER.to_string(),
self._ENTRIES ||= {{}} },
self._ENTRIES[{}] = {{ indexmap! {},
ComponentMod: module, indexmap! {},
default: EdgeRouteModuleWrapper.wrap(module.routeModule), )
}} .await?;
"#,
StringifyJs(&format_args!("middleware_{}", pathname))
)?;
let file = File::from(source.build());
// TODO(alexkirsz) Figure out how to name this virtual asset.
let virtual_source = VirtualSource::new(
project_root.join("edge-wrapper.js".to_string()),
AssetContent::file(file.into()),
);
let inner_assets = indexmap! { let inner_assets = indexmap! {
"MODULE".to_string() => entry INNER.to_string() => entry
}; };
Ok(context.process( let wrapped = context.process(
Vc::upcast(virtual_source), Vc::upcast(source),
Value::new(ReferenceType::Internal(Vc::cell(inner_assets))), Value::new(ReferenceType::Internal(Vc::cell(inner_assets))),
)) );
Ok(wrap_edge_entry(context, project_root, wrapped, pathname))
} }
...@@ -445,6 +445,9 @@ pub struct ExperimentalConfig { ...@@ -445,6 +445,9 @@ pub struct ExperimentalConfig {
/// Enables server actions. Using this feature will enable the /// Enables server actions. Using this feature will enable the
/// `react@experimental` for the `app` directory. @see https://nextjs.org/docs/app/api-reference/functions/server-actions /// `react@experimental` for the `app` directory. @see https://nextjs.org/docs/app/api-reference/functions/server-actions
server_actions: Option<bool>, server_actions: Option<bool>,
/// Allows adjusting body parser size limit for server actions.
pub server_actions_body_size_limit: Option<SizeLimit>,
pub sri: Option<SubResourceIntegrity>,
// --- // ---
// UNSUPPORTED // UNSUPPORTED
...@@ -487,13 +490,10 @@ pub struct ExperimentalConfig { ...@@ -487,13 +490,10 @@ pub struct ExperimentalConfig {
ppr: Option<bool>, ppr: Option<bool>,
taint: Option<bool>, taint: Option<bool>,
proxy_timeout: Option<f64>, proxy_timeout: Option<f64>,
/// Allows adjusting body parser size limit for server actions.
server_actions_body_size_limit: Option<SizeLimit>,
/// enables the minification of server code. /// enables the minification of server code.
server_minification: Option<bool>, server_minification: Option<bool>,
/// Enables source maps generation for the server production bundle. /// Enables source maps generation for the server production bundle.
server_source_maps: Option<bool>, server_source_maps: Option<bool>,
sri: Option<serde_json::Value>,
swc_minify: Option<bool>, swc_minify: Option<bool>,
swc_trace_profiling: Option<bool>, swc_trace_profiling: Option<bool>,
/// @internal Used by the Next.js internals only. /// @internal Used by the Next.js internals only.
...@@ -509,9 +509,15 @@ pub struct ExperimentalConfig { ...@@ -509,9 +509,15 @@ pub struct ExperimentalConfig {
worker_threads: Option<bool>, worker_threads: Option<bool>,
} }
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, TraceRawVcs)]
#[serde(rename_all = "camelCase")]
pub struct SubResourceIntegrity {
pub algorithm: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, TraceRawVcs)] #[derive(Clone, Debug, PartialEq, Serialize, Deserialize, TraceRawVcs)]
#[serde(untagged)] #[serde(untagged)]
enum SizeLimit { pub enum SizeLimit {
Number(f64), Number(f64),
WithUnit(String), WithUnit(String),
} }
......
...@@ -16,6 +16,7 @@ use super::{ClientReferenceManifest, ManifestNode, ManifestNodeEntry, ModuleId}; ...@@ -16,6 +16,7 @@ use super::{ClientReferenceManifest, ManifestNode, ManifestNodeEntry, ModuleId};
use crate::{ use crate::{
next_app::ClientReferencesChunks, next_app::ClientReferencesChunks,
next_client_reference::{ClientReferenceType, ClientReferences}, next_client_reference::{ClientReferenceType, ClientReferences},
util::NextRuntime,
}; };
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
...@@ -30,6 +31,7 @@ impl ClientReferenceManifest { ...@@ -30,6 +31,7 @@ impl ClientReferenceManifest {
client_chunking_context: Vc<Box<dyn EcmascriptChunkingContext>>, client_chunking_context: Vc<Box<dyn EcmascriptChunkingContext>>,
ssr_chunking_context: Vc<Box<dyn EcmascriptChunkingContext>>, ssr_chunking_context: Vc<Box<dyn EcmascriptChunkingContext>>,
asset_prefix: Vc<Option<String>>, asset_prefix: Vc<Option<String>>,
runtime: NextRuntime,
) -> Result<Vc<Box<dyn OutputAsset>>> { ) -> Result<Vc<Box<dyn OutputAsset>>> {
let mut entry_manifest: ClientReferenceManifest = Default::default(); let mut entry_manifest: ClientReferenceManifest = Default::default();
entry_manifest.module_loading.prefix = asset_prefix entry_manifest.module_loading.prefix = asset_prefix
...@@ -171,18 +173,34 @@ impl ClientReferenceManifest { ...@@ -171,18 +173,34 @@ impl ClientReferenceManifest {
ManifestNodeEntry { ManifestNodeEntry {
name: "*".to_string(), name: "*".to_string(),
id: (&*ssr_module_id).into(), id: (&*ssr_module_id).into(),
chunks: ssr_chunks_paths.clone(), chunks: if runtime == NextRuntime::Edge {
// the chunks get added to the middleware-manifest.json instead of
// this file because the edge runtime doesn't support dynamically
// loading chunks.
vec![]
} else {
ssr_chunks_paths.clone()
},
// TODO(WEB-434) // TODO(WEB-434)
r#async: false, r#async: false,
}, },
); );
match runtime {
NextRuntime::NodeJs => {
entry_manifest entry_manifest
.ssr_module_mapping .ssr_module_mapping
.insert((&*client_module_id).into(), ssr_manifest_node.clone());
}
NextRuntime::Edge => {
entry_manifest
.edge_ssr_module_mapping
.insert((&*client_module_id).into(), ssr_manifest_node); .insert((&*client_module_id).into(), ssr_manifest_node);
} }
} }
} }
}
}
let client_reference_manifest_json = serde_json::to_string(&entry_manifest).unwrap(); let client_reference_manifest_json = serde_json::to_string(&entry_manifest).unwrap();
......
...@@ -215,7 +215,7 @@ pub struct ModuleLoading { ...@@ -215,7 +215,7 @@ pub struct ModuleLoading {
pub cross_origin: Option<String>, pub cross_origin: Option<String>,
} }
#[derive(Serialize, Default, Debug)] #[derive(Serialize, Default, Debug, Clone)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct ManifestNode { pub struct ManifestNode {
/// Mapping of export name to manifest node entry. /// Mapping of export name to manifest node entry.
...@@ -223,7 +223,7 @@ pub struct ManifestNode { ...@@ -223,7 +223,7 @@ pub struct ManifestNode {
pub module_exports: HashMap<String, ManifestNodeEntry>, pub module_exports: HashMap<String, ManifestNodeEntry>,
} }
#[derive(Serialize, Debug)] #[derive(Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct ManifestNodeEntry { pub struct ManifestNodeEntry {
/// Turbopack module ID. /// Turbopack module ID.
...@@ -236,7 +236,7 @@ pub struct ManifestNodeEntry { ...@@ -236,7 +236,7 @@ pub struct ManifestNodeEntry {
pub r#async: bool, pub r#async: bool,
} }
#[derive(Serialize, Debug, Eq, PartialEq, Hash)] #[derive(Serialize, Debug, Eq, PartialEq, Hash, Clone)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
#[serde(untagged)] #[serde(untagged)]
pub enum ModuleId { pub enum ModuleId {
......
...@@ -2,6 +2,7 @@ use std::io::Write; ...@@ -2,6 +2,7 @@ use std::io::Write;
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use indexmap::indexmap; use indexmap::indexmap;
use serde::Serialize;
use turbo_tasks::Vc; use turbo_tasks::Vc;
use turbo_tasks_fs::FileSystemPath; use turbo_tasks_fs::FileSystemPath;
use turbopack_binding::{ use turbopack_binding::{
...@@ -13,15 +14,17 @@ use turbopack_binding::{ ...@@ -13,15 +14,17 @@ use turbopack_binding::{
core::{ core::{
asset::{Asset, AssetContent}, asset::{Asset, AssetContent},
context::AssetContext, context::AssetContext,
module::Module,
reference_type::{EntryReferenceSubType, ReferenceType}, reference_type::{EntryReferenceSubType, ReferenceType},
source::Source, source::Source,
virtual_source::VirtualSource, virtual_source::VirtualSource,
}, },
ecmascript::chunk::EcmascriptChunkPlaceable, ecmascript::{chunk::EcmascriptChunkPlaceable, utils::StringifyJs},
}, },
}; };
use crate::{ use crate::{
next_config::NextConfig,
next_edge::entry::wrap_edge_entry, next_edge::entry::wrap_edge_entry,
util::{file_content_rope, load_next_js_template, NextRuntime}, util::{file_content_rope, load_next_js_template, NextRuntime},
}; };
...@@ -35,6 +38,7 @@ pub async fn create_page_ssr_entry_module( ...@@ -35,6 +38,7 @@ pub async fn create_page_ssr_entry_module(
source: Vc<Box<dyn Source>>, source: Vc<Box<dyn Source>>,
next_original_name: Vc<String>, next_original_name: Vc<String>,
runtime: NextRuntime, runtime: NextRuntime,
next_config: Vc<NextConfig>,
) -> Result<Vc<Box<dyn EcmascriptChunkPlaceable>>> { ) -> Result<Vc<Box<dyn EcmascriptChunkPlaceable>>> {
let definition_page = &*next_original_name.await?; let definition_page = &*next_original_name.await?;
let definition_pathname = &*pathname.await?; let definition_pathname = &*pathname.await?;
...@@ -120,6 +124,16 @@ pub async fn create_page_ssr_entry_module( ...@@ -120,6 +124,16 @@ pub async fn create_page_ssr_entry_module(
); );
if matches!(runtime, NextRuntime::Edge) { if matches!(runtime, NextRuntime::Edge) {
if reference_type == ReferenceType::Entry(EntryReferenceSubType::Page) {
ssr_module = wrap_edge_page(
ssr_module_context,
project_root,
ssr_module,
definition_page.clone(),
definition_pathname.clone(),
next_config,
);
} else {
ssr_module = wrap_edge_entry( ssr_module = wrap_edge_entry(
ssr_module_context, ssr_module_context,
project_root, project_root,
...@@ -127,6 +141,7 @@ pub async fn create_page_ssr_entry_module( ...@@ -127,6 +141,7 @@ pub async fn create_page_ssr_entry_module(
definition_pathname.to_string(), definition_pathname.to_string(),
); );
} }
}
let Some(ssr_module) = let Some(ssr_module) =
Vc::try_resolve_downcast::<Box<dyn EcmascriptChunkPlaceable>>(ssr_module).await? Vc::try_resolve_downcast::<Box<dyn EcmascriptChunkPlaceable>>(ssr_module).await?
...@@ -136,3 +151,105 @@ pub async fn create_page_ssr_entry_module( ...@@ -136,3 +151,105 @@ pub async fn create_page_ssr_entry_module(
Ok(ssr_module) Ok(ssr_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,
next_config: Vc<NextConfig>,
) -> Result<Vc<Box<dyn Module>>> {
const INNER: &str = "INNER_PAGE_ENTRY";
let next_config = &*next_config.await?;
// TODO(WEB-1824): add build support
let build_id = "development";
let dev = true;
let sri_enabled = !dev
&& next_config
.experimental
.sri
.as_ref()
.map(|sri| sri.algorithm.as_ref())
.is_some();
let source = load_next_js_template(
"edge-ssr.js",
project_root,
indexmap! {
"VAR_USERLAND" => INNER.to_string(),
"VAR_PAGE" => page.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(),
},
indexmap! {
"pagesType" => StringifyJs("pages").to_string(),
"sriEnabled" => serde_json::Value::Bool(sri_enabled).to_string(),
"nextConfig" => serde_json::to_string(next_config)?,
"dev" => serde_json::Value::Bool(dev).to_string(),
"pageRouteModuleOptions" => serde_json::to_string(&get_route_module_options(page.clone(), pathname.clone()))?,
"errorRouteModuleOptions" => serde_json::to_string(&get_route_module_options("/_error".to_string(), "/_error".to_string()))?,
"user500RouteModuleOptions" => serde_json::to_string(&get_route_module_options("/500".to_string(), "/500".to_string()))?,
},
indexmap! {
// TODO
"incrementalCacheHandler" => None,
"userland500Page" => None,
},
)
.await?;
let inner_assets = indexmap! {
INNER.to_string() => entry
};
let wrapped = context.process(
Vc::upcast(source),
Value::new(ReferenceType::Internal(Vc::cell(inner_assets))),
);
Ok(wrap_edge_entry(
context,
project_root,
wrapped,
pathname.clone(),
))
}
#[derive(Serialize)]
struct PartialRouteModuleOptions {
definition: RouteDefinition,
}
#[derive(Serialize)]
struct RouteDefinition {
kind: String,
bundle_path: String,
filename: String,
/// Describes the pathname including all internal modifiers such as
/// intercepting routes, parallel routes and route/page suffixes that are
/// not part of the pathname.
page: String,
/// The pathname (including dynamic placeholders) for a route to resolve.
pathname: String,
}
fn get_route_module_options(page: String, pathname: String) -> PartialRouteModuleOptions {
PartialRouteModuleOptions {
definition: RouteDefinition {
kind: "PAGES".to_string(),
page,
pathname,
// The following aren't used in production.
bundle_path: "".to_string(),
filename: "".to_string(),
},
}
}
...@@ -591,6 +591,7 @@ export async function createEntrypoints( ...@@ -591,6 +591,7 @@ export async function createEntrypoints(
page, page,
}) })
// TODO(timneutkens): remove this
const isServerComponent = const isServerComponent =
isInsideAppDir && staticInfo.rsc !== RSC_MODULE_TYPES.client isInsideAppDir && staticInfo.rsc !== RSC_MODULE_TYPES.client
......
import type { LoaderTree } from '../../server/lib/app-dir-module' import type { LoaderTree } from '../../server/lib/app-dir-module'
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
;('TURBOPACK { transition: next-ssr }')
import { AppPageRouteModule } from '../../server/future/route-modules/app-page/module.compiled' import { AppPageRouteModule } from '../../server/future/route-modules/app-page/module.compiled'
import { RouteKind } from '../../server/future/route-kind' import { RouteKind } from '../../server/future/route-kind'
......
...@@ -80,6 +80,8 @@ import { ...@@ -80,6 +80,8 @@ import {
PHASE_DEVELOPMENT_SERVER, PHASE_DEVELOPMENT_SERVER,
SERVER_REFERENCE_MANIFEST, SERVER_REFERENCE_MANIFEST,
REACT_LOADABLE_MANIFEST, REACT_LOADABLE_MANIFEST,
MIDDLEWARE_REACT_LOADABLE_MANIFEST,
MIDDLEWARE_BUILD_MANIFEST,
} from '../../../shared/lib/constants' } from '../../../shared/lib/constants'
import { getMiddlewareRouteMatcher } from '../../../shared/lib/router/utils/middleware-route-matcher' import { getMiddlewareRouteMatcher } from '../../../shared/lib/router/utils/middleware-route-matcher'
...@@ -418,6 +420,7 @@ async function startWatcher(opts: SetupOpts) { ...@@ -418,6 +420,7 @@ async function startWatcher(opts: SetupOpts) {
const buildingIds = new Set() const buildingIds = new Set()
const readyIds = new Set() const readyIds = new Set()
function startBuilding(id: string, forceRebuild: boolean = false) { function startBuilding(id: string, forceRebuild: boolean = false) {
if (!forceRebuild && readyIds.has(id)) { if (!forceRebuild && readyIds.has(id)) {
return () => {} return () => {}
...@@ -795,11 +798,21 @@ async function startWatcher(opts: SetupOpts) { ...@@ -795,11 +798,21 @@ async function startWatcher(opts: SetupOpts) {
): Promise<void> { ): Promise<void> {
const buildManifest = mergeBuildManifests(buildManifests.values()) const buildManifest = mergeBuildManifests(buildManifests.values())
const buildManifestPath = path.join(distDir, BUILD_MANIFEST) const buildManifestPath = path.join(distDir, BUILD_MANIFEST)
const middlewareBuildManifestPath = path.join(
distDir,
'server',
`${MIDDLEWARE_BUILD_MANIFEST}.js`
)
deleteCache(buildManifestPath) deleteCache(buildManifestPath)
deleteCache(middlewareBuildManifestPath)
await writeFileAtomic( await writeFileAtomic(
buildManifestPath, buildManifestPath,
JSON.stringify(buildManifest, null, 2) JSON.stringify(buildManifest, null, 2)
) )
await writeFileAtomic(
middlewareBuildManifestPath,
`self.__BUILD_MANIFEST=${JSON.stringify(buildManifest)}`
)
const content: ClientBuildManifest = { const content: ClientBuildManifest = {
__rewrites: rewrites __rewrites: rewrites
...@@ -885,7 +898,8 @@ async function startWatcher(opts: SetupOpts) { ...@@ -885,7 +898,8 @@ async function startWatcher(opts: SetupOpts) {
) )
const middlewareManifestPath = path.join( const middlewareManifestPath = path.join(
distDir, distDir,
'server/middleware-manifest.json' 'server',
MIDDLEWARE_MANIFEST
) )
deleteCache(middlewareManifestPath) deleteCache(middlewareManifestPath)
await writeFileAtomic( await writeFileAtomic(
...@@ -920,34 +934,50 @@ async function startWatcher(opts: SetupOpts) { ...@@ -920,34 +934,50 @@ async function startWatcher(opts: SetupOpts) {
async function writeFontManifest(): Promise<void> { async function writeFontManifest(): Promise<void> {
// TODO: turbopack should write the correct // TODO: turbopack should write the correct
// version of this // version of this
const fontManifestPath = path.join( const fontManifest = {
distDir,
'server',
NEXT_FONT_MANIFEST + '.json'
)
deleteCache(fontManifestPath)
await writeFileAtomic(
fontManifestPath,
JSON.stringify(
{
pages: {}, pages: {},
app: {}, app: {},
appUsingSizeAdjust: false, appUsingSizeAdjust: false,
pagesUsingSizeAdjust: false, pagesUsingSizeAdjust: false,
}, }
null,
2 const json = JSON.stringify(fontManifest, null, 2)
const fontManifestJsonPath = path.join(
distDir,
'server',
`${NEXT_FONT_MANIFEST}.json`
) )
const fontManifestJsPath = path.join(
distDir,
'server',
`${NEXT_FONT_MANIFEST}.js`
)
deleteCache(fontManifestJsonPath)
deleteCache(fontManifestJsPath)
await writeFileAtomic(fontManifestJsonPath, json)
await writeFileAtomic(
fontManifestJsPath,
`self.__NEXT_FONT_MANIFEST=${JSON.stringify(json)}`
) )
} }
async function writeLoadableManifest(): Promise<void> { async function writeLoadableManifest(): Promise<void> {
const loadableManifest = mergeLoadableManifests(loadbleManifests.values()) const loadableManifest = mergeLoadableManifests(loadbleManifests.values())
const loadableManifestPath = path.join(distDir, REACT_LOADABLE_MANIFEST) const loadableManifestPath = path.join(distDir, REACT_LOADABLE_MANIFEST)
const middlewareloadableManifestPath = path.join(
distDir,
'server',
`${MIDDLEWARE_REACT_LOADABLE_MANIFEST}.js`
)
const json = JSON.stringify(loadableManifest, null, 2)
deleteCache(loadableManifestPath) deleteCache(loadableManifestPath)
deleteCache(middlewareloadableManifestPath)
await writeFileAtomic(loadableManifestPath, json)
await writeFileAtomic( await writeFileAtomic(
loadableManifestPath, middlewareloadableManifestPath,
JSON.stringify(loadableManifest, null, 2) `self.__REACT_LOADABLE_MANIFEST=${JSON.stringify(json)}`
) )
} }
...@@ -1471,6 +1501,14 @@ async function startWatcher(opts: SetupOpts) { ...@@ -1471,6 +1501,14 @@ async function startWatcher(opts: SetupOpts) {
} }
}) })
const type = writtenEndpoint?.type
if (type === 'edge') {
await loadMiddlewareManifest(page, 'app')
} else {
middlewareManifests.delete(page)
}
await loadAppBuildManifest(page) await loadAppBuildManifest(page)
await loadBuildManifest(page, 'app') await loadBuildManifest(page, 'app')
await loadAppPathManifest(page, 'app') await loadAppPathManifest(page, 'app')
......
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