Download next.js/turbopack/crates/turbopack-json/src/lib.rs from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 4.98 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/turbopack/crates/turbopack-json/src/lib.rs
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/next.js/turbopack/crates/turbopack-json/src/lib.rs
-
curl -L -o lib.rs https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/turbopack/crates/turbopack-json/src/lib.rs
4.98 kB
| //! JSON asset support for turbopack. | |
| //! | |
| //! JSON assets are parsed to ensure they contain valid JSON. | |
| //! | |
| //! When imported from ES modules, they produce a module that exports the | |
| //! JSON value as an object. | |
| use std::fmt::Write; | |
| use anyhow::{Error, Result, bail}; | |
| use turbo_rcstr::rcstr; | |
| use turbo_tasks::{ResolvedVc, ValueToString, Vc}; | |
| use turbo_tasks_fs::{FileContent, FileJsonContent, glob::Glob}; | |
| use turbopack_core::{ | |
| asset::{Asset, AssetContent}, | |
| chunk::{ChunkItem, ChunkType, ChunkableModule, ChunkingContext}, | |
| ident::AssetIdent, | |
| module::Module, | |
| module_graph::ModuleGraph, | |
| source::Source, | |
| }; | |
| use turbopack_ecmascript::{ | |
| chunk::{ | |
| EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkPlaceable, | |
| EcmascriptChunkType, EcmascriptExports, | |
| }, | |
| runtime_functions::TURBOPACK_EXPORT_VALUE, | |
| }; | |
| pub struct JsonModuleAsset { | |
| source: ResolvedVc<Box<dyn Source>>, | |
| } | |
| impl JsonModuleAsset { | |
| pub fn new(source: ResolvedVc<Box<dyn Source>>) -> Vc<Self> { | |
| Self::cell(JsonModuleAsset { source }) | |
| } | |
| } | |
| impl Module for JsonModuleAsset { | |
| fn ident(&self) -> Vc<AssetIdent> { | |
| self.source.ident().with_modifier(rcstr!("json")) | |
| } | |
| } | |
| impl Asset for JsonModuleAsset { | |
| fn content(&self) -> Vc<AssetContent> { | |
| self.source.content() | |
| } | |
| } | |
| impl ChunkableModule for JsonModuleAsset { | |
| fn as_chunk_item( | |
| self: ResolvedVc<Self>, | |
| _module_graph: Vc<ModuleGraph>, | |
| chunking_context: ResolvedVc<Box<dyn ChunkingContext>>, | |
| ) -> Vc<Box<dyn turbopack_core::chunk::ChunkItem>> { | |
| Vc::upcast(JsonChunkItem::cell(JsonChunkItem { | |
| module: self, | |
| chunking_context, | |
| })) | |
| } | |
| } | |
| impl EcmascriptChunkPlaceable for JsonModuleAsset { | |
| fn get_exports(&self) -> Vc<EcmascriptExports> { | |
| EcmascriptExports::Value.cell() | |
| } | |
| fn is_marked_as_side_effect_free(&self, _side_effect_free_packages: Vc<Glob>) -> Vc<bool> { | |
| Vc::cell(true) | |
| } | |
| } | |
| struct JsonChunkItem { | |
| module: ResolvedVc<JsonModuleAsset>, | |
| chunking_context: ResolvedVc<Box<dyn ChunkingContext>>, | |
| } | |
| impl ChunkItem for JsonChunkItem { | |
| fn asset_ident(&self) -> Vc<AssetIdent> { | |
| self.module.ident() | |
| } | |
| fn chunking_context(&self) -> Vc<Box<dyn ChunkingContext>> { | |
| Vc::upcast(*self.chunking_context) | |
| } | |
| async fn ty(&self) -> Result<Vc<Box<dyn ChunkType>>> { | |
| Ok(Vc::upcast( | |
| Vc::<EcmascriptChunkType>::default().resolve().await?, | |
| )) | |
| } | |
| fn module(&self) -> Vc<Box<dyn Module>> { | |
| Vc::upcast(*self.module) | |
| } | |
| } | |
| impl EcmascriptChunkItem for JsonChunkItem { | |
| async fn content(&self) -> Result<Vc<EcmascriptChunkItemContent>> { | |
| // We parse to JSON and then stringify again to ensure that the | |
| // JSON is valid. | |
| let content = self.module.content().file_content(); | |
| let data = content.parse_json().await?; | |
| match &*data { | |
| FileJsonContent::Content(data) => { | |
| let js_str_content = serde_json::to_string(&data.to_string())?; | |
| let inner_code = format!("{TURBOPACK_EXPORT_VALUE}(JSON.parse({js_str_content}));"); | |
| Ok(EcmascriptChunkItemContent { | |
| inner_code: inner_code.into(), | |
| ..Default::default() | |
| } | |
| .into()) | |
| } | |
| FileJsonContent::Unparsable(e) => { | |
| let mut message = "Unable to make a module from invalid JSON: ".to_string(); | |
| if let FileContent::Content(content) = &*content.await? { | |
| let text = content.content().to_str()?; | |
| e.write_with_content(&mut message, text.as_ref())?; | |
| } else { | |
| write!(message, "{e}")?; | |
| } | |
| Err(Error::msg(message)) | |
| } | |
| FileJsonContent::NotFound => { | |
| bail!( | |
| "JSON file not found: {}", | |
| self.module.ident().to_string().await? | |
| ); | |
| } | |
| } | |
| } | |
| } | |
| pub fn register() { | |
| turbo_tasks::register(); | |
| turbo_tasks_fs::register(); | |
| turbopack_core::register(); | |
| turbopack_ecmascript::register(); | |
| include!(concat!(env!("OUT_DIR"), "/register.rs")); | |
| } | |