Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
arlyon committed Nov 5, 2024
1 parent b55841d commit 38b9d33
Show file tree
Hide file tree
Showing 19 changed files with 572 additions and 311 deletions.
108 changes: 46 additions & 62 deletions crates/next-api/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ use next_core::{
use serde::{Deserialize, Serialize};
use tracing::Instrument;
use turbo_tasks::{
fxindexset, trace::TraceRawVcs, Completion, FxIndexMap, FxIndexSet, RcStr, ResolvedVc,
TryJoinIterExt, Value, ValueToString, Vc,
fxindexmap, fxindexset, trace::TraceRawVcs, Completion, FxIndexMap, FxIndexSet, RcStr,
ResolvedVc, TryJoinIterExt, Value, ValueToString, Vc,
};
use turbo_tasks_env::{CustomProcessEnv, ProcessEnv};
use turbo_tasks_fs::{File, FileContent, FileSystemPath};
Expand Down Expand Up @@ -75,7 +75,7 @@ use crate::{
font::create_font_manifest,
loadable_manifest::create_react_loadable_manifest,
paths::{
all_paths_in_root, all_server_paths, get_js_paths_from_root, get_paths_from_root,
all_paths_in_root, all_server_paths, get_asset_paths_from_root, get_js_paths_from_root,
get_wasm_paths_from_root, paths_to_bindings, wasm_paths_to_bindings,
},
project::Project,
Expand Down Expand Up @@ -686,7 +686,7 @@ pub fn app_entry_point_to_route(
}

#[turbo_tasks::function]
fn client_shared_chunks() -> Vc<RcStr> {
fn client_shared_chunks_mod() -> Vc<RcStr> {
Vc::cell("client_shared_chunks".into())
}

Expand Down Expand Up @@ -830,7 +830,6 @@ impl AppEndpoint {
let node_root = this.app_project.project().node_root();

let client_relative_path = this.app_project.project().client_relative_path();
let client_relative_path_ref = client_relative_path.await?;

let server_path = node_root.join("server".into());

Expand Down Expand Up @@ -867,21 +866,19 @@ impl AppEndpoint {
) = if process_client_components {
let client_shared_chunk_group = get_app_client_shared_chunk_group(
AssetIdent::from_path(this.app_project.project().project_path())
.with_modifier(client_shared_chunks()),
.with_modifier(client_shared_chunks_mod()),
this.app_project.client_runtime_entries(),
client_chunking_context,
)
.await?;

let mut client_shared_chunks_paths = vec![];
let mut client_shared_chunks = vec![];
for chunk in client_shared_chunk_group.assets.await?.iter().copied() {
client_assets.insert(chunk);

let chunk_path = chunk.ident().path().await?;
if chunk_path.extension_ref() == Some("js") {
if let Some(chunk_path) = client_relative_path_ref.get_path_to(&chunk_path) {
client_shared_chunks_paths.push(chunk_path.into());
}
client_shared_chunks.push(chunk);
}
}
let client_shared_availability_info = client_shared_chunk_group.availability_info;
Expand Down Expand Up @@ -973,39 +970,28 @@ impl AppEndpoint {
client_assets.extend(entry_client_chunks.iter().copied());
server_assets.extend(entry_ssr_chunks.iter().copied());

let entry_client_chunks_paths = entry_client_chunks
.iter()
.map(|chunk| chunk.ident().path())
.try_join()
.await?;
let mut entry_client_chunks_paths = entry_client_chunks_paths
.iter()
.map(|path| {
Ok(client_relative_path_ref
.get_path_to(path)
.context("asset path should be inside client root")?
.into())
})
.collect::<anyhow::Result<Vec<_>>>()?;
entry_client_chunks_paths.extend(client_shared_chunks_paths.iter().cloned());

let app_build_manifest = AppBuildManifest {
pages: [(app_entry.original_name.clone(), entry_client_chunks_paths)]
.into_iter()
.collect(),
pages: fxindexmap!(
app_entry.original_name.clone() => Vc::cell(entry_client_chunks
.iter()
.chain(client_shared_chunks.iter())
.copied()
.collect())
),
};
let manifest_path_prefix = &app_entry.original_name;
let app_build_manifest_output = VirtualOutputAsset::new(
node_root.join(
format!("server/app{manifest_path_prefix}/app-build-manifest.json",).into(),
),
AssetContent::file(
File::from(serde_json::to_string_pretty(&app_build_manifest)?).into(),
),
)
.to_resolved()
.await?;
server_assets.insert(ResolvedVc::upcast(app_build_manifest_output));
let app_build_manifest_output = app_build_manifest
.build_output(
node_root.join(
format!("server/app{manifest_path_prefix}/app-build-manifest.json",).into(),
),
client_relative_path,
)
.await?
.to_resolved()
.await?;

server_assets.insert(app_build_manifest_output);

// polyfill-nomodule.js is a pre-compiled asset distributed as part of next,
// load it as a RawModule.
Expand All @@ -1015,16 +1001,12 @@ impl AppEndpoint {
);
let polyfill_output_path =
client_chunking_context.chunk_path(polyfill_source.ident(), ".js".into());
let polyfill_output_asset =
let polyfill_output_asset = ResolvedVc::upcast(
RawOutput::new(polyfill_output_path, Vc::upcast(polyfill_source))
.to_resolved()
.await?;
let polyfill_client_path = client_relative_path_ref
.get_path_to(&*polyfill_output_path.await?)
.context("failed to resolve client-relative path to polyfill")?
.into();
let polyfill_client_paths = vec![polyfill_client_path];
client_assets.insert(ResolvedVc::upcast(polyfill_output_asset));
.await?,
);
client_assets.insert(polyfill_output_asset);

if *this
.app_project
Expand All @@ -1048,20 +1030,23 @@ impl AppEndpoint {
}

let build_manifest = BuildManifest {
root_main_files: client_shared_chunks_paths,
polyfill_files: polyfill_client_paths,
root_main_files: client_shared_chunks,
polyfill_files: vec![polyfill_output_asset],
..Default::default()
};
let build_manifest_output = VirtualOutputAsset::new(
node_root
.join(format!("server/app{manifest_path_prefix}/build-manifest.json",).into()),
AssetContent::file(
File::from(serde_json::to_string_pretty(&build_manifest)?).into(),
),
)
.to_resolved()
.await?;
server_assets.insert(ResolvedVc::upcast(build_manifest_output));
let build_manifest_output = ResolvedVc::upcast(
build_manifest
.build_output(
node_root.join(
format!("server/app{manifest_path_prefix}/build-manifest.json",).into(),
),
client_relative_path,
)
.await?
.to_resolved()
.await?,
);
server_assets.insert(build_manifest_output);

if runtime == NextRuntime::Edge {
// as the edge runtime doesn't support chunk loading we need to add all client
Expand Down Expand Up @@ -1200,8 +1185,7 @@ impl AppEndpoint {
.extend(get_wasm_paths_from_root(&node_root_value, &all_output_assets).await?);

let all_assets =
get_paths_from_root(&node_root_value, &all_output_assets, |_asset| true)
.await?;
get_asset_paths_from_root(&node_root_value, &all_output_assets).await?;

let entry_file = "app-edge-has-no-entrypoint".into();

Expand Down
27 changes: 26 additions & 1 deletion crates/next-api/src/instrumentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use next_core::{
use tracing::Instrument;
use turbo_tasks::{Completion, RcStr, ResolvedVc, Value, Vc};
use turbo_tasks_fs::{File, FileContent, FileSystemPath};
use turbopack::nft_json::NftJsonAsset;
use turbopack_core::{
asset::AssetContent,
chunk::{
Expand Down Expand Up @@ -92,6 +93,15 @@ impl InstrumentationEndpoint {
.cell())
}

#[turbo_tasks::function]
async fn entry_module(self: Vc<Self>) -> Result<Vc<Box<dyn Module>>> {
if self.await?.is_edge {
Ok(*self.core_modules().await?.edge_entry_module)
} else {
Ok(*self.core_modules().await?.userland_module)
}
}

#[turbo_tasks::function]
async fn edge_files(self: Vc<Self>) -> Result<Vc<OutputAssets>> {
let this = self.await?;
Expand Down Expand Up @@ -210,7 +220,22 @@ impl InstrumentationEndpoint {

Ok(Vc::cell(output_assets))
} else {
Ok(Vc::cell(vec![self.node_chunk().to_resolved().await?]))
let chunk = self.node_chunk().to_resolved().await?;
let mut output_assets = vec![chunk];
if this.project.next_mode().await?.is_production() {
output_assets.push(ResolvedVc::upcast(
NftJsonAsset::new(
*chunk,
this.project.output_fs(),
this.project.project_fs(),
this.project.client_fs(),
vec![],
)
.to_resolved()
.await?,
));
}
Ok(Vc::cell(output_assets))
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions crates/next-api/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use turbopack_ecmascript::chunk::EcmascriptChunkPlaceable;

use crate::{
paths::{
all_paths_in_root, all_server_paths, get_js_paths_from_root, get_paths_from_root,
all_paths_in_root, all_server_paths, get_asset_paths_from_root, get_js_paths_from_root,
get_wasm_paths_from_root, paths_to_bindings, wasm_paths_to_bindings,
},
project::Project,
Expand Down Expand Up @@ -140,8 +140,7 @@ impl MiddlewareEndpoint {
let wasm_paths_from_root =
get_wasm_paths_from_root(&node_root_value, &all_output_assets).await?;

let all_assets =
get_paths_from_root(&node_root_value, &all_output_assets, |_asset| true).await?;
let all_assets = get_asset_paths_from_root(&node_root_value, &all_output_assets).await?;

// Awaited later for parallelism
let config = config.await?;
Expand Down
46 changes: 14 additions & 32 deletions crates/next-api/src/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ use next_core::{
use serde::{Deserialize, Serialize};
use tracing::Instrument;
use turbo_tasks::{
trace::TraceRawVcs, Completion, FxIndexMap, RcStr, ResolvedVc, TaskInput, TryJoinIterExt,
Value, Vc,
fxindexmap, trace::TraceRawVcs, Completion, FxIndexMap, RcStr, ResolvedVc, TaskInput, Value, Vc,
};
use turbo_tasks_fs::{
self, File, FileContent, FileSystem, FileSystemPath, FileSystemPathOption, VirtualFileSystem,
Expand Down Expand Up @@ -68,7 +67,7 @@ use crate::{
font::create_font_manifest,
loadable_manifest::create_react_loadable_manifest,
paths::{
all_paths_in_root, all_server_paths, get_js_paths_from_root, get_paths_from_root,
all_paths_in_root, all_server_paths, get_asset_paths_from_root, get_js_paths_from_root,
get_wasm_paths_from_root, paths_to_bindings, wasm_paths_to_bindings,
},
project::Project,
Expand Down Expand Up @@ -1015,37 +1014,21 @@ impl PageEndpoint {
) -> Result<Vc<Box<dyn OutputAsset>>> {
let node_root = self.pages_project.project().node_root();
let client_relative_path = self.pages_project.project().client_relative_path();
let client_relative_path_ref = client_relative_path.await?;
let build_manifest = BuildManifest {
pages: [(
self.pathname.await?.clone_value(),
client_chunks
.await?
.iter()
.copied()
.map(|chunk| {
let client_relative_path_ref = client_relative_path_ref.clone();
async move {
let chunk_path = chunk.ident().path().await?;
Ok(client_relative_path_ref
.get_path_to(&chunk_path)
.context("client chunk entry path must be inside the client root")?
.into())
}
})
.try_join()
.await?,
)]
.into_iter()
.collect(),
pages: fxindexmap!(self.pathname.await?.clone_value() => client_chunks),
..Default::default()
};
let manifest_path_prefix = get_asset_prefix_from_pathname(&self.pathname.await?);
Ok(Vc::upcast(VirtualOutputAsset::new(
node_root
.join(format!("server/pages{manifest_path_prefix}/build-manifest.json",).into()),
AssetContent::file(File::from(serde_json::to_string_pretty(&build_manifest)?).into()),
)))
Ok(Vc::upcast(
build_manifest
.build_output(
node_root.join(
format!("server/pages{manifest_path_prefix}/build-manifest.json",).into(),
),
client_relative_path,
)
.await?,
))
}

#[turbo_tasks::function]
Expand Down Expand Up @@ -1172,8 +1155,7 @@ impl PageEndpoint {
.extend(get_wasm_paths_from_root(&node_root_value, &all_output_assets).await?);

let all_assets =
get_paths_from_root(&node_root_value, &all_output_assets, |_asset| true)
.await?;
get_asset_paths_from_root(&node_root_value, &all_output_assets).await?;

let named_regex = get_named_middleware_regex(&pathname).into();
let matchers = MiddlewareMatcher {
Expand Down
10 changes: 10 additions & 0 deletions crates/next-api/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ pub(crate) async fn get_wasm_paths_from_root(
get_paths_from_root(root, output_assets, |path| path.ends_with(".wasm")).await
}

pub(crate) async fn get_asset_paths_from_root(
root: &FileSystemPath,
output_assets: &[ResolvedVc<Box<dyn OutputAsset>>],
) -> Result<Vec<RcStr>> {
get_paths_from_root(root, output_assets, |path| {
!path.ends_with(".js") && !path.ends_with(".map") && !path.ends_with(".wasm")
})
.await
}

pub(crate) async fn get_font_paths_from_root(
root: &FileSystemPath,
output_assets: &[ResolvedVc<Box<dyn OutputAsset>>],
Expand Down
4 changes: 3 additions & 1 deletion crates/next-api/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ impl Project {

#[turbo_tasks::function]
pub fn client_fs(self: Vc<Self>) -> Vc<Box<dyn FileSystem>> {
let virtual_fs = VirtualFileSystem::new();
let virtual_fs = VirtualFileSystem::new_with_name("client-fs".into());
Vc::upcast(virtual_fs)
}

Expand Down Expand Up @@ -666,6 +666,8 @@ impl Project {
Ok(get_server_compile_time_info(
self.env(),
this.define_env.nodejs(),
// `/ROOT` corresponds to `[project]/`, so we need exactly the `path` part.
format!("/ROOT/{}", self.project_path().await?.path).into(),
))
}

Expand Down
Loading

0 comments on commit 38b9d33

Please sign in to comment.