Skip to content

Commit

Permalink
NextServerUtilityTransition to remove hack
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic committed Dec 4, 2024
1 parent cc5e7d3 commit 61c8e09
Show file tree
Hide file tree
Showing 8 changed files with 320 additions and 54 deletions.
22 changes: 21 additions & 1 deletion crates/next-api/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ use next_core::{
get_client_module_options_context, get_client_resolve_options_context,
get_client_runtime_entries, ClientContextType, RuntimeEntries,
},
next_client_reference::{ClientReferenceGraphResult, NextEcmascriptClientReferenceTransition},
next_client_reference::{
find_server_entries, ClientReferenceGraphResult, NextEcmascriptClientReferenceTransition,

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / rustdoc check / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / stable - x86_64-unknown-linux-gnu - node@16

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / build / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / rust check / build

unused imports: `ServerEntries` and `find_server_entries`

Check failure on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / rust check / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test devlow package / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test unit (18) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test unit (20) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / build-native / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test cargo unit / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test next-swc wasm / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / Test new tests for flakes (dev) (1/4) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / Test new tests for flakes (prod) (1/4) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / types and precompiled / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / Test new tests for flakes (dev) (3/4) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / Test new tests for flakes (prod) (3/4) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / Test new tests for flakes (dev) (2/4) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / Test new tests for flakes (prod) (2/4) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / Test new tests for flakes (dev) (4/4) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test cargo benches / Test

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test cargo benches / Test

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test ppr integration / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / Test new tests for flakes (prod) (4/4) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test turbopack dev (3/5) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test turbopack development integration (5/5) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / lint / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test turbopack dev (4/5) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test turbopack development integration (4/5) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test turbopack dev (1/5) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / Run devlow benchmarks (--turbopack=true, --scenario=heavy-npm-deps-dev --page=homepage) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / Run devlow benchmarks (--turbopack=false, --scenario=heavy-npm-deps-build-turbo-cache-enabled --p... / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / Run devlow benchmarks (--turbopack=true, --scenario=heavy-npm-deps-build-turbo-cache-enabled --pa... / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test turbopack development integration (1/5) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test integration (10/12) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test turbopack development integration (2/5) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test firefox and safari / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test integration (11/12) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test integration (3/12) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test integration (1/12) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test integration (6/12) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test integration (9/12) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test integration (5/12) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test integration (7/12) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / Run devlow benchmarks (--turbopack=false, --scenario=heavy-npm-deps-build --page=homepage) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test integration (12/12) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test turbopack dev (5/5) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test turbopack production integration (4/5) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test ppr dev (3/4) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test integration (8/12) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test integration (2/12) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test turbopack production (4/5) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test ppr dev (2/4) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test turbopack dev (2/5) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test turbopack production integration (1/5) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test turbopack production integration (2/5) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test ppr dev (4/4) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test integration (4/12) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test ppr dev (1/4) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test dev (1/4) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test turbopack production integration (5/5) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / Run devlow benchmarks (--turbopack=true, --scenario=heavy-npm-deps-build --page=homepage) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test dev (4/4) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test turbopack production (5/5) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test turbopack production (3/5) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test turbopack production (1/5) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test ppr prod (1/4) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test dev (2/4) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test turbopack production integration (3/5) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test turbopack production (2/5) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test prod (3/5) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test turbopack development integration (3/5) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test ppr prod (4/4) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test prod (1/5) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test prod (4/5) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test prod (2/5) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test prod (5/5) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test ppr prod (3/4) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / Test new tests when deployed (3/4) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / Test new tests when deployed (4/4) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / Test new tests when deployed (2/4) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / Test new tests when deployed (1/4) / build

unused imports: `ServerEntries` and `find_server_entries`

Check warning on line 22 in crates/next-api/src/app.rs

View workflow job for this annotation

GitHub Actions / test ppr prod (2/4) / build

unused imports: `ServerEntries` and `find_server_entries`
ServerEntries,
},
next_config::NextConfig,
next_dynamic::NextDynamicTransition,
next_edge::route_regex::get_named_middleware_regex,
Expand All @@ -30,6 +33,7 @@ use next_core::{
get_server_module_options_context, get_server_resolve_options_context,
get_server_runtime_entries, ServerContextType,
},
next_server_utility::NextServerUtilityTransition,
parse_segment_config_from_source,
util::NextRuntime,
};
Expand Down Expand Up @@ -334,6 +338,10 @@ impl AppProject {
"next-shared".into(),
ResolvedVc::upcast(self.shared_transition().to_resolved().await?),
),
(
"next-server-utility".into(),
ResolvedVc::upcast(NextServerUtilityTransition::new().to_resolved().await?),
),
]
.into_iter()
.collect();
Expand Down Expand Up @@ -379,6 +387,10 @@ impl AppProject {
"next-shared".into(),
ResolvedVc::upcast(self.edge_shared_transition().to_resolved().await?),
),
(
"next-server-utility".into(),
ResolvedVc::upcast(NextServerUtilityTransition::new().to_resolved().await?),
),
]
.into_iter()
.collect();
Expand Down Expand Up @@ -422,6 +434,10 @@ impl AppProject {
"next-shared".into(),
ResolvedVc::upcast(self.shared_transition().to_resolved().await?),
),
(
"next-server-utility".into(),
ResolvedVc::upcast(NextServerUtilityTransition::new().to_resolved().await?),
),
]
.into_iter()
.collect();
Expand Down Expand Up @@ -464,6 +480,10 @@ impl AppProject {
"next-shared".into(),
ResolvedVc::upcast(self.edge_shared_transition().to_resolved().await?),
),
(
"next-server-utility".into(),
ResolvedVc::upcast(NextServerUtilityTransition::new().to_resolved().await?),
),
]
.into_iter()
.collect();
Expand Down
1 change: 1 addition & 0 deletions crates/next-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub mod next_pages;
mod next_route_matcher;
pub mod next_server;
pub mod next_server_component;
pub mod next_server_utility;
mod next_shared;
pub mod next_telemetry;
mod page_loader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ use turbopack::css::CssModuleAsset;
use turbopack_core::{module::Module, reference::primary_referenced_modules};

