Unverified Commit 84005941 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

add edge support for next.rs API (#52885)

### What?

adds edge pages, apis, app pages and app routes to next.rs api

### Why?

### How?
parent 8664ffe2
...@@ -8,16 +8,43 @@ use turbopack_binding::turbopack::core::error::PrettyPrintError; ...@@ -8,16 +8,43 @@ use turbopack_binding::turbopack::core::error::PrettyPrintError;
use super::utils::{get_issues, subscribe, NapiIssue, RootTask, TurbopackResult, VcArc}; use super::utils::{get_issues, subscribe, NapiIssue, RootTask, TurbopackResult, VcArc};
#[napi(object)] #[napi(object)]
#[derive(Default)]
pub struct NapiEndpointConfig {}
#[napi(object)]
#[derive(Default)]
pub struct NapiWrittenEndpoint { pub struct NapiWrittenEndpoint {
pub server_entry_path: String, pub r#type: String,
pub server_paths: Vec<String>, pub entry_path: Option<String>,
pub server_paths: Option<Vec<String>>,
pub files: Option<Vec<String>>,
pub global_var_name: Option<String>,
pub config: NapiEndpointConfig,
} }
impl From<&WrittenEndpoint> for NapiWrittenEndpoint { impl From<&WrittenEndpoint> for NapiWrittenEndpoint {
fn from(written_endpoint: &WrittenEndpoint) -> Self { fn from(written_endpoint: &WrittenEndpoint) -> Self {
Self { match written_endpoint {
server_entry_path: written_endpoint.server_entry_path.clone(), WrittenEndpoint::NodeJs {
server_paths: written_endpoint.server_paths.clone(), server_entry_path,
server_paths,
} => Self {
r#type: "nodejs".to_string(),
entry_path: Some(server_entry_path.clone()),
server_paths: Some(server_paths.clone()),
..Default::default()
},
WrittenEndpoint::Edge {
files,
global_var_name,
server_paths,
} => Self {
r#type: "edge".to_string(),
files: Some(files.clone()),
server_paths: Some(server_paths.clone()),
global_var_name: Some(global_var_name.clone()),
..Default::default()
},
} }
} }
} }
......
...@@ -4,6 +4,7 @@ use anyhow::Result; ...@@ -4,6 +4,7 @@ use anyhow::Result;
use indexmap::{map::Entry, IndexMap}; use indexmap::{map::Entry, IndexMap};
use next_core::{ use next_core::{
app_structure::find_app_dir, app_structure::find_app_dir,
get_edge_chunking_context, get_edge_compile_time_info,
mode::NextMode, mode::NextMode,
next_client::{get_client_chunking_context, get_client_compile_time_info}, next_client::{get_client_chunking_context, get_client_compile_time_info},
next_config::NextConfig, next_config::NextConfig,
...@@ -262,6 +263,15 @@ impl Project { ...@@ -262,6 +263,15 @@ impl Project {
)) ))
} }
#[turbo_tasks::function]
pub(super) fn edge_compile_time_info(self: Vc<Self>) -> Vc<CompileTimeInfo> {
get_edge_compile_time_info(
self.project_path(),
// TODO(alexkirsz) Fill this out.
ServerAddr::empty(),
)
}
#[turbo_tasks::function] #[turbo_tasks::function]
pub(super) async fn client_chunking_context( pub(super) async fn client_chunking_context(
self: Vc<Self>, self: Vc<Self>,
...@@ -276,31 +286,65 @@ impl Project { ...@@ -276,31 +286,65 @@ impl Project {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
pub(super) fn server_chunking_context(self: Vc<Self>) -> Vc<BuildChunkingContext> { fn server_chunking_context(self: Vc<Self>) -> Vc<BuildChunkingContext> {
get_server_chunking_context( get_server_chunking_context(
self.project_path(), self.project_path(),
self.node_root(), self.node_root(),
self.client_fs().root(), self.client_root(),
self.server_compile_time_info().environment(), self.server_compile_time_info().environment(),
) )
} }
#[turbo_tasks::function]
fn edge_chunking_context(self: Vc<Self>) -> Vc<Box<dyn EcmascriptChunkingContext>> {
get_edge_chunking_context(
self.project_path(),
self.node_root(),
self.client_root(),
self.edge_compile_time_info().environment(),
)
}
#[turbo_tasks::function] #[turbo_tasks::function]
pub(super) fn ssr_chunking_context(self: Vc<Self>) -> Vc<BuildChunkingContext> { pub(super) fn ssr_chunking_context(self: Vc<Self>) -> Vc<BuildChunkingContext> {
self.server_chunking_context().with_layer("ssr".to_string()) self.server_chunking_context().with_layer("ssr".to_string())
} }
#[turbo_tasks::function]
pub(super) fn edge_ssr_chunking_context(
self: Vc<Self>,
) -> Vc<Box<dyn EcmascriptChunkingContext>> {
self.edge_chunking_context()
.with_layer("edge ssr".to_string())
}
#[turbo_tasks::function] #[turbo_tasks::function]
pub(super) fn ssr_data_chunking_context(self: Vc<Self>) -> Vc<BuildChunkingContext> { pub(super) fn ssr_data_chunking_context(self: Vc<Self>) -> Vc<BuildChunkingContext> {
self.server_chunking_context() self.server_chunking_context()
.with_layer("ssr data".to_string()) .with_layer("ssr data".to_string())
} }
#[turbo_tasks::function]
pub(super) fn edge_ssr_data_chunking_context(
self: Vc<Self>,
) -> Vc<Box<dyn EcmascriptChunkingContext>> {
self.edge_chunking_context()
.with_layer("edge ssr data".to_string())
}
#[turbo_tasks::function] #[turbo_tasks::function]
pub(super) fn rsc_chunking_context(self: Vc<Self>) -> Vc<BuildChunkingContext> { pub(super) fn rsc_chunking_context(self: Vc<Self>) -> Vc<BuildChunkingContext> {
self.server_chunking_context().with_layer("rsc".to_string()) self.server_chunking_context().with_layer("rsc".to_string())
} }
#[turbo_tasks::function]
pub(super) fn edge_rsc_chunking_context(
self: Vc<Self>,
) -> Vc<Box<dyn EcmascriptChunkingContext>> {
self.edge_chunking_context()
.with_layer("edge rsc".to_string())
}
/// Scans the app/pages directories for entry points files (matching the /// Scans the app/pages directories for entry points files (matching the
/// provided page_extensions). /// provided page_extensions).
#[turbo_tasks::function] #[turbo_tasks::function]
......
...@@ -29,11 +29,20 @@ pub trait Endpoint { ...@@ -29,11 +29,20 @@ pub trait Endpoint {
#[turbo_tasks::value(shared)] #[turbo_tasks::value(shared)]
#[derive(Debug)] #[derive(Debug)]
pub struct WrittenEndpoint { pub enum WrittenEndpoint {
NodeJs {
/// Relative to the root_path /// Relative to the root_path
pub server_entry_path: String, server_entry_path: String,
/// Relative to the root_path /// Relative to the root_path
pub server_paths: Vec<String>, server_paths: Vec<String>,
},
Edge {
/// Relative to the root_path
files: Vec<String>,
global_var_name: String,
/// Relative to the root_path
server_paths: Vec<String>,
},
} }
/// The routes as map from pathname to route. (pathname includes the leading /// The routes as map from pathname to route. (pathname includes the leading
......
...@@ -190,6 +190,8 @@ pub async fn get_app_entries( ...@@ -190,6 +190,8 @@ pub async fn get_app_entries(
.map(|(pathname, entrypoint)| async move { .map(|(pathname, entrypoint)| async move {
Ok(match entrypoint { Ok(match entrypoint {
Entrypoint::AppPage { loader_tree } => get_app_page_entry( Entrypoint::AppPage { loader_tree } => get_app_page_entry(
rsc_context,
// TODO add edge support
rsc_context, rsc_context,
*loader_tree, *loader_tree,
app_dir, app_dir,
...@@ -197,6 +199,8 @@ pub async fn get_app_entries( ...@@ -197,6 +199,8 @@ pub async fn get_app_entries(
project_root, project_root,
), ),
Entrypoint::AppRoute { path } => get_app_route_entry( Entrypoint::AppRoute { path } => get_app_route_entry(
rsc_context,
// TODO add edge support
rsc_context, rsc_context,
Vc::upcast(FileSource::new(*path)), Vc::upcast(FileSource::new(*path)),
pathname.clone(), pathname.clone(),
...@@ -212,6 +216,8 @@ pub async fn get_app_entries( ...@@ -212,6 +216,8 @@ pub async fn get_app_entries(
if let Some(favicon) = global_metadata.favicon { if let Some(favicon) = global_metadata.favicon {
entries.push(get_app_route_favicon_entry( entries.push(get_app_route_favicon_entry(
rsc_context,
// TODO add edge support
rsc_context, rsc_context,
favicon, favicon,
project_root, project_root,
......
...@@ -48,8 +48,14 @@ pub mod url_node; ...@@ -48,8 +48,14 @@ pub mod url_node;
pub mod util; pub mod util;
mod web_entry_source; mod web_entry_source;
pub use app_segment_config::{
parse_segment_config_from_loader_tree, parse_segment_config_from_source,
};
pub use app_source::create_app_source; pub use app_source::create_app_source;
pub use emit::{all_server_paths, emit_all_assets}; pub use emit::{all_server_paths, emit_all_assets};
pub use next_edge::context::{
get_edge_chunking_context, get_edge_compile_time_info, get_edge_resolve_options_context,
};
pub use page_loader::create_page_loader_entry_module; pub use page_loader::create_page_loader_entry_module;
pub use page_source::create_page_source; pub use page_source::create_page_source;
pub use turbopack_binding::{turbopack::node::source_map, *}; pub use turbopack_binding::{turbopack::node::source_map, *};
......
use turbo_tasks::Vc; use turbo_tasks::Vc;
use turbopack_binding::turbopack::ecmascript::chunk::EcmascriptChunkPlaceable; use turbopack_binding::turbopack::ecmascript::chunk::EcmascriptChunkPlaceable;
use crate::app_segment_config::NextSegmentConfig;
/// The entry module asset for a Next.js app route or page. /// The entry module asset for a Next.js app route or page.
#[turbo_tasks::value(shared)] #[turbo_tasks::value(shared)]
pub struct AppEntry { pub struct AppEntry {
...@@ -11,4 +13,6 @@ pub struct AppEntry { ...@@ -11,4 +13,6 @@ pub struct AppEntry {
pub original_name: String, pub original_name: String,
/// The RSC module asset for the route or page. /// The RSC module asset for the route or page.
pub rsc_entry: Vc<Box<dyn EcmascriptChunkPlaceable>>, pub rsc_entry: Vc<Box<dyn EcmascriptChunkPlaceable>>,
/// The source code config for this entry.
pub config: Vc<NextSegmentConfig>,
} }
...@@ -19,14 +19,15 @@ use crate::{app_structure::MetadataItem, next_app::AppEntry}; ...@@ -19,14 +19,15 @@ use crate::{app_structure::MetadataItem, next_app::AppEntry};
/// Computes the entry for a Next.js favicon file. /// Computes the entry for a Next.js favicon file.
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn get_app_route_favicon_entry( pub async fn get_app_route_favicon_entry(
rsc_context: Vc<ModuleAssetContext>, nodejs_context: Vc<ModuleAssetContext>,
edge_context: Vc<ModuleAssetContext>,
favicon: MetadataItem, favicon: MetadataItem,
project_root: Vc<FileSystemPath>, project_root: Vc<FileSystemPath>,
) -> Result<Vc<AppEntry>> { ) -> Result<Vc<AppEntry>> {
let path = match favicon { let path = match favicon {
// TODO(alexkirsz) Is there a difference here? // TODO(alexkirsz) Is there a difference here?
MetadataItem::Static { path } => path, MetadataItem::Static { path } => path,
MetadataItem::Dynamic { path } => path, MetadataItem::Dynamic { path: _ } => bail!("Dynamic metadata is not implemented yet"),
}; };
let mut code = RopeBuilder::default(); let mut code = RopeBuilder::default();
...@@ -79,7 +80,8 @@ pub async fn get_app_route_favicon_entry( ...@@ -79,7 +80,8 @@ pub async fn get_app_route_favicon_entry(
VirtualSource::new(project_root.join("todo.tsx".to_string()), AssetContent::file(file.into())); VirtualSource::new(project_root.join("todo.tsx".to_string()), AssetContent::file(file.into()));
Ok(get_app_route_entry( Ok(get_app_route_entry(
rsc_context, nodejs_context,
edge_context,
Vc::upcast(source), Vc::upcast(source),
// TODO(alexkirsz) Get this from the metadata? // TODO(alexkirsz) Get this from the metadata?
"/favicon.ico".to_string(), "/favicon.ico".to_string(),
......
...@@ -22,17 +22,27 @@ use crate::{ ...@@ -22,17 +22,27 @@ use crate::{
mode::NextMode, mode::NextMode,
next_app::UnsupportedDynamicMetadataIssue, next_app::UnsupportedDynamicMetadataIssue,
next_server_component::NextServerComponentTransition, next_server_component::NextServerComponentTransition,
parse_segment_config_from_loader_tree,
util::NextRuntime,
}; };
/// Computes the entry for a Next.js app page. /// Computes the entry for a Next.js app page.
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn get_app_page_entry( pub async fn get_app_page_entry(
context: Vc<ModuleAssetContext>, nodejs_context: Vc<ModuleAssetContext>,
edge_context: Vc<ModuleAssetContext>,
loader_tree: Vc<LoaderTree>, loader_tree: Vc<LoaderTree>,
app_dir: Vc<FileSystemPath>, app_dir: Vc<FileSystemPath>,
pathname: String, pathname: String,
project_root: Vc<FileSystemPath>, project_root: Vc<FileSystemPath>,
) -> Result<Vc<AppEntry>> { ) -> Result<Vc<AppEntry>> {
let config = parse_segment_config_from_loader_tree(loader_tree, Vc::upcast(nodejs_context));
let context = if matches!(config.await?.runtime, Some(NextRuntime::Edge)) {
nodejs_context
} else {
edge_context
};
let server_component_transition = Vc::upcast(NextServerComponentTransition::new()); let server_component_transition = Vc::upcast(NextServerComponentTransition::new());
let loader_tree = LoaderTreeModule::build( let loader_tree = LoaderTreeModule::build(
...@@ -111,6 +121,7 @@ pub async fn get_app_page_entry( ...@@ -111,6 +121,7 @@ pub async fn get_app_page_entry(
pathname: pathname.to_string(), pathname: pathname.to_string(),
original_name, original_name,
rsc_entry, rsc_entry,
config,
} }
.cell()) .cell())
} }
......
...@@ -11,7 +11,9 @@ use turbopack_binding::{ ...@@ -11,7 +11,9 @@ use turbopack_binding::{
core::{ core::{
asset::AssetContent, asset::AssetContent,
context::AssetContext, context::AssetContext,
reference_type::{EcmaScriptModulesReferenceSubType, ReferenceType}, reference_type::{
EcmaScriptModulesReferenceSubType, EntryReferenceSubType, ReferenceType,
},
source::Source, source::Source,
virtual_source::VirtualSource, virtual_source::VirtualSource,
}, },
...@@ -20,16 +22,30 @@ use turbopack_binding::{ ...@@ -20,16 +22,30 @@ use turbopack_binding::{
}, },
}; };
use crate::next_app::AppEntry; use crate::{next_app::AppEntry, parse_segment_config_from_source, util::NextRuntime};
/// Computes the entry for a Next.js app route. /// Computes the entry for a Next.js app route.
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn get_app_route_entry( pub async fn get_app_route_entry(
rsc_context: Vc<ModuleAssetContext>, nodejs_context: Vc<ModuleAssetContext>,
edge_context: Vc<ModuleAssetContext>,
source: Vc<Box<dyn Source>>, source: Vc<Box<dyn Source>>,
pathname: String, pathname: String,
project_root: Vc<FileSystemPath>, project_root: Vc<FileSystemPath>,
) -> Result<Vc<AppEntry>> { ) -> Result<Vc<AppEntry>> {
let config = parse_segment_config_from_source(
nodejs_context.process(
source,
Value::new(ReferenceType::Entry(EntryReferenceSubType::AppRoute)),
),
source,
);
let context = if matches!(config.await?.runtime, Some(NextRuntime::Edge)) {
nodejs_context
} else {
edge_context
};
let mut result = RopeBuilder::default(); let mut result = RopeBuilder::default();
let kind = "app-route"; let kind = "app-route";
...@@ -105,7 +121,7 @@ pub async fn get_app_route_entry( ...@@ -105,7 +121,7 @@ pub async fn get_app_route_entry(
AssetContent::file(file.into()), AssetContent::file(file.into()),
); );
let entry = rsc_context.process( let entry = context.process(
source, source,
Value::new(ReferenceType::EcmaScriptModules( Value::new(ReferenceType::EcmaScriptModules(
EcmaScriptModulesReferenceSubType::Undefined, EcmaScriptModulesReferenceSubType::Undefined,
...@@ -116,7 +132,7 @@ pub async fn get_app_route_entry( ...@@ -116,7 +132,7 @@ pub async fn get_app_route_entry(
"ENTRY".to_string() => entry "ENTRY".to_string() => entry
}; };
let rsc_entry = rsc_context.process( let rsc_entry = context.process(
Vc::upcast(virtual_source), Vc::upcast(virtual_source),
Value::new(ReferenceType::Internal(Vc::cell(inner_assets))), Value::new(ReferenceType::Internal(Vc::cell(inner_assets))),
); );
...@@ -131,6 +147,7 @@ pub async fn get_app_route_entry( ...@@ -131,6 +147,7 @@ pub async fn get_app_route_entry(
pathname: pathname.to_string(), pathname: pathname.to_string(),
original_name, original_name,
rsc_entry, rsc_entry,
config,
} }
.cell()) .cell())
} }
......
...@@ -11,15 +11,17 @@ use turbopack_binding::{ ...@@ -11,15 +11,17 @@ use turbopack_binding::{
environment::{EdgeWorkerEnvironment, Environment, ExecutionEnvironment, ServerAddr}, environment::{EdgeWorkerEnvironment, Environment, ExecutionEnvironment, ServerAddr},
free_var_references, free_var_references,
}, },
node::execution_context::ExecutionContext, dev::DevChunkingContext,
ecmascript::chunk::EcmascriptChunkingContext,
node::{debug::should_debug, execution_context::ExecutionContext},
turbopack::resolve_options_context::ResolveOptionsContext, turbopack::resolve_options_context::ResolveOptionsContext,
}, },
}; };
use crate::{ use crate::{
mode::NextMode, next_config::NextConfig, next_import_map::get_next_edge_import_map, mode::NextMode, next_client::context::get_client_assets_path, next_config::NextConfig,
next_server::context::ServerContextType, next_shared::resolve::UnsupportedModulesResolvePlugin, next_import_map::get_next_edge_import_map, next_server::context::ServerContextType,
util::foreign_code_context_condition, next_shared::resolve::UnsupportedModulesResolvePlugin, util::foreign_code_context_condition,
}; };
fn defines() -> CompileTimeDefines { fn defines() -> CompileTimeDefines {
...@@ -108,3 +110,23 @@ pub async fn get_edge_resolve_options_context( ...@@ -108,3 +110,23 @@ pub async fn get_edge_resolve_options_context(
} }
.cell()) .cell())
} }
#[turbo_tasks::function]
pub fn get_edge_chunking_context(
project_path: Vc<FileSystemPath>,
node_root: Vc<FileSystemPath>,
client_root: Vc<FileSystemPath>,
environment: Vc<Environment>,
) -> Vc<Box<dyn EcmascriptChunkingContext>> {
Vc::upcast(
DevChunkingContext::builder(
project_path,
node_root.join("edge".to_string()),
node_root.join("edge/chunks".to_string()),
get_client_assets_path(client_root),
environment,
)
.reference_chunk_source_maps(should_debug("edge"))
.build(),
)
}
...@@ -96,7 +96,20 @@ pub async fn foreign_code_context_condition( ...@@ -96,7 +96,20 @@ pub async fn foreign_code_context_condition(
Ok(result) Ok(result)
} }
#[derive(Default, PartialEq, Eq, Clone, Copy, Debug, TraceRawVcs, Serialize, Deserialize)] #[derive(
Default,
PartialEq,
Eq,
Clone,
Copy,
Debug,
TraceRawVcs,
Serialize,
Deserialize,
Hash,
PartialOrd,
Ord,
)]
#[serde(rename_all = "lowercase")] #[serde(rename_all = "lowercase")]
pub enum NextRuntime { pub enum NextRuntime {
#[default] #[default]
......
...@@ -450,13 +450,23 @@ interface EndpointConfig { ...@@ -450,13 +450,23 @@ interface EndpointConfig {
preferredRegion?: string preferredRegion?: string
} }
interface WrittenEndpoint { type WrittenEndpoint =
| {
type: 'nodejs'
/** The entry path for the endpoint. */ /** The entry path for the endpoint. */
entryPath: string entryPath: string
/** All paths that has been written for the endpoint. */ /** All server paths that has been written for the endpoint. */
paths: string[] serverPaths: string[]
config: EndpointConfig config: EndpointConfig
} }
| {
type: 'edge'
files: string[]
/** All server paths that has been written for the endpoint. */
serverPaths: string[]
globalVarName: string
config: EndpointConfig
}
// TODO(sokra) Support wasm option. // TODO(sokra) Support wasm option.
function bindingToApi(binding: any, _wasm: boolean) { function bindingToApi(binding: any, _wasm: boolean) {
......
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