Unverified Commit aed332b2 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Implement Next.rs API (#52983)

This implements the handling for the new `--experimental-turbo` flag
that keeps the Next.js routing handling in front of turbopack and
leverages the new Next.rs API.

---------
Co-authored-by: 's avatarTobias Koppers <tobias.koppers@googlemail.com>
Co-authored-by: 's avatarAlex Kirszenberg <alex.kirszenberg@vercel.com>
parent 7c67b121
use std::sync::Arc; use std::{path::PathBuf, sync::Arc};
use anyhow::Result; use anyhow::{Context, Result};
use napi::{bindgen_prelude::External, JsFunction}; use napi::{bindgen_prelude::External, JsFunction};
use next_api::{ use next_api::{
project::{Middleware, ProjectContainer, ProjectOptions}, project::{Middleware, ProjectContainer, ProjectOptions},
route::{Endpoint, Route}, route::{Endpoint, Route},
}; };
use next_core::tracing_presets::{
TRACING_NEXT_TARGETS, TRACING_NEXT_TURBOPACK_TARGETS, TRACING_NEXT_TURBO_TASKS_TARGETS,
};
use tracing_subscriber::{
prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt, EnvFilter, Registry,
};
use turbo_tasks::{TurboTasks, Vc}; use turbo_tasks::{TurboTasks, Vc};
use turbopack_binding::{ use turbopack_binding::{
turbo::tasks_memory::MemoryBackend, turbopack::core::error::PrettyPrintError, turbo::tasks_memory::MemoryBackend,
turbopack::{
cli_utils::{
exit::ExitGuard,
raw_trace::RawTraceLayer,
trace_writer::{TraceWriter, TraceWriterGuard},
tracing_presets::TRACING_OVERVIEW_TARGETS,
},
core::error::PrettyPrintError,
},
}; };
use super::{ use super::{
...@@ -71,12 +86,62 @@ impl From<NapiProjectOptions> for ProjectOptions { ...@@ -71,12 +86,62 @@ impl From<NapiProjectOptions> for ProjectOptions {
} }
} }
pub struct ProjectInstance {
turbo_tasks: Arc<TurboTasks<MemoryBackend>>,
container: Vc<ProjectContainer>,
#[allow(dead_code)]
guard: Option<ExitGuard<TraceWriterGuard>>,
}
#[napi(ts_return_type = "{ __napiType: \"Project\" }")] #[napi(ts_return_type = "{ __napiType: \"Project\" }")]
pub async fn project_new( pub async fn project_new(
options: NapiProjectOptions, options: NapiProjectOptions,
turbo_engine_options: NapiTurboEngineOptions, turbo_engine_options: NapiTurboEngineOptions,
) -> napi::Result<External<VcArc<Vc<ProjectContainer>>>> { ) -> napi::Result<External<ProjectInstance>> {
register(); register();
let trace = std::env::var("NEXT_TURBOPACK_TRACING").ok();
let guard = if let Some(mut trace) = trace {
// Trace presets
match trace.as_str() {
"overview" => {
trace = TRACING_OVERVIEW_TARGETS.join(",");
}
"next" => {
trace = TRACING_NEXT_TARGETS.join(",");
}
"turbopack" => {
trace = TRACING_NEXT_TURBOPACK_TARGETS.join(",");
}
"turbo-tasks" => {
trace = TRACING_NEXT_TURBO_TASKS_TARGETS.join(",");
}
_ => {}
}
let subscriber = Registry::default();
let subscriber = subscriber.with(EnvFilter::builder().parse(trace).unwrap());
let internal_dir = PathBuf::from(&options.project_path).join(".next");
std::fs::create_dir_all(&internal_dir)
.context("Unable to create .next directory")
.unwrap();
let trace_file = internal_dir.join("trace.log");
let trace_writer = std::fs::File::create(trace_file).unwrap();
let (trace_writer, guard) = TraceWriter::new(trace_writer);
let subscriber = subscriber.with(RawTraceLayer::new(trace_writer));
let guard = ExitGuard::new(guard).unwrap();
subscriber.init();
Some(guard)
} else {
None
};
let turbo_tasks = TurboTasks::new(MemoryBackend::new( let turbo_tasks = TurboTasks::new(MemoryBackend::new(
turbo_engine_options turbo_engine_options
.memory_limit .memory_limit
...@@ -84,7 +149,7 @@ pub async fn project_new( ...@@ -84,7 +149,7 @@ pub async fn project_new(
.unwrap_or(usize::MAX), .unwrap_or(usize::MAX),
)); ));
let options = options.into(); let options = options.into();
let project = turbo_tasks let container = turbo_tasks
.run_once(async move { .run_once(async move {
let project = ProjectContainer::new(options); let project = ProjectContainer::new(options);
let project = project.resolve().await?; let project = project.resolve().await?;
...@@ -93,24 +158,26 @@ pub async fn project_new( ...@@ -93,24 +158,26 @@ pub async fn project_new(
.await .await
.map_err(|e| napi::Error::from_reason(PrettyPrintError(&e).to_string()))?; .map_err(|e| napi::Error::from_reason(PrettyPrintError(&e).to_string()))?;
Ok(External::new_with_size_hint( Ok(External::new_with_size_hint(
VcArc::new(turbo_tasks, project), ProjectInstance {
turbo_tasks,
container,
guard,
},
100, 100,
)) ))
} }
#[napi(ts_return_type = "{ __napiType: \"Project\" }")] #[napi(ts_return_type = "{ __napiType: \"Project\" }")]
pub async fn project_update( pub async fn project_update(
#[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External< #[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<ProjectInstance>,
VcArc<Vc<ProjectContainer>>,
>,
options: NapiProjectOptions, options: NapiProjectOptions,
) -> napi::Result<()> { ) -> napi::Result<()> {
let turbo_tasks = project.turbo_tasks().clone(); let turbo_tasks = project.turbo_tasks.clone();
let options = options.into(); let options = options.into();
let project = **project; let container = project.container;
turbo_tasks turbo_tasks
.run_once(async move { .run_once(async move {
project.update(options).await?; container.update(options).await?;
Ok(()) Ok(())
}) })
.await .await
...@@ -221,18 +288,16 @@ struct NapiEntrypoints { ...@@ -221,18 +288,16 @@ struct NapiEntrypoints {
#[napi(ts_return_type = "{ __napiType: \"RootTask\" }")] #[napi(ts_return_type = "{ __napiType: \"RootTask\" }")]
pub fn project_entrypoints_subscribe( pub fn project_entrypoints_subscribe(
#[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External< #[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<ProjectInstance>,
VcArc<Vc<ProjectContainer>>,
>,
func: JsFunction, func: JsFunction,
) -> napi::Result<External<RootTask>> { ) -> napi::Result<External<RootTask>> {
let turbo_tasks = project.turbo_tasks().clone(); let turbo_tasks = project.turbo_tasks.clone();
let project = **project; let container = project.container;
subscribe( subscribe(
turbo_tasks.clone(), turbo_tasks.clone(),
func, func,
move || async move { move || async move {
let entrypoints = project.entrypoints(); let entrypoints = container.entrypoints();
let issues = get_issues(entrypoints).await?; let issues = get_issues(entrypoints).await?;
let diags = get_diagnostics(entrypoints).await?; let diags = get_diagnostics(entrypoints).await?;
......
...@@ -28,15 +28,15 @@ use next_core::{ ...@@ -28,15 +28,15 @@ use next_core::{
util::NextRuntime, util::NextRuntime,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use turbo_tasks::{trace::TraceRawVcs, Completion, TryJoinIterExt, Value, Vc}; use turbo_tasks::{trace::TraceRawVcs, Completion, TryFlatJoinIterExt, TryJoinIterExt, Value, Vc};
use turbopack_binding::{ use turbopack_binding::{
turbo::{ turbo::{
tasks_env::{CustomProcessEnv, ProcessEnv}, tasks_env::{CustomProcessEnv, ProcessEnv},
tasks_fs::{File, FileSystemPath}, tasks_fs::{rope::RopeBuilder, File, FileContent, FileSystemPath},
}, },
turbopack::{ turbopack::{
core::{ core::{
asset::AssetContent, asset::{Asset, AssetContent},
changed::any_content_changed_of_output_assets, changed::any_content_changed_of_output_assets,
chunk::{ChunkableModule, ChunkingContext, EvaluatableAssets}, chunk::{ChunkableModule, ChunkingContext, EvaluatableAssets},
file_source::FileSource, file_source::FileSource,
...@@ -621,6 +621,29 @@ impl AppEndpoint { ...@@ -621,6 +621,29 @@ impl AppEndpoint {
); );
output_assets.push(entry_manifest); output_assets.push(entry_manifest);
fn create_app_paths_manifest(
node_root: Vc<FileSystemPath>,
original_name: &str,
filename: String,
) -> Result<Vc<Box<dyn OutputAsset>>> {
let path =
node_root.join(format!("server/app{original_name}/app-paths-manifest.json",));
let app_paths_manifest = AppPathsManifest {
node_server_app_paths: PagesManifest {
pages: [(original_name.to_string(), filename)]
.into_iter()
.collect(),
},
..Default::default()
};
Ok(Vc::upcast(VirtualOutputAsset::new(
path,
AssetContent::file(
File::from(serde_json::to_string_pretty(&app_paths_manifest)?).into(),
),
)))
}
let endpoint_output = match app_entry.config.await?.runtime.unwrap_or_default() { let endpoint_output = match app_entry.config.await?.runtime.unwrap_or_default() {
NextRuntime::Edge => { NextRuntime::Edge => {
let chunking_context = this.app_project.project().edge_rsc_chunking_context(); let chunking_context = this.app_project.project().edge_rsc_chunking_context();
...@@ -630,10 +653,30 @@ impl AppEndpoint { ...@@ -630,10 +653,30 @@ impl AppEndpoint {
.as_root_chunk(Vc::upcast(chunking_context)), .as_root_chunk(Vc::upcast(chunking_context)),
this.app_project.edge_rsc_runtime_entries(), this.app_project.edge_rsc_runtime_entries(),
); );
output_assets.extend(files.await?.iter().copied()); // TODO concatenation is not good, we should use all files once next.js supports
// that output_assets.extend(files.await?.iter().copied());
let file = concatenate_output_assets(
server_path.join(format!(
"app/{original_name}.js",
original_name = app_entry.original_name
)),
files,
);
output_assets.push(file);
let app_paths_manifest_output = create_app_paths_manifest(
node_root,
&app_entry.original_name,
server_path
.await?
.get_path_to(&*file.ident().path().await?)
.expect("edge bundle path should be within app paths manifest directory")
.to_string(),
)?;
output_assets.push(app_paths_manifest_output);
AppEndpointOutput::Edge { AppEndpointOutput::Edge {
files, file,
output_assets: Vc::cell(output_assets), output_assets: Vc::cell(output_assets),
} }
} }
...@@ -652,32 +695,15 @@ impl AppEndpoint { ...@@ -652,32 +695,15 @@ impl AppEndpoint {
); );
output_assets.push(rsc_chunk); output_assets.push(rsc_chunk);
let app_paths_manifest = AppPathsManifest { let app_paths_manifest_output = create_app_paths_manifest(
node_server_app_paths: PagesManifest { node_root,
pages: [( &app_entry.original_name,
app_entry.original_name.clone(),
server_path server_path
.await? .await?
.get_path_to(&*rsc_chunk.ident().path().await?) .get_path_to(&*rsc_chunk.ident().path().await?)
.expect( .expect("RSC chunk path should be within app paths manifest directory")
"RSC chunk path should be within app paths manifest directory",
)
.to_string(), .to_string(),
)] )?;
.into_iter()
.collect(),
},
..Default::default()
};
let app_paths_manifest_output = Vc::upcast(VirtualOutputAsset::new(
node_root.join(format!(
"server/app{original_name}/app-paths-manifest.json",
original_name = app_entry.original_name
)),
AssetContent::file(
File::from(serde_json::to_string_pretty(&app_paths_manifest)?).into(),
),
));
output_assets.push(app_paths_manifest_output); output_assets.push(app_paths_manifest_output);
AppEndpointOutput::NodeJs { AppEndpointOutput::NodeJs {
...@@ -690,6 +716,36 @@ impl AppEndpoint { ...@@ -690,6 +716,36 @@ impl AppEndpoint {
} }
} }
#[turbo_tasks::function]
async fn concatenate_output_assets(
path: Vc<FileSystemPath>,
files: Vc<OutputAssets>,
) -> Result<Vc<Box<dyn OutputAsset>>> {
let mut concatenated_content = RopeBuilder::default();
let contents = files
.await?
.iter()
.map(|&file| async move {
Ok(
if let AssetContent::File(content) = *file.content().await? {
Some(content.await?)
} else {
None
},
)
})
.try_flat_join()
.await?;
for file in contents.iter().flat_map(|content| content.as_content()) {
concatenated_content.concat(file.content());
concatenated_content.push_static_bytes(b"\n\n");
}
Ok(Vc::upcast(VirtualOutputAsset::new(
path,
AssetContent::file(FileContent::Content(concatenated_content.build().into()).cell()),
)))
}
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
impl Endpoint for AppEndpoint { impl Endpoint for AppEndpoint {
#[turbo_tasks::function] #[turbo_tasks::function]
...@@ -722,25 +778,18 @@ impl Endpoint for AppEndpoint { ...@@ -722,25 +778,18 @@ impl Endpoint for AppEndpoint {
} => WrittenEndpoint::NodeJs { } => WrittenEndpoint::NodeJs {
server_entry_path: node_root_ref server_entry_path: node_root_ref
.get_path_to(&*rsc_chunk.ident().path().await?) .get_path_to(&*rsc_chunk.ident().path().await?)
.context("rsc chunk entry path must be inside the node root")? .context("Node.js chunk entry path must be inside the node root")?
.to_string(), .to_string(),
server_paths, server_paths,
}, },
AppEndpointOutput::Edge { AppEndpointOutput::Edge {
files, file,
output_assets: _, output_assets: _,
} => WrittenEndpoint::Edge { } => WrittenEndpoint::Edge {
files: files files: vec![node_root_ref
.await?
.iter()
.map(|&file| async move {
Ok(node_root_ref
.get_path_to(&*file.ident().path().await?) .get_path_to(&*file.ident().path().await?)
.context("ssr chunk file path must be inside the node root")? .context("edge chunk file path must be inside the node root")?
.to_string()) .to_string()],
})
.try_join()
.await?,
global_var_name: "TODO".to_string(), global_var_name: "TODO".to_string(),
server_paths, server_paths,
}, },
...@@ -762,7 +811,7 @@ enum AppEndpointOutput { ...@@ -762,7 +811,7 @@ enum AppEndpointOutput {
output_assets: Vc<OutputAssets>, output_assets: Vc<OutputAssets>,
}, },
Edge { Edge {
files: Vc<OutputAssets>, file: Vc<Box<dyn OutputAsset>>,
output_assets: Vc<OutputAssets>, output_assets: Vc<OutputAssets>,
}, },
} }
...@@ -777,7 +826,7 @@ impl AppEndpointOutput { ...@@ -777,7 +826,7 @@ impl AppEndpointOutput {
output_assets, output_assets,
} => output_assets, } => output_assets,
AppEndpointOutput::Edge { AppEndpointOutput::Edge {
files: _, file: _,
output_assets, output_assets,
} => output_assets, } => output_assets,
} }
......
...@@ -10,6 +10,7 @@ use next_core::{ ...@@ -10,6 +10,7 @@ use next_core::{
}, },
next_dynamic::NextDynamicTransition, next_dynamic::NextDynamicTransition,
next_manifests::{BuildManifest, PagesManifest}, next_manifests::{BuildManifest, PagesManifest},
next_pages::create_page_ssr_entry_module,
next_server::{ next_server::{
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,
...@@ -17,7 +18,8 @@ use next_core::{ ...@@ -17,7 +18,8 @@ use next_core::{
pages_structure::{ pages_structure::{
find_pages_structure, PagesDirectoryStructure, PagesStructure, PagesStructureItem, find_pages_structure, PagesDirectoryStructure, PagesStructure, PagesStructureItem,
}, },
util::{parse_config_from_source, NextRuntime}, util::{get_asset_prefix_from_pathname, parse_config_from_source, NextRuntime},
PageLoaderAsset,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use turbo_tasks::{trace::TraceRawVcs, Completion, TaskInput, TryJoinIterExt, Value, Vc}; use turbo_tasks::{trace::TraceRawVcs, Completion, TaskInput, TryJoinIterExt, Value, Vc};
...@@ -31,12 +33,18 @@ use turbopack_binding::{ ...@@ -31,12 +33,18 @@ use turbopack_binding::{
chunk::{ChunkableModule, ChunkingContext, EvaluatableAssets}, chunk::{ChunkableModule, ChunkingContext, EvaluatableAssets},
context::AssetContext, context::AssetContext,
file_source::FileSource, file_source::FileSource,
issue::{IssueSeverity, OptionIssueSource},
output::{OutputAsset, OutputAssets}, output::{OutputAsset, OutputAssets},
reference_type::{EntryReferenceSubType, ReferenceType}, reference_type::{
EcmaScriptModulesReferenceSubType, EntryReferenceSubType, ReferenceType,
},
resolve::{origin::PlainResolveOrigin, parse::Request, pattern::Pattern},
source::Source, source::Source,
virtual_output::VirtualOutputAsset, virtual_output::VirtualOutputAsset,
}, },
ecmascript::{chunk::EcmascriptChunkingContext, EcmascriptModuleAsset}, ecmascript::{
chunk::EcmascriptChunkingContext, resolve::esm_resolve, EcmascriptModuleAsset,
},
turbopack::{ turbopack::{
module_options::ModuleOptionsContext, module_options::ModuleOptionsContext,
resolve_options_context::ResolveOptionsContext, resolve_options_context::ResolveOptionsContext,
...@@ -501,11 +509,9 @@ impl PageEndpoint { ...@@ -501,11 +509,9 @@ impl PageEndpoint {
async fn client_chunks(self: Vc<Self>) -> Result<Vc<OutputAssets>> { async fn client_chunks(self: Vc<Self>) -> Result<Vc<OutputAssets>> {
let this = self.await?; let this = self.await?;
let client_module = create_page_loader_entry_module( let client_module_context = this.pages_project.client_module_context();
this.pages_project.client_module_context(), let client_module =
self.source(), create_page_loader_entry_module(client_module_context, self.source(), this.pathname);
this.pathname,
);
let Some(client_module) = let Some(client_module) =
Vc::try_resolve_downcast_type::<EcmascriptModuleAsset>(client_module).await? Vc::try_resolve_downcast_type::<EcmascriptModuleAsset>(client_module).await?
...@@ -513,18 +519,57 @@ impl PageEndpoint { ...@@ -513,18 +519,57 @@ impl PageEndpoint {
bail!("expected an ECMAScript module asset"); bail!("expected an ECMAScript module asset");
}; };
let Some(client_main_module) = *esm_resolve(
Vc::upcast(PlainResolveOrigin::new(
client_module_context,
this.pages_project
.project()
.project_path()
.join("_".to_string()),
)),
Request::parse(Value::new(Pattern::Constant(
"next/dist/client/next-dev-turbopack.js".to_string(),
))),
Value::new(EcmaScriptModulesReferenceSubType::Undefined),
OptionIssueSource::none(),
IssueSeverity::Error.cell(),
)
.first_module()
.await?
else {
bail!("expected next/dist/client/next-dev-turbopack.js to resolve to a module");
};
let Some(client_main_module) =
Vc::try_resolve_downcast_type::<EcmascriptModuleAsset>(client_main_module).await?
else {
bail!("expected an ECMAScript module asset");
};
let client_chunking_context = this.pages_project.project().client_chunking_context(); let client_chunking_context = this.pages_project.project().client_chunking_context();
let client_entry_chunk = client_module.as_root_chunk(Vc::upcast(client_chunking_context)); let client_entry_chunk = client_module.as_root_chunk(Vc::upcast(client_chunking_context));
let client_chunks = client_chunking_context.evaluated_chunk_group( let mut client_chunks = client_chunking_context
.evaluated_chunk_group(
client_entry_chunk, client_entry_chunk,
this.pages_project this.pages_project
.client_runtime_entries() .client_runtime_entries()
.with_entry(Vc::upcast(client_main_module))
.with_entry(Vc::upcast(client_module)), .with_entry(Vc::upcast(client_module)),
); )
.await?
.clone_value();
client_chunks.push(Vc::upcast(PageLoaderAsset::new(
this.pages_project.project().client_root(),
client_module_context,
Vc::upcast(client_chunking_context),
self.source(),
this.pathname,
)));
Ok(client_chunks) Ok(Vc::cell(client_chunks))
} }
#[turbo_tasks::function] #[turbo_tasks::function]
...@@ -547,13 +592,13 @@ impl PageEndpoint { ...@@ -547,13 +592,13 @@ impl PageEndpoint {
let is_edge = matches!(config.runtime, NextRuntime::Edge); let is_edge = matches!(config.runtime, NextRuntime::Edge);
if is_edge { if is_edge {
let ssr_module = edge_module_context.process(self.source(), reference_type.clone()); let ssr_module = create_page_ssr_entry_module(
this.pathname,
let Some(ssr_module) = reference_type,
Vc::try_resolve_downcast_type::<EcmascriptModuleAsset>(ssr_module).await? Vc::upcast(edge_module_context),
else { self.source(),
bail!("expected an ECMAScript module asset"); this.original_name,
}; );
let edge_files = edge_chunking_context.evaluated_chunk_group( let edge_files = edge_chunking_context.evaluated_chunk_group(
ssr_module.as_root_chunk(Vc::upcast(edge_chunking_context)), ssr_module.as_root_chunk(Vc::upcast(edge_chunking_context)),
...@@ -562,21 +607,20 @@ impl PageEndpoint { ...@@ -562,21 +607,20 @@ impl PageEndpoint {
Ok(SsrChunk::Edge { files: edge_files }.cell()) Ok(SsrChunk::Edge { files: edge_files }.cell())
} else { } else {
let Some(ssr_module) = let ssr_module = create_page_ssr_entry_module(
Vc::try_resolve_downcast_type::<EcmascriptModuleAsset>(ssr_module).await? this.pathname,
else { reference_type,
bail!("expected an ECMAScript module asset"); Vc::upcast(module_context),
}; self.source(),
this.original_name,
);
let asset_path = get_asset_path_from_pathname(&this.pathname.await?, ".js"); let asset_path = get_asset_path_from_pathname(&this.pathname.await?, ".js");
let ssr_entry_chunk_path_string = format!("pages{asset_path}"); let ssr_entry_chunk_path_string = format!("pages{asset_path}");
let ssr_entry_chunk_path = node_path.join(ssr_entry_chunk_path_string); let ssr_entry_chunk_path = node_path.join(ssr_entry_chunk_path_string);
let ssr_entry_chunk = chunking_context.entry_chunk( let ssr_entry_chunk =
ssr_entry_chunk_path, chunking_context.entry_chunk(ssr_entry_chunk_path, ssr_module, runtime_entries);
Vc::upcast(ssr_module),
runtime_entries,
);
Ok(SsrChunk::NodeJs { Ok(SsrChunk::NodeJs {
entry: ssr_entry_chunk, entry: ssr_entry_chunk,
...@@ -661,10 +705,10 @@ impl PageEndpoint { ...@@ -661,10 +705,10 @@ impl PageEndpoint {
.into_iter() .into_iter()
.collect(), .collect(),
}; };
let manifest_path_prefix = get_asset_prefix_from_pathname(&this.pathname.await?);
Ok(Vc::upcast(VirtualOutputAsset::new( Ok(Vc::upcast(VirtualOutputAsset::new(
node_root.join(format!( node_root.join(format!(
"server/pages{original_name}/pages-manifest.json", "server/pages{manifest_path_prefix}/pages-manifest.json",
original_name = this.original_name.await?
)), )),
AssetContent::file(File::from(serde_json::to_string_pretty(&pages_manifest)?).into()), AssetContent::file(File::from(serde_json::to_string_pretty(&pages_manifest)?).into()),
))) )))
...@@ -703,10 +747,10 @@ impl PageEndpoint { ...@@ -703,10 +747,10 @@ impl PageEndpoint {
.collect(), .collect(),
..Default::default() ..Default::default()
}; };
let manifest_path_prefix = get_asset_prefix_from_pathname(&this.pathname.await?);
Ok(Vc::upcast(VirtualOutputAsset::new( Ok(Vc::upcast(VirtualOutputAsset::new(
node_root.join(format!( node_root.join(format!(
"server/pages{original_name}/build-manifest.json", "server/pages{manifest_path_prefix}/build-manifest.json",
original_name = this.original_name.await?
)), )),
AssetContent::file(File::from(serde_json::to_string_pretty(&build_manifest)?).into()), AssetContent::file(File::from(serde_json::to_string_pretty(&build_manifest)?).into()),
))) )))
......
use std::io::Write;
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use indexmap::indexmap;
use indoc::writedoc;
use next_core::{ use next_core::{
create_page_loader_entry_module, get_asset_path_from_pathname, create_page_loader_entry_module, get_asset_path_from_pathname,
mode::NextMode, mode::NextMode,
...@@ -13,6 +9,7 @@ use next_core::{ ...@@ -13,6 +9,7 @@ use next_core::{
next_config::NextConfig, next_config::NextConfig,
next_dynamic::NextDynamicTransition, next_dynamic::NextDynamicTransition,
next_manifests::{BuildManifest, PagesManifest}, next_manifests::{BuildManifest, PagesManifest},
next_pages::create_page_ssr_entry_module,
next_server::{ next_server::{
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,
...@@ -24,15 +21,10 @@ use next_core::{ ...@@ -24,15 +21,10 @@ use next_core::{
}; };
use turbo_tasks::Vc; use turbo_tasks::Vc;
use turbopack_binding::{ use turbopack_binding::{
turbo::{ turbo::{tasks::Value, tasks_env::ProcessEnv, tasks_fs::FileSystemPath},
tasks::Value,
tasks_env::ProcessEnv,
tasks_fs::{rope::RopeBuilder, File, FileSystemPath},
},
turbopack::{ turbopack::{
build::BuildChunkingContext, build::BuildChunkingContext,
core::{ core::{
asset::AssetContent,
chunk::{ChunkableModule, ChunkingContext, EvaluatableAssets}, chunk::{ChunkableModule, ChunkingContext, EvaluatableAssets},
compile_time_info::CompileTimeInfo, compile_time_info::CompileTimeInfo,
context::AssetContext, context::AssetContext,
...@@ -40,11 +32,9 @@ use turbopack_binding::{ ...@@ -40,11 +32,9 @@ use turbopack_binding::{
output::OutputAsset, output::OutputAsset,
reference_type::{EntryReferenceSubType, ReferenceType}, reference_type::{EntryReferenceSubType, ReferenceType},
source::Source, source::Source,
virtual_source::VirtualSource,
}, },
ecmascript::{ ecmascript::{
chunk::{EcmascriptChunkPlaceable, EcmascriptChunkingContext}, chunk::{EcmascriptChunkPlaceable, EcmascriptChunkingContext},
utils::StringifyJs,
EcmascriptModuleAsset, EcmascriptModuleAsset,
}, },
node::execution_context::ExecutionContext, node::execution_context::ExecutionContext,
...@@ -203,7 +193,7 @@ async fn get_page_entries_for_root_directory( ...@@ -203,7 +193,7 @@ async fn get_page_entries_for_root_directory(
next_router_root, next_router_root,
app.next_router_path, app.next_router_path,
app.original_path, app.original_path,
PathType::Page, PathType::PagesPage,
)); ));
// This only makes sense on the server. // This only makes sense on the server.
...@@ -215,7 +205,7 @@ async fn get_page_entries_for_root_directory( ...@@ -215,7 +205,7 @@ async fn get_page_entries_for_root_directory(
next_router_root, next_router_root,
document.next_router_path, document.next_router_path,
document.original_path, document.original_path,
PathType::Page, PathType::PagesPage,
)); ));
// 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
...@@ -228,7 +218,7 @@ async fn get_page_entries_for_root_directory( ...@@ -228,7 +218,7 @@ async fn get_page_entries_for_root_directory(
next_router_root, next_router_root,
error.next_router_path, error.next_router_path,
error.original_path, error.original_path,
PathType::Page, PathType::PagesPage,
)); ));
if let Some(api) = api { if let Some(api) = api {
...@@ -238,7 +228,7 @@ async fn get_page_entries_for_root_directory( ...@@ -238,7 +228,7 @@ async fn get_page_entries_for_root_directory(
api, api,
next_router_root, next_router_root,
&mut entries, &mut entries,
PathType::PagesAPI, PathType::PagesApi,
) )
.await?; .await?;
} }
...@@ -250,7 +240,7 @@ async fn get_page_entries_for_root_directory( ...@@ -250,7 +240,7 @@ async fn get_page_entries_for_root_directory(
pages, pages,
next_router_root, next_router_root,
&mut entries, &mut entries,
PathType::Page, PathType::PagesPage,
) )
.await?; .await?;
} }
...@@ -327,127 +317,20 @@ async fn get_page_entry_for_file( ...@@ -327,127 +317,20 @@ async fn get_page_entry_for_file(
path_type: PathType, path_type: PathType,
) -> 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::Page => EntryReferenceSubType::Page, PathType::PagesPage => EntryReferenceSubType::Page,
PathType::PagesAPI => EntryReferenceSubType::PagesApi, PathType::PagesApi => EntryReferenceSubType::PagesApi,
_ => bail!("Invalid path type"), _ => bail!("Invalid path type"),
})); }));
let pathname = pathname_for_path(next_router_root, next_router_path, path_type); let pathname = pathname_for_path(next_router_root, next_router_path, path_type);
let original_name = next_original_path.await?.path.clone();
let definition_page = format!("/{}", next_original_path.await?); let ssr_module = create_page_ssr_entry_module(
let definition_pathname = pathname.await?; pathname,
reference_type,
let ssr_module = ssr_module_context.process(source, reference_type.clone()); ssr_module_context,
source,
let mut result = RopeBuilder::default(); Vc::cell(original_name),
match path_type {
PathType::Page => {
// Sourced from https://github.com/vercel/next.js/blob/2848ce51d1552633119c89ab49ff7fe2e4e91c91/packages/next/src/build/webpack/loaders/next-route-loader/index.ts
writedoc!(
result,
r#"
import RouteModule from "next/dist/server/future/route-modules/pages/module"
import {{ hoist }} from "next/dist/build/webpack/loaders/next-route-loader/helpers"
import Document from "@vercel/turbopack-next/pages/_document"
import App from "@vercel/turbopack-next/pages/_app"
import * as userland from "INNER"
export default hoist(userland, "default")
export const getStaticProps = hoist(userland, "getStaticProps")
export const getStaticPaths = hoist(userland, "getStaticPaths")
export const getServerSideProps = hoist(userland, "getServerSideProps")
export const config = hoist(userland, "config")
export const reportWebVitals = hoist(userland, "reportWebVitals")
export const unstable_getStaticProps = hoist(userland, "unstable_getStaticProps")
export const unstable_getStaticPaths = hoist(userland, "unstable_getStaticPaths")
export const unstable_getStaticParams = hoist(userland, "unstable_getStaticParams")
export const unstable_getServerProps = hoist(userland, "unstable_getServerProps")
export const unstable_getServerSideProps = hoist(userland, "unstable_getServerSideProps")
export const routeModule = new RouteModule({{
definition: {{
kind: "PAGES",
page: {definition_page},
pathname: {definition_pathname},
// The following aren't used in production, but are
// required for the RouteModule constructor.
bundlePath: "",
filename: "",
}},
components: {{
App,
Document,
}},
userland,
}})
"#,
definition_page = StringifyJs(&definition_page),
definition_pathname = StringifyJs(&definition_pathname),
)?;
// When we're building the instrumentation page (only when the
// instrumentation file conflicts with a page also labeled
// /instrumentation) hoist the `register` method.
if definition_page == "/instrumentation" || definition_page == "/src/instrumentation" {
writeln!(
result,
r#"export const register = hoist(userland, "register")"#
)?;
}
}
PathType::PagesAPI => {
// Sourced from https://github.com/vercel/next.js/blob/2848ce51d1552633119c89ab49ff7fe2e4e91c91/packages/next/src/build/webpack/loaders/next-route-loader/index.ts
writedoc!(
result,
r#"
import RouteModule from "next/dist/server/future/route-modules/pages-api/module"
import {{ hoist }} from "next/dist/build/webpack/loaders/next-route-loader/helpers"
import * as userland from "INNER"
export default hoist(userland, "default")
export const config = hoist(userland, "config")
export const routeModule = new RouteModule({{
definition: {{
kind: "PAGES_API",
page: {definition_page},
pathname: {definition_pathname},
// The following aren't used in production, but are
// required for the RouteModule constructor.
bundlePath: "",
filename: "",
}},
userland,
}})
"#,
definition_page = StringifyJs(&definition_page),
definition_pathname = StringifyJs(&definition_pathname),
)?;
}
_ => bail!("Invalid path type"),
};
let file = File::from(result.build());
let asset = VirtualSource::new(
source.ident().path().join(match path_type {
PathType::Page => "pages-entry.tsx".to_string(),
PathType::PagesAPI => "pages-api-entry.tsx".to_string(),
_ => bail!("Invalid path type"),
}),
AssetContent::file(file.into()),
);
let ssr_module = ssr_module_context.process(
Vc::upcast(asset),
Value::new(ReferenceType::Internal(Vc::cell(indexmap! {
"INNER".to_string() => ssr_module,
}))),
); );
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);
...@@ -458,12 +341,6 @@ async fn get_page_entry_for_file( ...@@ -458,12 +341,6 @@ async fn get_page_entry_for_file(
bail!("expected an ECMAScript module asset"); bail!("expected an ECMAScript module asset");
}; };
let Some(ssr_module) =
Vc::try_resolve_sidecast::<Box<dyn EcmascriptChunkPlaceable>>(ssr_module).await?
else {
bail!("expected an ECMAScript chunk placeable asset");
};
Ok(PageEntry { Ok(PageEntry {
pathname, pathname,
ssr_module, ssr_module,
...@@ -499,7 +376,7 @@ pub async fn compute_page_entries_chunks( ...@@ -499,7 +376,7 @@ pub async fn compute_page_entries_chunks(
let ssr_entry_chunk = ssr_chunking_context.entry_chunk( let ssr_entry_chunk = ssr_chunking_context.entry_chunk(
node_root.join(format!("server/pages/{asset_path}")), node_root.join(format!("server/pages/{asset_path}")),
page_entry.ssr_module, Vc::upcast(page_entry.ssr_module),
page_entries.ssr_runtime_entries, page_entries.ssr_runtime_entries,
); );
all_chunks.push(ssr_entry_chunk); all_chunks.push(ssr_entry_chunk);
......
...@@ -754,12 +754,7 @@ async fn directory_tree_to_entrypoints_internal( ...@@ -754,12 +754,7 @@ async fn directory_tree_to_entrypoints_internal(
// Next.js has this logic in "collect-app-paths", where the root not-found page // Next.js has this logic in "collect-app-paths", where the root not-found page
// is considered as its own entry point. // is considered as its own entry point.
if let Some(_not_found) = components.not_found { if let Some(_not_found) = components.not_found {
add_app_page( let tree = LoaderTree {
app_dir,
&mut result,
"/_not-found".to_string(),
"/_not-found".to_string(),
LoaderTree {
segment: directory_name.to_string(), segment: directory_name.to_string(),
parallel_routes: indexmap! { parallel_routes: indexmap! {
"children".to_string() => LoaderTree { "children".to_string() => LoaderTree {
...@@ -775,7 +770,21 @@ async fn directory_tree_to_entrypoints_internal( ...@@ -775,7 +770,21 @@ async fn directory_tree_to_entrypoints_internal(
}, },
components: components.without_leafs().cell(), components: components.without_leafs().cell(),
} }
.cell(), .cell();
add_app_page(
app_dir,
&mut result,
"/not-found".to_string(),
"/not-found".to_string(),
tree,
)
.await?;
add_app_page(
app_dir,
&mut result,
"/_not-found".to_string(),
"/_not-found".to_string(),
tree,
) )
.await?; .await?;
} }
......
...@@ -32,6 +32,7 @@ mod next_font; ...@@ -32,6 +32,7 @@ mod next_font;
pub mod next_image; pub mod next_image;
mod next_import_map; mod next_import_map;
pub mod next_manifests; pub mod next_manifests;
pub mod next_pages;
mod next_route_matcher; mod next_route_matcher;
pub mod next_server; pub mod next_server;
pub mod next_server_component; pub mod next_server_component;
...@@ -44,6 +45,7 @@ pub mod router; ...@@ -44,6 +45,7 @@ pub mod router;
pub mod router_source; pub mod router_source;
mod runtime; mod runtime;
mod sass; mod sass;
pub mod tracing_presets;
mod transform_options; mod transform_options;
pub mod url_node; pub mod url_node;
pub mod util; pub mod util;
...@@ -57,7 +59,7 @@ pub use emit::{all_server_paths, emit_all_assets}; ...@@ -57,7 +59,7 @@ pub use emit::{all_server_paths, emit_all_assets};
pub use next_edge::context::{ pub use next_edge::context::{
get_edge_chunking_context, get_edge_compile_time_info, get_edge_resolve_options_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, PageLoaderAsset};
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, *};
pub use util::{get_asset_path_from_pathname, pathname_for_path, PathType}; pub use util::{get_asset_path_from_pathname, pathname_for_path, PathType};
......
...@@ -113,9 +113,9 @@ pub async fn get_app_page_entry( ...@@ -113,9 +113,9 @@ pub async fn get_app_page_entry(
); );
let Some(rsc_entry) = let Some(rsc_entry) =
Vc::try_resolve_sidecast::<Box<dyn EcmascriptChunkPlaceable>>(rsc_entry).await? Vc::try_resolve_downcast::<Box<dyn EcmascriptChunkPlaceable>>(rsc_entry).await?
else { else {
bail!("expected an ECMAScript chunk placeable asset"); bail!("expected an ECMAScript chunk placeable module");
}; };
Ok(AppEntry { Ok(AppEntry {
...@@ -133,6 +133,7 @@ fn get_original_page_name(pathname: &str) -> String { ...@@ -133,6 +133,7 @@ fn get_original_page_name(pathname: &str) -> String {
match pathname { match pathname {
"/" => "/page".to_string(), "/" => "/page".to_string(),
"/_not-found" => "/_not-found".to_string(), "/_not-found" => "/_not-found".to_string(),
"/not-found" => "/not-found".to_string(),
_ => format!("{}/page", pathname), _ => format!("{}/page", pathname),
} }
} }
...@@ -139,9 +139,9 @@ pub async fn get_app_route_entry( ...@@ -139,9 +139,9 @@ pub async fn get_app_route_entry(
); );
let Some(rsc_entry) = let Some(rsc_entry) =
Vc::try_resolve_sidecast::<Box<dyn EcmascriptChunkPlaceable>>(rsc_entry).await? Vc::try_resolve_downcast::<Box<dyn EcmascriptChunkPlaceable>>(rsc_entry).await?
else { else {
bail!("expected an ECMAScript chunk placeable asset"); bail!("expected an ECMAScript chunk placeable module");
}; };
Ok(AppEntry { Ok(AppEntry {
......
...@@ -282,9 +282,13 @@ pub fn get_client_chunking_context( ...@@ -282,9 +282,13 @@ pub fn get_client_chunking_context(
environment: Vc<Environment>, environment: Vc<Environment>,
mode: NextMode, mode: NextMode,
) -> Vc<Box<dyn EcmascriptChunkingContext>> { ) -> Vc<Box<dyn EcmascriptChunkingContext>> {
let output_root = match mode {
NextMode::DevServer => client_root,
NextMode::Development | NextMode::Build => client_root.join("_next".to_string()),
};
let builder = DevChunkingContext::builder( let builder = DevChunkingContext::builder(
project_path, project_path,
client_root, output_root,
client_root.join("_next/static/chunks".to_string()), client_root.join("_next/static/chunks".to_string()),
get_client_assets_path(client_root), get_client_assets_path(client_root),
environment, environment,
......
pub(crate) mod page_entry;
pub use page_entry::create_page_ssr_entry_module;
use std::io::Write;
use anyhow::{bail, Result};
use indexmap::indexmap;
use indoc::writedoc;
use turbo_tasks::Vc;
use turbopack_binding::{
turbo::{
tasks::Value,
tasks_fs::{rope::RopeBuilder, File},
},
turbopack::{
core::{
asset::AssetContent,
context::AssetContext,
reference_type::{EntryReferenceSubType, ReferenceType},
source::Source,
virtual_source::VirtualSource,
},
ecmascript::{chunk::EcmascriptChunkPlaceable, utils::StringifyJs},
},
};
#[turbo_tasks::function]
pub async fn create_page_ssr_entry_module(
pathname: Vc<String>,
reference_type: Value<ReferenceType>,
ssr_module_context: Vc<Box<dyn AssetContext>>,
source: Vc<Box<dyn Source>>,
next_original_name: Vc<String>,
) -> Result<Vc<Box<dyn EcmascriptChunkPlaceable>>> {
let definition_page = next_original_name.await?;
let definition_pathname = pathname.await?;
let ssr_module = ssr_module_context.process(source, reference_type.clone());
let mut result = RopeBuilder::default();
let entry_suffix = match reference_type.into_value() {
ReferenceType::Entry(EntryReferenceSubType::Page) => {
// Sourced from https://github.com/vercel/next.js/blob/2848ce51d1552633119c89ab49ff7fe2e4e91c91/packages/next/src/build/webpack/loaders/next-route-loader/index.ts
writedoc!(
result,
r#"
import RouteModule from "next/dist/server/future/route-modules/pages/module"
import {{ hoist }} from "next/dist/build/webpack/loaders/next-route-loader/helpers"
import Document from "@vercel/turbopack-next/pages/_document"
import App from "@vercel/turbopack-next/pages/_app"
import * as userland from "INNER"
export default hoist(userland, "default")
export const getStaticProps = hoist(userland, "getStaticProps")
export const getStaticPaths = hoist(userland, "getStaticPaths")
export const getServerSideProps = hoist(userland, "getServerSideProps")
export const config = hoist(userland, "config")
export const reportWebVitals = hoist(userland, "reportWebVitals")
export const unstable_getStaticProps = hoist(userland, "unstable_getStaticProps")
export const unstable_getStaticPaths = hoist(userland, "unstable_getStaticPaths")
export const unstable_getStaticParams = hoist(userland, "unstable_getStaticParams")
export const unstable_getServerProps = hoist(userland, "unstable_getServerProps")
export const unstable_getServerSideProps = hoist(userland, "unstable_getServerSideProps")
export const routeModule = new RouteModule({{
definition: {{
kind: "PAGES",
page: {definition_page},
pathname: {definition_pathname},
// The following aren't used in production, but are
// required for the RouteModule constructor.
bundlePath: "",
filename: "",
}},
components: {{
App,
Document,
}},
userland,
}})
"#,
definition_page = StringifyJs(&definition_page),
definition_pathname = StringifyJs(&definition_pathname),
)?;
// When we're building the instrumentation page (only when the
// instrumentation file conflicts with a page also labeled
// /instrumentation) hoist the `register` method.
if definition_page.as_str() == "/instrumentation"
|| definition_page.as_str() == "/src/instrumentation"
{
writeln!(
result,
r#"export const register = hoist(userland, "register")"#
)?;
}
"pages-entry.tsx".to_string()
}
ReferenceType::Entry(EntryReferenceSubType::PagesApi) => {
// Sourced from https://github.com/vercel/next.js/blob/2848ce51d1552633119c89ab49ff7fe2e4e91c91/packages/next/src/build/webpack/loaders/next-route-loader/index.ts
writedoc!(
result,
r#"
import RouteModule from "next/dist/server/future/route-modules/pages-api/module"
import {{ hoist }} from "next/dist/build/webpack/loaders/next-route-loader/helpers"
import * as userland from "INNER"
export default hoist(userland, "default")
export const config = hoist(userland, "config")
export const routeModule = new RouteModule({{
definition: {{
kind: "PAGES_API",
page: {definition_page},
pathname: {definition_pathname},
// The following aren't used in production, but are
// required for the RouteModule constructor.
bundlePath: "",
filename: "",
}},
userland,
}})
"#,
definition_page = StringifyJs(&definition_page),
definition_pathname = StringifyJs(&definition_pathname),
)?;
"pages-api-entry.tsx".to_string()
}
reference_type => bail!("unexpected reference type: {:?}", reference_type),
};
let file = File::from(result.build());
let source = VirtualSource::new(
source.ident().path().join(entry_suffix),
AssetContent::file(file.into()),
);
let ssr_module = ssr_module_context.process(
Vc::upcast(source),
Value::new(ReferenceType::Internal(Vc::cell(indexmap! {
"INNER".to_string() => ssr_module,
}))),
);
let Some(ssr_module) =
Vc::try_resolve_downcast::<Box<dyn EcmascriptChunkPlaceable>>(ssr_module).await?
else {
bail!("expected an ECMAScript chunk placeable module");
};
Ok(ssr_module)
}
...@@ -33,7 +33,7 @@ pub async fn get_swc_ecma_transform_plugin_impl( ...@@ -33,7 +33,7 @@ pub async fn get_swc_ecma_transform_plugin_impl(
project_path: Vc<FileSystemPath>, project_path: Vc<FileSystemPath>,
plugin_configs: &[(String, serde_json::Value)], plugin_configs: &[(String, serde_json::Value)],
) -> Result<Vc<OptionTransformPlugin>> { ) -> Result<Vc<OptionTransformPlugin>> {
use anyhow::{bail, Context}; use anyhow::bail;
use turbo_tasks::Value; use turbo_tasks::Value;
use turbo_tasks_fs::FileContent; use turbo_tasks_fs::FileContent;
use turbopack_binding::turbopack::{ use turbopack_binding::turbopack::{
...@@ -41,10 +41,7 @@ pub async fn get_swc_ecma_transform_plugin_impl( ...@@ -41,10 +41,7 @@ pub async fn get_swc_ecma_transform_plugin_impl(
asset::Asset, asset::Asset,
issue::{IssueSeverity, OptionIssueSource}, issue::{IssueSeverity, OptionIssueSource},
reference_type::ReferenceType, reference_type::ReferenceType,
resolve::{ resolve::{handle_resolve_error, parse::Request, pattern::Pattern, resolve},
handle_resolve_error, parse::Request, pattern::Pattern, resolve,
ModuleResolveResultItem,
},
}, },
ecmascript_plugin::transform::swc_ecma_transform_plugins::{ ecmascript_plugin::transform::swc_ecma_transform_plugins::{
SwcEcmaTransformPluginsTransformer, SwcPluginModule, SwcEcmaTransformPluginsTransformer, SwcPluginModule,
......
...@@ -50,15 +50,6 @@ pub async fn create_page_loader( ...@@ -50,15 +50,6 @@ pub async fn create_page_loader(
))) )))
} }
#[turbo_tasks::value(shared)]
pub struct PageLoaderAsset {
pub server_root: Vc<FileSystemPath>,
pub client_context: Vc<Box<dyn AssetContext>>,
pub client_chunking_context: Vc<Box<dyn ChunkingContext>>,
pub entry_asset: Vc<Box<dyn Source>>,
pub pathname: Vc<String>,
}
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn create_page_loader_entry_module( pub async fn create_page_loader_entry_module(
client_context: Vc<Box<dyn AssetContext>>, client_context: Vc<Box<dyn AssetContext>>,
...@@ -98,8 +89,35 @@ pub async fn create_page_loader_entry_module( ...@@ -98,8 +89,35 @@ pub async fn create_page_loader_entry_module(
)) ))
} }
#[turbo_tasks::value(shared)]
pub struct PageLoaderAsset {
pub server_root: Vc<FileSystemPath>,
pub client_context: Vc<Box<dyn AssetContext>>,
pub client_chunking_context: Vc<Box<dyn ChunkingContext>>,
pub entry_asset: Vc<Box<dyn Source>>,
pub pathname: Vc<String>,
}
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
impl PageLoaderAsset { impl PageLoaderAsset {
#[turbo_tasks::function]
pub fn new(
server_root: Vc<FileSystemPath>,
client_context: Vc<Box<dyn AssetContext>>,
client_chunking_context: Vc<Box<dyn ChunkingContext>>,
entry_asset: Vc<Box<dyn Source>>,
pathname: Vc<String>,
) -> Vc<Self> {
Self {
server_root,
client_context,
client_chunking_context,
entry_asset,
pathname,
}
.cell()
}
#[turbo_tasks::function] #[turbo_tasks::function]
async fn get_page_chunks(self: Vc<Self>) -> Result<Vc<OutputAssets>> { async fn get_page_chunks(self: Vc<Self>) -> Result<Vc<OutputAssets>> {
let this = &*self.await?; let this = &*self.await?;
......
...@@ -379,7 +379,7 @@ async fn create_page_source_for_file( ...@@ -379,7 +379,7 @@ async fn create_page_source_for_file(
mode, mode,
); );
let pathname = pathname_for_path(client_root, client_path, PathType::Page); let pathname = pathname_for_path(client_root, client_path, PathType::PagesPage);
let route_matcher = NextParamsMatcher::new(pathname); let route_matcher = NextParamsMatcher::new(pathname);
let (base_segments, route_type) = pathname_to_segments(&pathname.await?, "")?; let (base_segments, route_type) = pathname_to_segments(&pathname.await?, "")?;
......
use once_cell::sync::Lazy;
use turbopack_binding::turbopack::cli_utils::tracing_presets::{
TRACING_OVERVIEW_TARGETS, TRACING_TURBOPACK_TARGETS, TRACING_TURBO_TASKS_TARGETS,
};
pub static TRACING_NEXT_TARGETS: Lazy<Vec<&str>> = Lazy::new(|| {
[
&TRACING_OVERVIEW_TARGETS[..],
&[
"next_dev=trace",
"next_core=trace",
"next_font=trace",
"turbopack_node=trace",
],
]
.concat()
});
pub static TRACING_NEXT_TURBOPACK_TARGETS: Lazy<Vec<&str>> =
Lazy::new(|| [&TRACING_NEXT_TARGETS[..], &TRACING_TURBOPACK_TARGETS[..]].concat());
pub static TRACING_NEXT_TURBO_TASKS_TARGETS: Lazy<Vec<&str>> = Lazy::new(|| {
[
&TRACING_TURBOPACK_TARGETS[..],
&TRACING_TURBO_TASKS_TARGETS[..],
]
.concat()
});
...@@ -28,8 +28,8 @@ use crate::next_config::{NextConfig, OutputType}; ...@@ -28,8 +28,8 @@ use crate::next_config::{NextConfig, OutputType};
#[derive(Debug, Clone, Copy, PartialEq, Eq, TaskInput)] #[derive(Debug, Clone, Copy, PartialEq, Eq, TaskInput)]
pub enum PathType { pub enum PathType {
Page, PagesPage,
PagesAPI, PagesApi,
Data, Data,
} }
...@@ -62,16 +62,23 @@ pub async fn pathname_for_path( ...@@ -62,16 +62,23 @@ pub async fn pathname_for_path(
} }
// Adapted from https://github.com/vercel/next.js/blob/canary/packages/next/shared/lib/router/utils/get-asset-path-from-route.ts // Adapted from https://github.com/vercel/next.js/blob/canary/packages/next/shared/lib/router/utils/get-asset-path-from-route.ts
pub fn get_asset_path_from_pathname(pathname: &str, ext: &str) -> String { // TODO(alexkirsz) There's no need to create an intermediate string here (and
// below), we should instead return an `impl Display`.
pub fn get_asset_prefix_from_pathname(pathname: &str) -> String {
if pathname == "/" { if pathname == "/" {
format!("/index{}", ext) "/index".to_string()
} else if pathname == "/index" || pathname.starts_with("/index/") { } else if pathname == "/index" || pathname.starts_with("/index/") {
format!("/index{}{}", pathname, ext) format!("/index{}", pathname)
} else { } else {
format!("{}{}", pathname, ext) pathname.to_string()
} }
} }
// Adapted from https://github.com/vercel/next.js/blob/canary/packages/next/shared/lib/router/utils/get-asset-path-from-route.ts
pub fn get_asset_path_from_pathname(pathname: &str, ext: &str) -> String {
format!("{}{}", get_asset_prefix_from_pathname(pathname), ext)
}
pub async fn foreign_code_context_condition( pub async fn foreign_code_context_condition(
next_config: Vc<NextConfig>, next_config: Vc<NextConfig>,
) -> Result<ContextCondition> { ) -> Result<ContextCondition> {
......
...@@ -32,8 +32,10 @@ use next_core::{ ...@@ -32,8 +32,10 @@ use next_core::{
pages_structure::find_pages_structure, pages_structure::find_pages_structure,
router_source::NextRouterContentSource, router_source::NextRouterContentSource,
source_map::NextSourceMapTraceContentSource, source_map::NextSourceMapTraceContentSource,
tracing_presets::{
TRACING_NEXT_TARGETS, TRACING_NEXT_TURBOPACK_TARGETS, TRACING_NEXT_TURBO_TASKS_TARGETS,
},
}; };
use once_cell::sync::Lazy;
use owo_colors::OwoColorize; use owo_colors::OwoColorize;
use tracing_subscriber::{prelude::*, EnvFilter, Registry}; use tracing_subscriber::{prelude::*, EnvFilter, Registry};
use turbo_tasks::{ use turbo_tasks::{
...@@ -53,9 +55,7 @@ use turbopack_binding::{ ...@@ -53,9 +55,7 @@ use turbopack_binding::{
issue::{ConsoleUi, LogOptions}, issue::{ConsoleUi, LogOptions},
raw_trace::RawTraceLayer, raw_trace::RawTraceLayer,
trace_writer::TraceWriter, trace_writer::TraceWriter,
tracing_presets::{ tracing_presets::TRACING_OVERVIEW_TARGETS,
TRACING_OVERVIEW_TARGETS, TRACING_TURBOPACK_TARGETS, TRACING_TURBO_TASKS_TARGETS,
},
}, },
core::{ core::{
environment::ServerAddr, environment::ServerAddr,
...@@ -463,28 +463,6 @@ pub fn register() { ...@@ -463,28 +463,6 @@ pub fn register() {
include!(concat!(env!("OUT_DIR"), "/register.rs")); include!(concat!(env!("OUT_DIR"), "/register.rs"));
} }
static TRACING_NEXT_TARGETS: Lazy<Vec<&str>> = Lazy::new(|| {
[
&TRACING_OVERVIEW_TARGETS[..],
&[
"next_dev=trace",
"next_core=trace",
"next_font=trace",
"turbopack_node=trace",
],
]
.concat()
});
static TRACING_NEXT_TURBOPACK_TARGETS: Lazy<Vec<&str>> =
Lazy::new(|| [&TRACING_NEXT_TARGETS[..], &TRACING_TURBOPACK_TARGETS[..]].concat());
static TRACING_NEXT_TURBO_TASKS_TARGETS: Lazy<Vec<&str>> = Lazy::new(|| {
[
&TRACING_TURBOPACK_TARGETS[..],
&TRACING_TURBO_TASKS_TARGETS[..],
]
.concat()
});
/// Start a devserver with the given options. /// Start a devserver with the given options.
pub async fn start_server(options: &DevServerOptions) -> Result<()> { pub async fn start_server(options: &DevServerOptions) -> Result<()> {
let start = Instant::now(); let start = Instant::now();
......
...@@ -451,7 +451,7 @@ interface Issue { ...@@ -451,7 +451,7 @@ interface Issue {
interface Diagnostics {} interface Diagnostics {}
type TurbopackResult<T = {}> = T & { export type TurbopackResult<T = {}> = T & {
issues: Issue[] issues: Issue[]
diagnostics: Diagnostics[] diagnostics: Diagnostics[]
} }
...@@ -475,7 +475,7 @@ interface Project { ...@@ -475,7 +475,7 @@ interface Project {
entrypointsSubscribe(): AsyncIterableIterator<TurbopackResult<Entrypoints>> entrypointsSubscribe(): AsyncIterableIterator<TurbopackResult<Entrypoints>>
} }
type Route = export type Route =
| { | {
type: 'conflict' type: 'conflict'
} }
...@@ -498,7 +498,7 @@ type Route = ...@@ -498,7 +498,7 @@ type Route =
endpoint: Endpoint endpoint: Endpoint
} }
interface Endpoint { export interface Endpoint {
/** Write files for the endpoint to disk. */ /** Write files for the endpoint to disk. */
writeToDisk(): Promise<TurbopackResult<WrittenEndpoint>> writeToDisk(): Promise<TurbopackResult<WrittenEndpoint>>
/** /**
...@@ -525,7 +525,7 @@ interface EndpointConfig { ...@@ -525,7 +525,7 @@ interface EndpointConfig {
preferredRegion?: string preferredRegion?: string
} }
type WrittenEndpoint = export type WrittenEndpoint =
| { | {
type: 'nodejs' type: 'nodejs'
/** The entry path for the endpoint. */ /** The entry path for the endpoint. */
...@@ -817,7 +817,28 @@ function bindingToApi(binding: any, _wasm: boolean) { ...@@ -817,7 +817,28 @@ function bindingToApi(binding: any, _wasm: boolean) {
) )
} }
return JSON.stringify(nextConfigSerializable) nextConfigSerializable.modularizeImports =
nextConfigSerializable.modularizeImports
? Object.fromEntries(
Object.entries<any>(nextConfigSerializable.modularizeImports).map(
([mod, config]) => [
mod,
{
...config,
transform:
typeof config.transform === 'string'
? config.transform
: Object.entries(config.transform).map(([key, value]) => [
key,
value,
]),
},
]
)
)
: undefined
return JSON.stringify(nextConfigSerializable, null, 2)
} }
function ensureLoadersHaveSerializableOptions( function ensureLoadersHaveSerializableOptions(
......
...@@ -22,6 +22,11 @@ type DeepMutable<T> = { -readonly [P in keyof T]: DeepMutable<T[P]> } ...@@ -22,6 +22,11 @@ type DeepMutable<T> = { -readonly [P in keyof T]: DeepMutable<T[P]> }
export type ClientBuildManifest = Record<string, string[]> export type ClientBuildManifest = Record<string, string[]>
// Add the runtime ssg manifest file as a lazy-loaded file dependency.
// We also stub this file out for development mode (when it is not
// generated).
export const srcEmptySsgManifest = `self.__SSG_MANIFEST=new Set;self.__SSG_MANIFEST_CB&&self.__SSG_MANIFEST_CB()`
// This function takes the asset map generated in BuildManifestPlugin and creates a // This function takes the asset map generated in BuildManifestPlugin and creates a
// reduced version to send to the client. // reduced version to send to the client.
function generateClientManifest( function generateClientManifest(
...@@ -226,12 +231,8 @@ export default class BuildManifestPlugin { ...@@ -226,12 +231,8 @@ export default class BuildManifestPlugin {
assetMap.lowPriorityFiles.push( assetMap.lowPriorityFiles.push(
`${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_buildManifest.js` `${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_buildManifest.js`
) )
// Add the runtime ssg manifest file as a lazy-loaded file dependency.
// We also stub this file out for development mode (when it is not
// generated).
const srcEmptySsgManifest = `self.__SSG_MANIFEST=new Set;self.__SSG_MANIFEST_CB&&self.__SSG_MANIFEST_CB()`
const ssgManifestPath = `${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_ssgManifest.js` const ssgManifestPath = `${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_ssgManifest.js`
assetMap.lowPriorityFiles.push(ssgManifestPath) assetMap.lowPriorityFiles.push(ssgManifestPath)
assets[ssgManifestPath] = new sources.RawSource(srcEmptySsgManifest) assets[ssgManifestPath] = new sources.RawSource(srcEmptySsgManifest)
} }
......
...@@ -221,90 +221,8 @@ const nextDev: CliCommand = async (argv) => { ...@@ -221,90 +221,8 @@ const nextDev: CliCommand = async (argv) => {
if (args['--experimental-turbo']) { if (args['--experimental-turbo']) {
process.env.EXPERIMENTAL_TURBOPACK = '1' process.env.EXPERIMENTAL_TURBOPACK = '1'
} }
const experimentalTurbo = !!process.env.EXPERIMENTAL_TURBOPACK
if (experimentalTurbo) { if (process.env.TURBOPACK) {
const { loadBindings } =
require('../build/swc') as typeof import('../build/swc')
const { default: loadJsConfig } =
require('../build/load-jsconfig') as typeof import('../build/load-jsconfig')
const { jsConfig } = await loadJsConfig(dir, config)
resetEnv()
let bindings = await loadBindings()
// Just testing code here:
const options = {
projectPath: dir,
rootPath: args['--root'] ?? findRootDir(dir) ?? dir,
nextConfig: config,
jsConfig,
env: {
NEXT_PUBLIC_ENV_VAR: 'world',
},
watch: true,
}
const project = await bindings.turbo.createProject(options)
const iter = project.entrypointsSubscribe()
try {
for await (const entrypoints of iter) {
Log.info(entrypoints)
Log.info(`writing _document to disk`)
Log.info(await entrypoints.pagesDocumentEndpoint.writeToDisk())
Log.info(`writing _app to disk`)
Log.info(await entrypoints.pagesAppEndpoint.writeToDisk())
Log.info(`writing _error to disk`)
Log.info(await entrypoints.pagesErrorEndpoint.writeToDisk())
for (const [pathname, route] of entrypoints.routes) {
switch (route.type) {
case 'page': {
Log.info(`writing ${pathname} to disk`)
const written = await route.htmlEndpoint.writeToDisk()
Log.info(written)
break
}
case 'page-api': {
Log.info(`writing ${pathname} to disk`)
const written = await route.endpoint.writeToDisk()
Log.info(written)
break
}
case 'app-page': {
Log.info(`writing ${pathname} to disk`)
const written = await route.rscEndpoint.writeToDisk()
Log.info(written)
break
}
case 'app-route': {
Log.info(`writing ${pathname} to disk`)
const written = await route.endpoint.writeToDisk()
Log.info(written)
break
}
default:
Log.info(`skipping ${pathname} (${route.type})`)
break
}
}
Log.info('iteration done')
await project.update({
...options,
env: {
NEXT_PUBLIC_ENV_VAR: 'hello',
},
})
}
} catch (e) {
console.dir(e)
}
Log.error('Not supported yet')
process.exit(1)
} else if (process.env.TURBOPACK) {
isTurboSession = true isTurboSession = true
const { validateTurboNextConfig } = const { validateTurboNextConfig } =
......
// TODO: Remove use of `any` type.
import { initialize, hydrate, version, router, emitter } from './'
import { displayContent } from './dev/fouc'
import './setup-hydration-warning'
window.next = {
version: `${version}-turbo`,
// router is initialized later so it has to be live-binded
get router() {
return router
},
emitter,
}
;(self as any).__next_set_public_path__ = () => {}
// for the page loader
declare let __turbopack_load__: any
initialize({
// TODO the prop name is confusing as related to webpack
webpackHMR: {
onUnrecoverableError() {},
},
})
.then(({ assetPrefix }) => {
// for the page loader
async function loadPageChunk(chunkData: any) {
if (typeof chunkData === 'string') {
const fullPath = assetPrefix + chunkData
await __turbopack_load__(fullPath)
} else {
let fullChunkData = {
...chunkData,
path: assetPrefix + chunkData.path,
}
await __turbopack_load__(fullChunkData)
}
}
;(self as any).__turbopack_load_page_chunks__ = (
page: string,
chunksData: any
) => {
const chunkPromises = chunksData.map(loadPageChunk)
Promise.all(chunkPromises).catch((err) =>
console.error('failed to load chunks for page ' + page, err)
)
}
return hydrate({ beforeRender: displayContent }).then(() => {})
})
.catch((err) => {
console.error('Error was not caught', err)
})
...@@ -11,30 +11,7 @@ import { ...@@ -11,30 +11,7 @@ import {
urlQueryToSearchParams, urlQueryToSearchParams,
} from '../shared/lib/router/utils/querystring' } from '../shared/lib/router/utils/querystring'
declare global { import './setup-hydration-warning'
interface Window {
_nextSetupHydrationWarning?: boolean
}
}
if (!window._nextSetupHydrationWarning) {
const origConsoleError = window.console.error
window.console.error = (...args) => {
const isHydrateError = args.some(
(arg) =>
typeof arg === 'string' &&
arg.match(/(hydration|content does not match|did not match)/i)
)
if (isHydrateError) {
args = [
...args,
`\n\nSee more info here: https://nextjs.org/docs/messages/react-hydration-error`,
]
}
origConsoleError.apply(window.console, args)
}
window._nextSetupHydrationWarning = true
}
window.next = { window.next = {
version, version,
......
declare global {
interface Window {
_nextSetupHydrationWarning?: boolean
}
}
if (!window._nextSetupHydrationWarning) {
const origConsoleError = window.console.error
window.console.error = (...args) => {
const isHydrateError = args.some(
(arg) =>
typeof arg === 'string' &&
arg.match(/(hydration|content does not match|did not match)/i)
)
if (isHydrateError) {
args = [
...args,
`\n\nSee more info here: https://nextjs.org/docs/messages/react-hydration-error`,
]
}
origConsoleError.apply(window.console, args)
}
window._nextSetupHydrationWarning = true
}
export {}
...@@ -122,7 +122,7 @@ function addCorsSupport(req: IncomingMessage, res: ServerResponse) { ...@@ -122,7 +122,7 @@ function addCorsSupport(req: IncomingMessage, res: ServerResponse) {
return { preflight: false } return { preflight: false }
} }
const matchNextPageBundleRequest = getPathMatch( export const matchNextPageBundleRequest = getPathMatch(
'/_next/static/chunks/pages/:path*.js(\\.map|)' '/_next/static/chunks/pages/:path*.js(\\.map|)'
) )
......
...@@ -111,6 +111,7 @@ export async function initialize(opts: { ...@@ -111,6 +111,7 @@ export async function initialize(opts: {
dir: opts.dir, dir: opts.dir,
nextConfig: config, nextConfig: config,
isCustomServer: opts.customServer, isCustomServer: opts.customServer,
turbo: !!process.env.EXPERIMENTAL_TURBOPACK,
}) })
} }
......
import type { NextConfigComplete } from '../../config-shared' import type { NextConfigComplete } from '../../config-shared'
import type {
Endpoint,
Route,
TurbopackResult,
WrittenEndpoint,
} from '../../../build/swc'
import fs from 'fs' import fs from 'fs'
import url from 'url' import url from 'url'
...@@ -10,7 +16,7 @@ import isError from '../../../lib/is-error' ...@@ -10,7 +16,7 @@ import isError from '../../../lib/is-error'
import findUp from 'next/dist/compiled/find-up' import findUp from 'next/dist/compiled/find-up'
import { buildCustomRoute } from './filesystem' import { buildCustomRoute } from './filesystem'
import * as Log from '../../../build/output/log' import * as Log from '../../../build/output/log'
import HotReloader from '../../dev/hot-reloader' import HotReloader, { matchNextPageBundleRequest } from '../../dev/hot-reloader'
import { traceGlobals } from '../../../trace/shared' import { traceGlobals } from '../../../trace/shared'
import { Telemetry } from '../../../telemetry/storage' import { Telemetry } from '../../../telemetry/storage'
import { IncomingMessage, ServerResponse } from 'http' import { IncomingMessage, ServerResponse } from 'http'
...@@ -35,10 +41,15 @@ import { absolutePathToPage } from '../../../shared/lib/page-path/absolute-path- ...@@ -35,10 +41,15 @@ import { absolutePathToPage } from '../../../shared/lib/page-path/absolute-path-
import { generateInterceptionRoutesRewrites } from '../../../lib/generate-interception-routes-rewrites' import { generateInterceptionRoutesRewrites } from '../../../lib/generate-interception-routes-rewrites'
import { import {
APP_BUILD_MANIFEST,
APP_PATHS_MANIFEST,
BUILD_MANIFEST,
CLIENT_STATIC_FILES_PATH, CLIENT_STATIC_FILES_PATH,
COMPILER_NAMES, COMPILER_NAMES,
DEV_CLIENT_PAGES_MANIFEST, DEV_CLIENT_PAGES_MANIFEST,
DEV_MIDDLEWARE_MANIFEST, DEV_MIDDLEWARE_MANIFEST,
NEXT_FONT_MANIFEST,
PAGES_MANIFEST,
PHASE_DEVELOPMENT_SERVER, PHASE_DEVELOPMENT_SERVER,
} from '../../../shared/lib/constants' } from '../../../shared/lib/constants'
...@@ -61,9 +72,16 @@ import { ...@@ -61,9 +72,16 @@ import {
getSourceById, getSourceById,
parseStack, parseStack,
} from 'next/dist/compiled/@next/react-dev-overlay/dist/middleware' } from 'next/dist/compiled/@next/react-dev-overlay/dist/middleware'
import { BuildManifest } from '../../get-page-files'
import { mkdir, readFile, writeFile } from 'fs/promises'
import { PagesManifest } from '../../../build/webpack/plugins/pages-manifest-plugin'
import { AppBuildManifest } from '../../../build/webpack/plugins/app-build-manifest-plugin'
import { PageNotFoundError } from '../../../shared/lib/utils'
import { srcEmptySsgManifest } from '../../../build/webpack/plugins/build-manifest-plugin'
type SetupOpts = { type SetupOpts = {
dir: string dir: string
turbo?: boolean
appDir?: string appDir?: string
pagesDir?: string pagesDir?: string
telemetry: Telemetry telemetry: Telemetry
...@@ -108,7 +126,509 @@ async function startWatcher(opts: SetupOpts) { ...@@ -108,7 +126,509 @@ async function startWatcher(opts: SetupOpts) {
appDir appDir
) )
const hotReloader = new HotReloader(opts.dir, { const renderWorkers: {
app?: import('../router-server').RenderWorker
pages?: import('../router-server').RenderWorker
} = {}
async function propagateToWorkers(field: string, args: any) {
await renderWorkers.app?.propagateServerField(field, args)
await renderWorkers.pages?.propagateServerField(field, args)
}
let hotReloader: InstanceType<typeof HotReloader>
if (opts.turbo) {
const { loadBindings } =
require('../../../build/swc') as typeof import('../../../build/swc')
let bindings = await loadBindings()
const { jsConfig } = await loadJsConfig(dir, opts.nextConfig)
const project = await bindings.turbo.createProject({
projectPath: dir,
rootPath: opts.nextConfig.experimental.outputFileTracingRoot || dir,
nextConfig: opts.nextConfig,
jsConfig,
watch: true,
env: process.env as Record<string, string>,
})
const iter = project.entrypointsSubscribe()
const curEntries: Map<string, Route> = new Map()
const globalEntries: {
app: Endpoint | undefined
document: Endpoint | undefined
error: Endpoint | undefined
} = {
app: undefined,
document: undefined,
error: undefined,
}
try {
async function handleEntries() {
for await (const entrypoints of iter) {
globalEntries.app = entrypoints.pagesAppEndpoint
globalEntries.document = entrypoints.pagesDocumentEndpoint
globalEntries.error = entrypoints.pagesErrorEndpoint
curEntries.clear()
for (const [pathname, route] of entrypoints.routes) {
switch (route.type) {
case 'page':
case 'page-api':
case 'app-page':
case 'app-route': {
curEntries.set(pathname, route)
break
}
default:
Log.info(`skipping ${pathname} (${route.type})`)
break
}
}
}
}
handleEntries().catch((err) => {
console.error(err)
process.exit(1)
})
} catch (e) {
console.error(e)
}
const buildManifests = new Map<string, BuildManifest>()
const appBuildManifests = new Map<string, AppBuildManifest>()
const pagesManifests = new Map<string, PagesManifest>()
const appPathsManifests = new Map<string, PagesManifest>()
function mergeBuildManifests(manifests: Iterable<BuildManifest>) {
const manifest: Partial<BuildManifest> & Pick<BuildManifest, 'pages'> = {
pages: {
'/_app': [],
},
// Something in next.js depends on these to exist even for app dir rendering
devFiles: [],
ampDevFiles: [],
polyfillFiles: [],
lowPriorityFiles: [
'static/development/_ssgManifest.js',
'static/development/_buildManifest.js',
],
rootMainFiles: [],
ampFirstPages: [],
}
for (const m of manifests) {
Object.assign(manifest.pages, m.pages)
if (m.rootMainFiles.length) manifest.rootMainFiles = m.rootMainFiles
}
return manifest
}
function mergeAppBuildManifests(manifests: Iterable<AppBuildManifest>) {
const manifest: AppBuildManifest = {
pages: {},
}
for (const m of manifests) {
Object.assign(manifest.pages, m.pages)
}
return manifest
}
function mergePagesManifests(manifests: Iterable<PagesManifest>) {
const manifest: PagesManifest = {}
for (const m of manifests) {
Object.assign(manifest, m)
}
return manifest
}
function printIssues(
result: TurbopackResult<WrittenEndpoint> | undefined
): TurbopackResult<WrittenEndpoint> | undefined {
if (result) {
for (const issue of result.issues) {
// TODO better formatting
if (issue.severity !== 'error' && issue.severity !== 'fatal') continue
console.error(
`⚠ ${issue.severity} - ${issue.context}\n${issue.title}\n${issue.description}\n\n`
)
}
}
return result
}
const clearCache = (filePath: string) =>
(global as any)._nextDeleteCache?.([filePath])
async function writeBuildManifest(): Promise<void> {
const buildManifest = mergeBuildManifests(buildManifests.values())
const buildManifestPath = path.join(distDir, 'build-manifest.json')
await clearCache(buildManifestPath)
await writeFile(
buildManifestPath,
JSON.stringify(buildManifest, null, 2),
'utf-8'
)
const content = {
__rewrites: { afterFiles: [], beforeFiles: [], fallback: [] },
...Object.fromEntries(
[...curEntries.keys()].map((pathname) => [
pathname,
`static/chunks/pages${pathname === '/' ? '/index' : pathname}.js`,
])
),
sortedPages: [...curEntries.keys()],
}
const buildManifestJs = `self.__BUILD_MANIFEST = ${JSON.stringify(
content
)};self.__BUILD_MANIFEST_CB && self.__BUILD_MANIFEST_CB()`
await writeFile(
path.join(distDir, 'static', 'development', '_buildManifest.js'),
buildManifestJs,
'utf-8'
)
await writeFile(
path.join(distDir, 'static', 'development', '_ssgManifest.js'),
srcEmptySsgManifest,
'utf-8'
)
}
async function writeAppBuildManifest(): Promise<void> {
const appBuildManifest = mergeAppBuildManifests(
appBuildManifests.values()
)
const appBuildManifestPath = path.join(distDir, APP_BUILD_MANIFEST)
await clearCache(appBuildManifestPath)
await writeFile(
appBuildManifestPath,
JSON.stringify(appBuildManifest, null, 2),
'utf-8'
)
}
async function writePagesManifest(): Promise<void> {
const pagesManifest = mergePagesManifests(pagesManifests.values())
const pagesManifestPath = path.join(distDir, 'server', PAGES_MANIFEST)
await clearCache(pagesManifestPath)
await writeFile(
pagesManifestPath,
JSON.stringify(pagesManifest, null, 2),
'utf-8'
)
}
async function writeAppPathsManifest(): Promise<void> {
const appPathsManifest = mergePagesManifests(appPathsManifests.values())
const appPathsManifestPath = path.join(
distDir,
'server',
APP_PATHS_MANIFEST
)
await clearCache(appPathsManifestPath)
await writeFile(
appPathsManifestPath,
JSON.stringify(appPathsManifest, null, 2),
'utf-8'
)
}
async function writeMiddlewareManifest(): Promise<void> {
const middlewareManifestPath = path.join(
distDir,
'server/middleware-manifest.json'
)
await clearCache(middlewareManifestPath)
await writeFile(
middlewareManifestPath,
JSON.stringify(
{
sortedMiddleware: [],
middleware: {},
functions: {},
version: 2,
},
null,
2
),
'utf-8'
)
}
async function writeOtherManifests(): Promise<void> {
const loadableManifestPath = path.join(
distDir,
'react-loadable-manifest.json'
)
await clearCache(loadableManifestPath)
await writeFile(
loadableManifestPath,
JSON.stringify({}, null, 2),
'utf-8'
)
// TODO: turbopack should write the correct
// version of this
const fontManifestPath = path.join(
distDir,
'server',
NEXT_FONT_MANIFEST + '.json'
)
await clearCache(fontManifestPath)
await writeFile(
fontManifestPath,
JSON.stringify(
{
pages: {},
app: {},
appUsingSizeAdjust: false,
pagesUsingSizeAdjust: false,
},
null,
2
)
)
}
// Write empty manifests
await mkdir(path.join(distDir, 'server'), { recursive: true })
await mkdir(path.join(distDir, 'static/development'), { recursive: true })
await writeFile(
path.join(distDir, 'package.json'),
JSON.stringify(
{
type: 'commonjs',
},
null,
2
)
)
await writeBuildManifest()
await writeAppBuildManifest()
await writePagesManifest()
await writeAppPathsManifest()
await writeMiddlewareManifest()
await writeOtherManifests()
hotReloader = new Proxy({} as any, {
get(_target, prop, _receiver) {
if (prop === 'ensurePage') {
return async (
ensureOpts: Parameters<(typeof hotReloader)['ensurePage']>[0]
) => {
let page = ensureOpts.match?.definition?.pathname ?? ensureOpts.page
async function loadPartialManifest<T>(
name: string,
pageName: string,
isApp: boolean = false,
isRoute: boolean = false
): Promise<T> {
const manifestPath = path.posix.join(
distDir,
`server`,
isApp ? 'app' : 'pages',
pageName === '/' && !isApp ? 'index' : pageName,
isApp && pageName !== '/_not-found' && pageName !== '/not-found'
? isRoute
? 'route'
: 'page'
: '',
name
)
return JSON.parse(
await readFile(path.posix.join(manifestPath), 'utf-8')
) as T
}
async function loadBuildManifest(
pageName: string,
isApp: boolean = false
): Promise<void> {
buildManifests.set(
pageName,
await loadPartialManifest(BUILD_MANIFEST, pageName, isApp)
)
}
async function loadAppBuildManifest(
pageName: string
): Promise<void> {
appBuildManifests.set(
pageName,
await loadPartialManifest(APP_BUILD_MANIFEST, pageName, true)
)
}
async function loadPagesManifest(pageName: string): Promise<void> {
pagesManifests.set(
pageName,
await loadPartialManifest(PAGES_MANIFEST, pageName)
)
}
async function loadAppPathManifest(
pageName: string,
routeHandler: boolean
): Promise<void> {
appPathsManifests.set(
pageName,
await loadPartialManifest(
APP_PATHS_MANIFEST,
pageName,
true,
routeHandler
)
)
}
if (page === '/_error') {
printIssues(await globalEntries.app?.writeToDisk())
await loadBuildManifest('_app')
await loadPagesManifest('_app')
printIssues(await globalEntries.document?.writeToDisk())
await loadPagesManifest('_document')
printIssues(await globalEntries.error?.writeToDisk())
await loadBuildManifest('_error')
await loadPagesManifest('_error')
await writeBuildManifest()
await writePagesManifest()
await writeMiddlewareManifest()
await writeOtherManifests()
return
}
const route = curEntries.get(page)
if (!route) {
// TODO: why is this entry missing in turbopack?
if (page === '/_app') return
if (page === '/_document') return
throw new PageNotFoundError(`route not found ${page}`)
}
switch (route.type) {
case 'page':
case 'page-api': {
if (ensureOpts.isApp) {
throw new Error(
`mis-matched route type: isApp && page for ${page}`
)
}
printIssues(await globalEntries.app?.writeToDisk())
await loadBuildManifest('_app')
await loadPagesManifest('_app')
printIssues(await globalEntries.document?.writeToDisk())
await loadPagesManifest('_document')
const writtenEndpoint =
route.type === 'page-api'
? printIssues(await route.endpoint.writeToDisk())
: printIssues(await route.htmlEndpoint.writeToDisk())
if (route.type === 'page') {
await loadBuildManifest(page)
}
await loadPagesManifest(page)
switch (writtenEndpoint!.type) {
case 'nodejs': {
break
}
case 'edge': {
throw new Error('edge is not implemented yet')
}
default: {
throw new Error(
`unknown endpoint type ${(writtenEndpoint as any).type}`
)
}
}
await writeBuildManifest()
await writePagesManifest()
await writeMiddlewareManifest()
await writeOtherManifests()
break
}
case 'app-page': {
printIssues(await route.htmlEndpoint.writeToDisk())
await loadAppBuildManifest(page)
await loadBuildManifest(page, true)
await loadAppPathManifest(page, false)
await writeAppBuildManifest()
await writeBuildManifest()
await writeAppPathsManifest()
await writeMiddlewareManifest()
await writeOtherManifests()
break
}
case 'app-route': {
printIssues(await route.endpoint.writeToDisk())
await loadAppPathManifest(page, true)
await writeAppBuildManifest()
await writeAppPathsManifest()
await writeMiddlewareManifest()
await writeOtherManifests()
break
}
default: {
throw new Error(`unknown route type ${route.type} for ${page}`)
}
}
}
}
if (prop === 'run') {
return async (req: IncomingMessage, _res: ServerResponse) => {
// intercept page chunks request and ensure them with turbopack
if (req.url?.startsWith('/_next/static/chunks/pages/')) {
const params = matchNextPageBundleRequest(req.url)
if (params) {
const decodedPagePath = `/${params.path
.map((param: string) => decodeURIComponent(param))
.join('/')}`
await hotReloader
.ensurePage({
page: decodedPagePath,
clientOnly: false,
})
.catch(console.error)
}
}
return { finished: false }
}
}
if (prop === 'activeConfigs') {
return []
}
return () => {}
},
set() {
return true
},
})
} else {
hotReloader = new HotReloader(opts.dir, {
appDir, appDir,
pagesDir, pagesDir,
distDir: distDir, distDir: distDir,
...@@ -118,10 +638,7 @@ async function startWatcher(opts: SetupOpts) { ...@@ -118,10 +638,7 @@ async function startWatcher(opts: SetupOpts) {
rewrites: opts.fsChecker.rewrites, rewrites: opts.fsChecker.rewrites,
previewProps: opts.fsChecker.prerenderManifest.preview, previewProps: opts.fsChecker.prerenderManifest.preview,
}) })
const renderWorkers: { }
app?: import('../router-server').RenderWorker
pages?: import('../router-server').RenderWorker
} = {}
await hotReloader.start() await hotReloader.start()
...@@ -162,11 +679,6 @@ async function startWatcher(opts: SetupOpts) { ...@@ -162,11 +679,6 @@ async function startWatcher(opts: SetupOpts) {
>[] >[]
} = {} } = {}
async function propagateToWorkers(field: string, args: any) {
await renderWorkers.app?.propagateServerField(field, args)
await renderWorkers.pages?.propagateServerField(field, args)
}
await new Promise<void>(async (resolve, reject) => { await new Promise<void>(async (resolve, reject) => {
if (pagesDir) { if (pagesDir) {
// Watchpack doesn't emit an event for an empty directory // Watchpack doesn't emit an event for an empty directory
...@@ -626,6 +1138,7 @@ async function startWatcher(opts: SetupOpts) { ...@@ -626,6 +1138,7 @@ async function startWatcher(opts: SetupOpts) {
) || [] ) || []
const exportPathMap = const exportPathMap =
(typeof nextConfig.exportPathMap === 'function' &&
(await nextConfig.exportPathMap?.( (await nextConfig.exportPathMap?.(
{}, {},
{ {
...@@ -635,9 +1148,10 @@ async function startWatcher(opts: SetupOpts) { ...@@ -635,9 +1148,10 @@ async function startWatcher(opts: SetupOpts) {
distDir: distDir, distDir: distDir,
buildId: 'development', buildId: 'development',
} }
)) || {} ))) ||
{}
for (const [key, value] of Object.entries(exportPathMap)) { for (const [key, value] of Object.entries(exportPathMap || {})) {
opts.fsChecker.interceptionRoutes.push( opts.fsChecker.interceptionRoutes.push(
buildCustomRoute( buildCustomRoute(
'before_files_rewrite', 'before_files_rewrite',
......
...@@ -86,6 +86,7 @@ export const createRouterWorker = async ( ...@@ -86,6 +86,7 @@ export const createRouterWorker = async (
? { __NEXT_PRIVATE_CPU_PROFILE: `CPU.router` } ? { __NEXT_PRIVATE_CPU_PROFILE: `CPU.router` }
: {}), : {}),
WATCHPACK_WATCHER_LIMIT: '20', WATCHPACK_WATCHER_LIMIT: '20',
EXPERIMENTAL_TURBOPACK: process.env.EXPERIMENTAL_TURBOPACK,
}, },
}, },
exposedMethods: ['initialize'], exposedMethods: ['initialize'],
......
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