use super::ecmascript_client_reference::ecmascript_client_reference_module::EcmascriptClientReferenceModule;
use crate::next_server_component::server_component_module::NextServerComponentModule;
use crate::{
next_server_component::server_component_module::NextServerComponentModule,
next_server_utility::server_utility_module::NextServerUtilityModule,
};

#[derive(
Copy, Clone, Eq, PartialEq, Hash, Serialize, Deserialize, Debug, ValueDebugFormat, TraceRawVcs,
Expand Down Expand Up @@ -149,11 +152,8 @@ pub async fn client_reference_graph(
server_component: *server_component,
}
} else {
let entry_path = module.ident().path().resolve().await?;
if entry_path.await?.file_name() == "edge-wrapper.js" {
VisitClientReferenceNodeState::EdgeEntry { entry_path }
} else {
VisitClientReferenceNodeState::Entry { entry_path }
VisitClientReferenceNodeState::Entry {
entry_path: module.ident().path().resolve().await?,
}
},
ty: VisitClientReferenceNodeType::Internal(
Expand Down Expand Up @@ -229,13 +229,7 @@ pub async fn find_server_entries(entry: ResolvedVc<Box<dyn Module>>) -> Result<V
.skip_duplicates()
.visit(
vec![VisitClientReferenceNode {
state: {
if entry_path.await?.file_name() == "edge-wrapper.js" {
VisitClientReferenceNodeState::EdgeEntry { entry_path }
} else {
VisitClientReferenceNodeState::Entry { entry_path }
}
},
state: { VisitClientReferenceNodeState::Entry { entry_path } },
ty: VisitClientReferenceNodeType::Internal(entry, entry.ident().to_string().await?),
}],
VisitClientReference {
Expand Down Expand Up @@ -285,9 +279,6 @@ struct VisitClientReferenceNode {
Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize, Debug, ValueDebugFormat, TraceRawVcs,
)]
enum VisitClientReferenceNodeState {
EdgeEntry {
entry_path: Vc<FileSystemPath>,
},
Entry {
entry_path: Vc<FileSystemPath>,
},
Expand All @@ -299,7 +290,6 @@ enum VisitClientReferenceNodeState {
impl VisitClientReferenceNodeState {
fn server_component(&self) -> Option<Vc<NextServerComponentModule>> {
match self {
VisitClientReferenceNodeState::EdgeEntry { .. } => None,
VisitClientReferenceNodeState::Entry { .. } => None,
VisitClientReferenceNodeState::InServerComponent { server_component } => {
Some(*server_component)
Expand Down Expand Up @@ -422,39 +412,17 @@ impl Visit<VisitClientReferenceNode> for VisitClientReference {
});
}

// TODO have a cleaner way to check for template modules
// e.g. edge-wrapper.js -> edge-ssr-app.js -> app-page.js

// Skip over edge-wrapper.js -> edge-ssr-app.js and properly detect server utils
if let VisitClientReferenceNodeState::EdgeEntry { entry_path } = &node.state {
let module_path = module.ident().path().resolve().await?;
let module_path_ref = &module_path.await?.path;
if module_path != *entry_path
&& !module_path_ref
.ends_with("next/dist/esm/build/templates/edge-ssr-app.js")
{
return Ok(VisitClientReferenceNode {
state: VisitClientReferenceNodeState::Entry {
entry_path: module_path,
},
ty: VisitClientReferenceNodeType::Internal(
*module,
module.ident().to_string().await?,
),
});
}
}

if let VisitClientReferenceNodeState::Entry { entry_path } = &node.state {
if module.ident().path().resolve().await? != *entry_path {
return Ok(VisitClientReferenceNode {
state: VisitClientReferenceNodeState::InServerUtil,
ty: VisitClientReferenceNodeType::ServerUtilEntry(
*module,
module.ident().to_string().await?,
),
});
}
if ResolvedVc::try_downcast_type::<NextServerUtilityModule>(*module)
.await?
.is_some()
{
return Ok(VisitClientReferenceNode {
state: VisitClientReferenceNodeState::InServerUtil,
ty: VisitClientReferenceNodeType::ServerUtilEntry(
*module,
module.ident().to_string().await?,
),
});
}

Ok(VisitClientReferenceNode {
Expand Down
5 changes: 5 additions & 0 deletions crates/next-core/src/next_server_utility/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod server_utility_module;
pub(crate) mod server_utility_reference;
pub(crate) mod server_utility_transition;

pub use server_utility_transition::NextServerUtilityTransition;
178 changes: 178 additions & 0 deletions crates/next-core/src/next_server_utility/server_utility_module.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
use std::collections::BTreeMap;

use anyhow::{bail, Result};
use indoc::formatdoc;
use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, Vc};
use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{
asset::{Asset, AssetContent},
chunk::{ChunkItem, ChunkItemExt, ChunkType, ChunkableModule, ChunkingContext},
ident::AssetIdent,
module::Module,
reference::ModuleReferences,
};
use turbopack_ecmascript::{
chunk::{
EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkPlaceable,
EcmascriptChunkType, EcmascriptExports,
},
references::esm::{EsmExport, EsmExports},
utils::StringifyJs,
};

use super::server_utility_reference::NextServerUtilityModuleReference;

#[turbo_tasks::function]
fn modifier() -> Vc<RcStr> {
Vc::cell("Next.js server utility".into())
}

#[turbo_tasks::value(shared)]
pub struct NextServerUtilityModule {
module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>,
}

#[turbo_tasks::value_impl]
impl NextServerUtilityModule {
#[turbo_tasks::function]
pub fn new(module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>) -> Vc<Self> {
NextServerUtilityModule { module }.cell()
}

#[turbo_tasks::function]
pub fn server_path(&self) -> Vc<FileSystemPath> {
self.module.ident().path()
}
}

#[turbo_tasks::value_impl]
impl Module for NextServerUtilityModule {
#[turbo_tasks::function]
fn ident(&self) -> Vc<AssetIdent> {
self.module.ident().with_modifier(modifier())
}

#[turbo_tasks::function]
fn references(&self) -> Vc<ModuleReferences> {
Vc::cell(vec![Vc::upcast(NextServerUtilityModuleReference::new(
Vc::upcast(*self.module),
))])
}
}

#[turbo_tasks::value_impl]
impl Asset for NextServerUtilityModule {
#[turbo_tasks::function]
fn content(&self) -> Result<Vc<AssetContent>> {
bail!("Next.js server component module has no content")
}
}

#[turbo_tasks::value_impl]
impl ChunkableModule for NextServerUtilityModule {
#[turbo_tasks::function]
fn as_chunk_item(
self: ResolvedVc<Self>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
) -> Vc<Box<dyn turbopack_core::chunk::ChunkItem>> {
Vc::upcast(
NextServerComponentChunkItem {
chunking_context,
inner: self,
}
.cell(),
)
}
}

#[turbo_tasks::value_impl]
impl EcmascriptChunkPlaceable for NextServerUtilityModule {
#[turbo_tasks::function]
async fn get_exports(&self) -> Result<Vc<EcmascriptExports>> {
let module_reference = ResolvedVc::upcast(
NextServerUtilityModuleReference::new(Vc::upcast(*self.module))
.to_resolved()
.await?,
);

let mut exports = BTreeMap::new();
exports.insert(
"default".into(),
EsmExport::ImportedBinding(*module_reference, "default".into(), false),
);

Ok(EcmascriptExports::EsmExports(
EsmExports {
exports,
star_exports: vec![*module_reference],
}
.resolved_cell(),
)
.cell())
}
}

#[turbo_tasks::value]
struct NextServerComponentChunkItem {
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
inner: ResolvedVc<NextServerUtilityModule>,
}

#[turbo_tasks::value_impl]
impl EcmascriptChunkItem for NextServerComponentChunkItem {
#[turbo_tasks::function]
fn chunking_context(&self) -> Vc<Box<dyn ChunkingContext>> {
*self.chunking_context
}

#[turbo_tasks::function]
async fn content(&self) -> Result<Vc<EcmascriptChunkItemContent>> {
let inner = self.inner.await?;

let module_id = inner
.module
.as_chunk_item(Vc::upcast(*self.chunking_context))
.id()
.await?;
Ok(EcmascriptChunkItemContent {
inner_code: formatdoc!(
r#"
__turbopack_export_namespace__(__turbopack_import__({}));
"#,
StringifyJs(&module_id),
)
.into(),
..Default::default()
}
.cell())
}
}

#[turbo_tasks::value_impl]
impl ChunkItem for NextServerComponentChunkItem {
#[turbo_tasks::function]
fn asset_ident(&self) -> Vc<AssetIdent> {
self.inner.ident()
}

#[turbo_tasks::function]
fn references(&self) -> Vc<ModuleReferences> {
self.inner.references()
}

#[turbo_tasks::function]
fn chunking_context(&self) -> Vc<Box<dyn ChunkingContext>> {
*self.chunking_context
}

#[turbo_tasks::function]
fn ty(&self) -> Vc<Box<dyn ChunkType>> {
Vc::upcast(Vc::<EcmascriptChunkType>::default())
}

#[turbo_tasks::function]
fn module(&self) -> Vc<Box<dyn Module>> {
Vc::upcast(*self.inner)
}
}
Loading

0 comments on commit 61c8e09

Please sign in to comment.