-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NextServerUtilityTransition to remove hack
- Loading branch information
Showing
8 changed files
with
320 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
178
crates/next-core/src/next_server_utility/server_utility_module.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.