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 {
ECMASCRIPT_CLIENT_TRANSITION_NAME.to_string(),
Vc::upcast(NextEcmascriptClientReferenceTransition::new(
self.client_transition(),
self.ssr_transition(),
self.edge_ssr_transition(),
)),
),
(
"next-dynamic".to_string(),
Vc::upcast(NextDynamicTransition::new(self.client_transition())),
),
(
"next-ssr".to_string(),
Vc::upcast(self.edge_ssr_transition()),
),
]
.into_iter()
.collect();
......@@ -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]
fn ssr_transition(self: Vc<Self>) -> Vc<ContextTransition> {
ContextTransition::new(
......@@ -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]
async fn runtime_entries(self: Vc<Self>) -> Result<Vc<RuntimeEntries>> {
let this = self.await?;
......@@ -460,6 +486,7 @@ impl AppEndpoint {
loader_tree,
self.page.clone(),
self.app_project.project().project_path(),
self.app_project.project().next_config(),
)
}
......@@ -519,6 +546,8 @@ impl AppEndpoint {
let mut server_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?;
......@@ -579,11 +608,20 @@ impl AppEndpoint {
.entry(Vc::upcast(app_entry.rsc_entry))
.await?;
let runtime = app_entry.config.await?.runtime.unwrap_or_default();
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(
client_reference_types,
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?;
......@@ -655,13 +693,31 @@ impl AppEndpoint {
client_references,
client_references_chunks,
this.app_project.project().client_chunking_context(),
Vc::upcast(this.app_project.project().server_chunking_context()),
ssr_chunking_context,
this.app_project
.project()
.next_config()
.computed_asset_prefix(),
runtime,
);
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(
......@@ -791,36 +847,54 @@ impl AppEndpoint {
);
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 files_paths_from_root = files
.await?
let middleware_paths_from_root = middleware_assets
.iter()
.map(move |&file| {
.map({
let node_root_value = node_root_value.clone();
async move {
Ok(node_root_value
.get_path_to(&*file.ident().path().await?)
.map(|path| path.to_string()))
move |&file| {
let node_root_value = node_root_value.clone();
async move {
Ok(node_root_value
.get_path_to(&*file.ident().path().await?)
.map(|path| path.to_string()))
}
}
})
.try_flat_join()
.await?;
let server_path_value = server_path.await?;
let files_paths_from_server = files
file_paths_from_root.extend(middleware_paths_from_root);
let rsc_paths_from_root = files
.await?
.iter()
.map(move |&file| {
let server_path_value = server_path_value.clone();
let node_root_value = node_root_value.clone();
async move {
Ok(server_path_value
Ok(node_root_value
.get_path_to(&*file.ident().path().await?)
.map(|path| path.to_string()))
}
})
.try_flat_join()
.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
// TODO(alexkirsz) This should be shared with next build.
......@@ -831,7 +905,7 @@ impl AppEndpoint {
..Default::default()
};
let edge_function_definition = EdgeFunctionDefinition {
files: files_paths_from_root,
files: file_paths_from_root,
name: app_entry.pathname.to_string(),
page: app_entry.original_name.clone(),
regions: app_entry
......@@ -870,7 +944,7 @@ impl AppEndpoint {
ty,
&app_entry.pathname,
&app_entry.original_name,
base_file,
entry_file,
)?;
server_assets.push(app_paths_manifest_output);
......
......@@ -612,6 +612,7 @@ impl PageEndpoint {
self.source(),
this.original_name,
config.runtime,
this.pages_project.project().next_config(),
);
let mut evaluatable_assets = edge_runtime_entries.await?.clone_value();
......@@ -645,6 +646,7 @@ impl PageEndpoint {
self.source(),
this.original_name,
config.runtime,
this.pages_project.project().next_config(),
);
let asset_path = get_asset_path_from_pathname(&this.pathname.await?, ".js");
......@@ -926,8 +928,19 @@ impl PageEndpoint {
}
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 files_paths_from_root: Vec<String> = files_value
let middleware_paths_from_root: Vec<String> = files_value
.iter()
.map(move |&file| {
let node_root_value = node_root_value.clone();
......@@ -940,6 +953,8 @@ impl PageEndpoint {
.try_flat_join()
.await?;
file_paths_from_root.extend(middleware_paths_from_root);
let pathname = this.pathname.await?;
let named_regex = get_named_middleware_regex(&pathname);
let matchers = MiddlewareMatcher {
......@@ -949,7 +964,7 @@ impl PageEndpoint {
};
let original_name = this.original_name.await?;
let edge_function_definition = EdgeFunctionDefinition {
files: files_paths_from_root,
files: file_paths_from_root,
name: pathname.to_string(),
page: original_name.to_string(),
regions: None,
......
......@@ -20,6 +20,7 @@ use next_core::{
get_server_module_options_context, get_server_resolve_options_context,
get_server_runtime_entries, ServerContextType,
},
util::NextRuntime,
};
use turbo_tasks::{TryJoinIterExt, Value, Vc};
use turbopack_binding::{
......@@ -137,6 +138,8 @@ pub async fn get_app_entries(
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";
transitions.insert(
......@@ -191,15 +194,16 @@ pub async fn get_app_entries(
Ok(match entrypoint {
Entrypoint::AppPage { page, loader_tree } => get_app_page_entry(
rsc_context,
// TODO add edge support
// TODO(WEB-1824): add edge support
rsc_context,
*loader_tree,
page.clone(),
project_root,
next_config,
),
Entrypoint::AppRoute { page, path } => get_app_route_entry(
rsc_context,
// TODO add edge support
// TODO(WEB-1824): add edge support
rsc_context,
Vc::upcast(FileSource::new(*path)),
page.clone(),
......@@ -207,7 +211,7 @@ pub async fn get_app_entries(
),
Entrypoint::AppMetadata { page, metadata } => get_app_metadata_route_entry(
rsc_context,
// TODO add edge support
// TODO(WEB-1824): add edge support
rsc_context,
project_root,
page.clone(),
......@@ -260,6 +264,7 @@ pub async fn compute_app_entries_chunks(
build_manifest: &mut BuildManifest,
app_paths_manifest: &mut AppPathsManifest,
all_chunks: &mut Vec<Vc<Box<dyn OutputAsset>>>,
runtime: NextRuntime,
) -> Result<()> {
let client_relative_path_ref = client_relative_path.await?;
......@@ -356,6 +361,7 @@ pub async fn compute_app_entries_chunks(
client_chunking_context,
ssr_chunking_context,
next_config.computed_asset_prefix(),
runtime,
);
all_chunks.push(entry_manifest);
......
......@@ -20,6 +20,7 @@ use next_core::{
},
next_server::{get_server_chunking_context, get_server_compile_time_info},
url_node::get_sorted_routes,
util::NextRuntime,
{self},
};
use serde::Serialize;
......@@ -310,7 +311,8 @@ pub(crate) async fn next_build(options: TransientInstance<BuildOptions>) -> Resu
let app_client_references_chunks = get_app_client_references_chunks(
app_client_reference_tys,
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?;
......@@ -339,6 +341,8 @@ pub(crate) async fn next_build(options: TransientInstance<BuildOptions>) -> Resu
&mut build_manifest,
&mut app_paths_manifest,
&mut all_chunks,
// TODO(WEB-1824): add edge support
NextRuntime::NodeJs,
)
.await?;
......
......@@ -161,6 +161,7 @@ pub async fn get_page_entries(
pages_structure,
project_root,
next_router_root,
next_config,
)
.await?;
......@@ -178,6 +179,7 @@ async fn get_page_entries_for_root_directory(
pages_structure: Vc<PagesStructure>,
project_root: Vc<FileSystemPath>,
next_router_root: Vc<FileSystemPath>,
next_config: Vc<NextConfig>,
) -> Result<Vec<Vc<PageEntry>>> {
let PagesStructure {
app,
......@@ -200,6 +202,7 @@ async fn get_page_entries_for_root_directory(
app.next_router_path,
app.original_path,
PathType::PagesPage,
next_config,
));
// This only makes sense on the server.
......@@ -213,6 +216,7 @@ async fn get_page_entries_for_root_directory(
document.next_router_path,
document.original_path,
PathType::PagesPage,
next_config,
));
// 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(
error.next_router_path,
error.original_path,
PathType::PagesPage,
next_config,
));
if let Some(api) = api {
......@@ -238,6 +243,7 @@ async fn get_page_entries_for_root_directory(
next_router_root,
&mut entries,
PathType::PagesApi,
next_config,
)
.await?;
}
......@@ -251,6 +257,7 @@ async fn get_page_entries_for_root_directory(
next_router_root,
&mut entries,
PathType::PagesPage,
next_config,
)
.await?;
}
......@@ -267,6 +274,7 @@ async fn get_page_entries_for_directory(
next_router_root: Vc<FileSystemPath>,
entries: &mut Vec<Vc<PageEntry>>,
path_type: PathType,
next_config: Vc<NextConfig>,
) -> Result<()> {
let PagesDirectoryStructure {
ref items,
......@@ -289,6 +297,7 @@ async fn get_page_entries_for_directory(
next_router_path,
original_path,
path_type,
next_config,
));
}
......@@ -301,6 +310,7 @@ async fn get_page_entries_for_directory(
next_router_root,
entries,
path_type,
next_config,
)
.await?;
}
......@@ -329,6 +339,7 @@ async fn get_page_entry_for_file(
next_router_path: Vc<FileSystemPath>,
next_original_path: Vc<FileSystemPath>,
path_type: PathType,
next_config: Vc<NextConfig>,
) -> Result<Vc<PageEntry>> {
let reference_type = Value::new(ReferenceType::Entry(match path_type {
PathType::PagesPage => EntryReferenceSubType::Page,
......@@ -347,6 +358,7 @@ async fn get_page_entry_for_file(
source,
Vc::cell(original_name),
NextRuntime::NodeJs,
next_config,
);
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};
use turbo_tasks_fs::FileSystemPath;
use turbopack_binding::turbopack::{
core::{
context::AssetContext,
file_source::FileSource,
ident::AssetIdent,
issue::{Issue, IssueExt, IssueSeverity, IssueSource, OptionIssueSource},
module::Module,
reference_type::{EcmaScriptModulesReferenceSubType, ReferenceType},
source::Source,
},
ecmascript::{
analyzer::{graph::EvalContext, ConstantNumber, ConstantValue, JsValue},
parse::ParseResult,
EcmascriptModuleAsset,
parse::{parse, ParseResult},
EcmascriptInputTransforms, EcmascriptModuleAssetType,
},
};
......@@ -210,20 +207,28 @@ impl Issue for NextSegmentConfigParsingIssue {
#[turbo_tasks::function]
pub async fn parse_segment_config_from_source(
module: Vc<Box<dyn Module>>,
source: Vc<Box<dyn Source>>,
) -> Result<Vc<NextSegmentConfig>> {
let Some(ecmascript_asset) =
Vc::try_resolve_downcast_type::<EcmascriptModuleAsset>(module).await?
else {
return Ok(Default::default());
};
let path = source.ident().path().await?;
let result = &*parse(
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 {
program: Program::Module(module_ast),
eval_context,
..
} = &*ecmascript_asset.parse().await?
} = result
else {
return Ok(Default::default());
};
......@@ -245,7 +250,7 @@ pub async fn parse_segment_config_from_source(
};
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> {
}
fn parse_config_value(
module: Vc<Box<dyn Module>>,
source: Vc<Box<dyn Source>>,
config: &mut NextSegmentConfig,
ident: &Ident,
......@@ -269,7 +273,7 @@ fn parse_config_value(
let invalid_config = |detail: &str, value: &JsValue| {
let (explainer, hints) = value.explain(2, 0);
NextSegmentConfigParsingIssue {
ident: module.ident(),
ident: source.ident(),
detail: Vc::cell(format!("{detail} Got {explainer}.{hints}")),
source: issue_source(source, span),
}
......@@ -397,7 +401,6 @@ fn parse_config_value(
#[turbo_tasks::function]
pub async fn parse_segment_config_from_loader_tree(
loader_tree: Vc<LoaderTree>,
context: Vc<Box<dyn AssetContext>>,
) -> Result<Vc<NextSegmentConfig>> {
let loader_tree = loader_tree.await?;
let components = loader_tree.components.await?;
......@@ -406,7 +409,7 @@ pub async fn parse_segment_config_from_loader_tree(
.parallel_routes
.values()
.copied()
.map(|tree| parse_segment_config_from_loader_tree(tree, context))
.map(parse_segment_config_from_loader_tree)
.try_join()
.await?;
for tree in parallel_configs {
......@@ -417,18 +420,8 @@ pub async fn parse_segment_config_from_loader_tree(
.flatten()
{
let source = Vc::upcast(FileSource::new(component));
config.apply_parent_config(
&*parse_segment_config_from_source(
context.process(
source,
turbo_tasks::Value::new(ReferenceType::EcmaScriptModules(
EcmaScriptModulesReferenceSubType::Undefined,
)),
),
source,
)
.await?,
);
config.apply_parent_config(&*parse_segment_config_from_source(source).await?);
}
Ok(config.cell())
}
......@@ -3,7 +3,6 @@ use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use turbo_tasks::{debug::ValueDebugFormat, trace::TraceRawVcs, TryJoinIterExt, Vc};
use turbopack_binding::turbopack::{
build::BuildChunkingContext,
core::{chunk::ChunkingContextExt, output::OutputAssets},
ecmascript::chunk::EcmascriptChunkingContext,
};
......@@ -32,7 +31,7 @@ pub struct ClientReferencesChunks(IndexMap<ClientReferenceType, ClientReferenceC
pub async fn get_app_client_references_chunks(
app_client_reference_types: Vc<ClientReferenceTypes>,
client_chunking_context: Vc<Box<dyn EcmascriptChunkingContext>>,
ssr_chunking_context: Vc<BuildChunkingContext>,
ssr_chunking_context: Vc<Box<dyn EcmascriptChunkingContext>>,
) -> Result<Vc<ClientReferencesChunks>> {
let app_client_references_chunks: IndexMap<_, _> = app_client_reference_types
.await?
......
......@@ -25,6 +25,8 @@ use crate::{
loader_tree::LoaderTreeModule,
mode::NextMode,
next_app::{AppPage, AppPath},
next_config::NextConfig,
next_edge::entry::wrap_edge_entry,
next_server_component::NextServerComponentTransition,
parse_segment_config_from_loader_tree,
util::{file_content_rope, load_next_js_template, NextRuntime},
......@@ -38,8 +40,9 @@ pub async fn get_app_page_entry(
loader_tree: Vc<LoaderTree>,
page: AppPage,
project_root: Vc<FileSystemPath>,
next_config: Vc<NextConfig>,
) -> 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 context = if is_edge {
edge_context
......@@ -109,7 +112,13 @@ pub async fn get_app_page_entry(
);
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) =
......@@ -127,21 +136,52 @@ pub async fn get_app_page_entry(
.cell())
}
async fn wrap_edge_entry(
context: Vc<ModuleAssetContext>,
#[turbo_tasks::function]
async fn wrap_edge_page(
context: Vc<Box<dyn AssetContext>>,
project_root: Vc<FileSystemPath>,
entry: Vc<Box<dyn Module>>,
page: AppPage,
next_config: Vc<NextConfig>,
) -> 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(
"edge-app-route.js",
"edge-ssr-app.js",
project_root,
indexmap! {
"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?;
......@@ -149,8 +189,15 @@ async fn wrap_edge_entry(
INNER.to_string() => entry
};
Ok(context.process(
let wrapped = context.process(
Vc::upcast(source),
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 indexmap::indexmap;
use indoc::writedoc;
use turbo_tasks::{Value, ValueToString, Vc};
use turbopack_binding::{
turbo::tasks_fs::{rope::RopeBuilder, File, FileSystemPath},
turbo::tasks_fs::FileSystemPath,
turbopack::{
core::{
asset::AssetContent,
context::AssetContext,
module::Module,
reference_type::{EntryReferenceSubType, ReferenceType},
source::Source,
virtual_source::VirtualSource,
},
ecmascript::{chunk::EcmascriptChunkPlaceable, utils::StringifyJs},
ecmascript::chunk::EcmascriptChunkPlaceable,
turbopack::ModuleAssetContext,
},
};
use crate::{
next_app::{AppEntry, AppPage, AppPath},
next_edge::entry::wrap_edge_entry,
parse_segment_config_from_source,
util::{load_next_js_template, NextRuntime},
};
......@@ -35,13 +31,7 @@ pub async fn get_app_route_entry(
page: AppPage,
project_root: Vc<FileSystemPath>,
) -> Result<Vc<AppEntry>> {
let config = parse_segment_config_from_source(
nodejs_context.process(
source,
Value::new(ReferenceType::Entry(EntryReferenceSubType::AppRoute)),
),
source,
);
let config = parse_segment_config_from_source(source);
let is_edge = matches!(config.await?.runtime, Some(NextRuntime::Edge));
let context = if is_edge {
edge_context
......@@ -92,7 +82,12 @@ pub async fn get_app_route_entry(
);
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) =
......@@ -111,39 +106,33 @@ pub async fn get_app_route_entry(
}
#[turbo_tasks::function]
pub async fn wrap_edge_entry(
context: Vc<ModuleAssetContext>,
async fn wrap_edge_route(
context: Vc<Box<dyn AssetContext>>,
project_root: Vc<FileSystemPath>,
entry: Vc<Box<dyn Module>>,
pathname: String,
) -> Result<Vc<Box<dyn Module>>> {
let mut source = RopeBuilder::default();
writedoc!(
source,
r#"
import {{ EdgeRouteModuleWrapper }} from 'next/dist/esm/server/web/edge-route-module-wrapper'
import * as module from "MODULE"
self._ENTRIES ||= {{}}
self._ENTRIES[{}] = {{
ComponentMod: module,
default: EdgeRouteModuleWrapper.wrap(module.routeModule),
}}
"#,
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()),
);
const INNER: &str = "INNER_ROUTE_ENTRY";
let source = load_next_js_template(
"edge-app-route.js",
project_root,
indexmap! {
"VAR_USERLAND" => INNER.to_string(),
},
indexmap! {},
indexmap! {},
)
.await?;
let inner_assets = indexmap! {
"MODULE".to_string() => entry
INNER.to_string() => entry
};
Ok(context.process(
Vc::upcast(virtual_source),
let wrapped = context.process(
Vc::upcast(source),
Value::new(ReferenceType::Internal(Vc::cell(inner_assets))),
))
);
Ok(wrap_edge_entry(context, project_root, wrapped, pathname))
}
......@@ -445,6 +445,9 @@ pub struct ExperimentalConfig {
/// 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
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
......@@ -487,13 +490,10 @@ pub struct ExperimentalConfig {
ppr: Option<bool>,
taint: Option<bool>,
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.
server_minification: Option<bool>,
/// Enables source maps generation for the server production bundle.
server_source_maps: Option<bool>,
sri: Option<serde_json::Value>,
swc_minify: Option<bool>,
swc_trace_profiling: Option<bool>,
/// @internal Used by the Next.js internals only.
......@@ -509,9 +509,15 @@ pub struct ExperimentalConfig {
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)]
#[serde(untagged)]
enum SizeLimit {
pub enum SizeLimit {
Number(f64),
WithUnit(String),
}
......
......@@ -16,6 +16,7 @@ use super::{ClientReferenceManifest, ManifestNode, ManifestNodeEntry, ModuleId};
use crate::{
next_app::ClientReferencesChunks,
next_client_reference::{ClientReferenceType, ClientReferences},
util::NextRuntime,
};
#[turbo_tasks::value_impl]
......@@ -30,6 +31,7 @@ impl ClientReferenceManifest {
client_chunking_context: Vc<Box<dyn EcmascriptChunkingContext>>,
ssr_chunking_context: Vc<Box<dyn EcmascriptChunkingContext>>,
asset_prefix: Vc<Option<String>>,
runtime: NextRuntime,
) -> Result<Vc<Box<dyn OutputAsset>>> {
let mut entry_manifest: ClientReferenceManifest = Default::default();
entry_manifest.module_loading.prefix = asset_prefix
......@@ -171,15 +173,31 @@ impl ClientReferenceManifest {
ManifestNodeEntry {
name: "*".to_string(),
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)
r#async: false,
},
);
entry_manifest
.ssr_module_mapping
.insert((&*client_module_id).into(), ssr_manifest_node);
match runtime {
NextRuntime::NodeJs => {
entry_manifest
.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);
}
}
}
}
}
......
......@@ -215,7 +215,7 @@ pub struct ModuleLoading {
pub cross_origin: Option<String>,
}
#[derive(Serialize, Default, Debug)]
#[derive(Serialize, Default, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ManifestNode {
/// Mapping of export name to manifest node entry.
......@@ -223,7 +223,7 @@ pub struct ManifestNode {
pub module_exports: HashMap<String, ManifestNodeEntry>,
}
#[derive(Serialize, Debug)]
#[derive(Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ManifestNodeEntry {
/// Turbopack module ID.
......@@ -236,7 +236,7 @@ pub struct ManifestNodeEntry {
pub r#async: bool,
}
#[derive(Serialize, Debug, Eq, PartialEq, Hash)]
#[derive(Serialize, Debug, Eq, PartialEq, Hash, Clone)]
#[serde(rename_all = "camelCase")]
#[serde(untagged)]
pub enum ModuleId {
......
......@@ -2,6 +2,7 @@ use std::io::Write;
use anyhow::{bail, Result};
use indexmap::indexmap;
use serde::Serialize;
use turbo_tasks::Vc;
use turbo_tasks_fs::FileSystemPath;
use turbopack_binding::{
......@@ -13,15 +14,17 @@ use turbopack_binding::{
core::{
asset::{Asset, AssetContent},
context::AssetContext,
module::Module,
reference_type::{EntryReferenceSubType, ReferenceType},
source::Source,
virtual_source::VirtualSource,
},
ecmascript::chunk::EcmascriptChunkPlaceable,
ecmascript::{chunk::EcmascriptChunkPlaceable, utils::StringifyJs},
},
};
use crate::{
next_config::NextConfig,
next_edge::entry::wrap_edge_entry,
util::{file_content_rope, load_next_js_template, NextRuntime},
};
......@@ -35,6 +38,7 @@ pub async fn create_page_ssr_entry_module(
source: Vc<Box<dyn Source>>,
next_original_name: Vc<String>,
runtime: NextRuntime,
next_config: Vc<NextConfig>,
) -> Result<Vc<Box<dyn EcmascriptChunkPlaceable>>> {
let definition_page = &*next_original_name.await?;
let definition_pathname = &*pathname.await?;
......@@ -120,12 +124,23 @@ pub async fn create_page_ssr_entry_module(
);
if matches!(runtime, NextRuntime::Edge) {
ssr_module = wrap_edge_entry(
ssr_module_context,
project_root,
ssr_module,
definition_pathname.to_string(),
);
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_context,
project_root,
ssr_module,
definition_pathname.to_string(),
);
}
}
let Some(ssr_module) =
......@@ -136,3 +151,105 @@ pub async fn create_page_ssr_entry_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(
page,
})
// TODO(timneutkens): remove this
const isServerComponent =
isInsideAppDir && staticInfo.rsc !== RSC_MODULE_TYPES.client
......
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 { RouteKind } from '../../server/future/route-kind'
......
......@@ -80,6 +80,8 @@ import {
PHASE_DEVELOPMENT_SERVER,
SERVER_REFERENCE_MANIFEST,
REACT_LOADABLE_MANIFEST,
MIDDLEWARE_REACT_LOADABLE_MANIFEST,
MIDDLEWARE_BUILD_MANIFEST,
} from '../../../shared/lib/constants'
import { getMiddlewareRouteMatcher } from '../../../shared/lib/router/utils/middleware-route-matcher'
......@@ -418,6 +420,7 @@ async function startWatcher(opts: SetupOpts) {
const buildingIds = new Set()
const readyIds = new Set()
function startBuilding(id: string, forceRebuild: boolean = false) {
if (!forceRebuild && readyIds.has(id)) {
return () => {}
......@@ -795,11 +798,21 @@ async function startWatcher(opts: SetupOpts) {
): Promise<void> {
const buildManifest = mergeBuildManifests(buildManifests.values())
const buildManifestPath = path.join(distDir, BUILD_MANIFEST)
const middlewareBuildManifestPath = path.join(
distDir,
'server',
`${MIDDLEWARE_BUILD_MANIFEST}.js`
)
deleteCache(buildManifestPath)
deleteCache(middlewareBuildManifestPath)
await writeFileAtomic(
buildManifestPath,
JSON.stringify(buildManifest, null, 2)
)
await writeFileAtomic(
middlewareBuildManifestPath,
`self.__BUILD_MANIFEST=${JSON.stringify(buildManifest)}`
)
const content: ClientBuildManifest = {
__rewrites: rewrites
......@@ -885,7 +898,8 @@ async function startWatcher(opts: SetupOpts) {
)
const middlewareManifestPath = path.join(
distDir,
'server/middleware-manifest.json'
'server',
MIDDLEWARE_MANIFEST
)
deleteCache(middlewareManifestPath)
await writeFileAtomic(
......@@ -920,34 +934,50 @@ async function startWatcher(opts: SetupOpts) {
async function writeFontManifest(): Promise<void> {
// TODO: turbopack should write the correct
// version of this
const fontManifestPath = path.join(
const fontManifest = {
pages: {},
app: {},
appUsingSizeAdjust: false,
pagesUsingSizeAdjust: false,
}
const json = JSON.stringify(fontManifest, null, 2)
const fontManifestJsonPath = path.join(
distDir,
'server',
NEXT_FONT_MANIFEST + '.json'
`${NEXT_FONT_MANIFEST}.json`
)
deleteCache(fontManifestPath)
const fontManifestJsPath = path.join(
distDir,
'server',
`${NEXT_FONT_MANIFEST}.js`
)
deleteCache(fontManifestJsonPath)
deleteCache(fontManifestJsPath)
await writeFileAtomic(fontManifestJsonPath, json)
await writeFileAtomic(
fontManifestPath,
JSON.stringify(
{
pages: {},
app: {},
appUsingSizeAdjust: false,
pagesUsingSizeAdjust: false,
},
null,
2
)
fontManifestJsPath,
`self.__NEXT_FONT_MANIFEST=${JSON.stringify(json)}`
)
}
async function writeLoadableManifest(): Promise<void> {
const loadableManifest = mergeLoadableManifests(loadbleManifests.values())
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(middlewareloadableManifestPath)
await writeFileAtomic(loadableManifestPath, json)
await writeFileAtomic(
loadableManifestPath,
JSON.stringify(loadableManifest, null, 2)
middlewareloadableManifestPath,
`self.__REACT_LOADABLE_MANIFEST=${JSON.stringify(json)}`
)
}
......@@ -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 loadBuildManifest(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