Download next.js/turbopack/crates/turbopack-dev-server/src/html.rs from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 8.26 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/turbopack/crates/turbopack-dev-server/src/html.rs
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/next.js/turbopack/crates/turbopack-dev-server/src/html.rs
-
curl -L -o html.rs https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/turbopack/crates/turbopack-dev-server/src/html.rs
8.26 kB
| use anyhow::Result; | |
| use mime_guess::mime::TEXT_HTML_UTF_8; | |
| use serde::{Deserialize, Serialize}; | |
| use turbo_rcstr::RcStr; | |
| use turbo_tasks::{ | |
| NonLocalValue, ReadRef, ResolvedVc, TaskInput, TryJoinIterExt, Vc, trace::TraceRawVcs, | |
| }; | |
| use turbo_tasks_fs::{File, FileSystemPath}; | |
| use turbo_tasks_hash::{Xxh3Hash64Hasher, encode_hex}; | |
| use turbopack_core::{ | |
| asset::{Asset, AssetContent}, | |
| chunk::{ | |
| ChunkableModule, ChunkingContext, ChunkingContextExt, EvaluatableAssets, | |
| availability_info::AvailabilityInfo, | |
| }, | |
| module::Module, | |
| module_graph::{ModuleGraph, chunk_group_info::ChunkGroup}, | |
| output::{OutputAsset, OutputAssets}, | |
| version::{Version, VersionedContent}, | |
| }; | |
| pub struct DevHtmlEntry { | |
| pub chunkable_module: ResolvedVc<Box<dyn ChunkableModule>>, | |
| pub module_graph: ResolvedVc<ModuleGraph>, | |
| pub chunking_context: ResolvedVc<Box<dyn ChunkingContext>>, | |
| pub runtime_entries: Option<ResolvedVc<EvaluatableAssets>>, | |
| } | |
| /// The HTML entry point of the dev server. | |
| /// | |
| /// Generates an HTML page that includes the ES and CSS chunks. | |
| pub struct DevHtmlAsset { | |
| path: FileSystemPath, | |
| entries: Vec<DevHtmlEntry>, | |
| body: Option<RcStr>, | |
| } | |
| impl OutputAsset for DevHtmlAsset { | |
| fn path(&self) -> Vc<FileSystemPath> { | |
| self.path.clone().cell() | |
| } | |
| fn references(self: Vc<Self>) -> Vc<OutputAssets> { | |
| self.chunks() | |
| } | |
| } | |
| impl Asset for DevHtmlAsset { | |
| fn content(self: Vc<Self>) -> Vc<AssetContent> { | |
| self.html_content().content() | |
| } | |
| fn versioned_content(self: Vc<Self>) -> Vc<Box<dyn VersionedContent>> { | |
| Vc::upcast(self.html_content()) | |
| } | |
| } | |
| impl DevHtmlAsset { | |
| /// Create a new dev HTML asset. | |
| pub fn new(path: FileSystemPath, entries: Vec<DevHtmlEntry>) -> Vc<Self> { | |
| DevHtmlAsset { | |
| path, | |
| entries, | |
| body: None, | |
| } | |
| .cell() | |
| } | |
| /// Create a new dev HTML asset. | |
| pub fn new_with_body( | |
| path: FileSystemPath, | |
| entries: Vec<DevHtmlEntry>, | |
| body: RcStr, | |
| ) -> Vc<Self> { | |
| DevHtmlAsset { | |
| path, | |
| entries, | |
| body: Some(body), | |
| } | |
| .cell() | |
| } | |
| } | |
| impl DevHtmlAsset { | |
| pub async fn with_path(self: Vc<Self>, path: FileSystemPath) -> Result<Vc<Self>> { | |
| let mut html: DevHtmlAsset = self.owned().await?; | |
| html.path = path; | |
| Ok(html.cell()) | |
| } | |
| pub async fn with_body(self: Vc<Self>, body: RcStr) -> Result<Vc<Self>> { | |
| let mut html: DevHtmlAsset = self.owned().await?; | |
| html.body = Some(body); | |
| Ok(html.cell()) | |
| } | |
| } | |
| impl DevHtmlAsset { | |
| async fn html_content(self: Vc<Self>) -> Result<Vc<DevHtmlAssetContent>> { | |
| let this = self.await?; | |
| let context_path = this.path.parent(); | |
| let mut chunk_paths = vec![]; | |
| for chunk in &*self.chunks().await? { | |
| let chunk_path = &*chunk.path().await?; | |
| if let Some(relative_path) = context_path.get_path_to(chunk_path) { | |
| chunk_paths.push(format!("/{relative_path}").into()); | |
| } | |
| } | |
| Ok(DevHtmlAssetContent::new(chunk_paths, this.body.clone())) | |
| } | |
| async fn chunks(&self) -> Result<Vc<OutputAssets>> { | |
| let all_assets = self | |
| .entries | |
| .iter() | |
| .map(|entry| async move { | |
| let &DevHtmlEntry { | |
| chunkable_module, | |
| chunking_context, | |
| module_graph, | |
| runtime_entries, | |
| } = entry; | |
| let assets = if let Some(runtime_entries) = runtime_entries { | |
| let runtime_entries = | |
| if let Some(evaluatable) = ResolvedVc::try_downcast(chunkable_module) { | |
| runtime_entries | |
| .with_entry(*evaluatable) | |
| .to_resolved() | |
| .await? | |
| } else { | |
| runtime_entries | |
| }; | |
| chunking_context.evaluated_chunk_group_assets( | |
| chunkable_module.ident(), | |
| ChunkGroup::Entry( | |
| runtime_entries | |
| .await? | |
| .iter() | |
| .map(|v| ResolvedVc::upcast(*v)) | |
| .collect(), | |
| ), | |
| *module_graph, | |
| AvailabilityInfo::Root, | |
| ) | |
| } else { | |
| chunking_context.root_chunk_group_assets( | |
| chunkable_module.ident(), | |
| ChunkGroup::Entry(vec![ResolvedVc::upcast(chunkable_module)]), | |
| *module_graph, | |
| ) | |
| }; | |
| assets.await | |
| }) | |
| .try_join() | |
| .await? | |
| .iter() | |
| .flatten() | |
| .copied() | |
| .collect(); | |
| Ok(Vc::cell(all_assets)) | |
| } | |
| } | |
| struct DevHtmlAssetContent { | |
| chunk_paths: Vec<RcStr>, | |
| body: Option<RcStr>, | |
| } | |
| impl DevHtmlAssetContent { | |
| fn new(chunk_paths: Vec<RcStr>, body: Option<RcStr>) -> Vc<Self> { | |
| DevHtmlAssetContent { chunk_paths, body }.cell() | |
| } | |
| } | |
| impl DevHtmlAssetContent { | |
| fn content(&self) -> Result<Vc<AssetContent>> { | |
| let mut scripts = Vec::new(); | |
| let mut stylesheets = Vec::new(); | |
| for relative_path in &*self.chunk_paths { | |
| if relative_path.ends_with(".js") { | |
| scripts.push(format!("<script src=\"{relative_path}\"></script>")); | |
| } else if relative_path.ends_with(".css") { | |
| stylesheets.push(format!( | |
| "<link data-turbopack rel=\"stylesheet\" href=\"{relative_path}\">" | |
| )); | |
| } else { | |
| anyhow::bail!("chunk with unknown asset type: {}", relative_path) | |
| } | |
| } | |
| let body = match &self.body { | |
| Some(body) => body.as_str(), | |
| None => "", | |
| }; | |
| let html: RcStr = format!( | |
| "<!DOCTYPE html>\n<html>\n<head>\n{}\n</head>\n<body>\n{}\n{}\n</body>\n</html>", | |
| stylesheets.join("\n"), | |
| body, | |
| scripts.join("\n"), | |
| ) | |
| .into(); | |
| Ok(AssetContent::file( | |
| File::from(html).with_content_type(TEXT_HTML_UTF_8).into(), | |
| )) | |
| } | |
| async fn version(self: Vc<Self>) -> Result<Vc<DevHtmlAssetVersion>> { | |
| let this = self.await?; | |
| Ok(DevHtmlAssetVersion { content: this }.cell()) | |
| } | |
| } | |
| impl VersionedContent for DevHtmlAssetContent { | |
| fn content(self: Vc<Self>) -> Vc<AssetContent> { | |
| self.content() | |
| } | |
| fn version(self: Vc<Self>) -> Vc<Box<dyn Version>> { | |
| Vc::upcast(self.version()) | |
| } | |
| } | |
| struct DevHtmlAssetVersion { | |
| content: ReadRef<DevHtmlAssetContent>, | |
| } | |
| impl Version for DevHtmlAssetVersion { | |
| fn id(&self) -> Vc<RcStr> { | |
| let mut hasher = Xxh3Hash64Hasher::new(); | |
| for relative_path in &*self.content.chunk_paths { | |
| hasher.write_ref(relative_path); | |
| } | |
| if let Some(body) = &self.content.body { | |
| hasher.write_ref(body); | |
| } | |
| let hash = hasher.finish(); | |
| let hex_hash = encode_hex(hash); | |
| Vc::cell(hex_hash.into()) | |
| } | |
| } | |