Download next.js/turbopack/crates/turbopack-css/src/references/import.rs from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 5.54 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/turbopack/crates/turbopack-css/src/references/import.rs
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/next.js/turbopack/crates/turbopack-css/src/references/import.rs
-
curl -L -o import.rs https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/turbopack/crates/turbopack-css/src/references/import.rs
5.54 kB
| use anyhow::Result; | |
| use lightningcss::{ | |
| media_query::MediaList, | |
| rules::{import::ImportRule, layer::LayerName, supports::SupportsCondition}, | |
| traits::ToCss, | |
| }; | |
| use turbo_rcstr::RcStr; | |
| use turbo_tasks::{ResolvedVc, ValueToString, Vc}; | |
| use turbopack_core::{ | |
| chunk::{ChunkableModuleReference, ChunkingContext}, | |
| issue::IssueSource, | |
| reference::ModuleReference, | |
| reference_type::{CssReferenceSubType, ImportContext}, | |
| resolve::{ModuleResolveResult, origin::ResolveOrigin, parse::Request}, | |
| }; | |
| use crate::{ | |
| chunk::CssImport, | |
| code_gen::{CodeGenerateable, CodeGeneration}, | |
| references::css_resolve, | |
| }; | |
| pub enum ImportAttributes { | |
| LightningCss { | |
| layer_name: Option<LayerName<'static>>, | |
| supports: Option<SupportsCondition<'static>>, | |
| media: MediaList<'static>, | |
| }, | |
| } | |
| impl PartialEq for ImportAttributes { | |
| fn eq(&self, _: &Self) -> bool { | |
| false | |
| } | |
| } | |
| impl ImportAttributes { | |
| pub fn new_from_lightningcss(prelude: &ImportRule<'static>) -> Self { | |
| let layer_name = prelude.layer.clone().flatten(); | |
| let supports = prelude.supports.clone(); | |
| let media = prelude.media.clone(); | |
| Self::LightningCss { | |
| layer_name, | |
| supports, | |
| media, | |
| } | |
| } | |
| fn as_reference_import_attributes(&self) -> turbopack_core::reference_type::ImportAttributes { | |
| match self { | |
| ImportAttributes::LightningCss { | |
| layer_name, | |
| supports, | |
| media, | |
| } => turbopack_core::reference_type::ImportAttributes { | |
| layer: layer_name | |
| .as_ref() | |
| .map(|l| l.to_css_string(Default::default()).unwrap()) | |
| .map(From::from), | |
| supports: supports | |
| .as_ref() | |
| .map(|s| s.to_css_string(Default::default()).unwrap()) | |
| .map(From::from), | |
| media: { | |
| if media.always_matches() { | |
| None | |
| } else { | |
| Some(media.to_css_string(Default::default()).unwrap().into()) | |
| } | |
| }, | |
| }, | |
| } | |
| } | |
| } | |
| pub struct ImportAssetReference { | |
| pub origin: ResolvedVc<Box<dyn ResolveOrigin>>, | |
| pub request: ResolvedVc<Request>, | |
| pub attributes: ResolvedVc<ImportAttributes>, | |
| pub import_context: Option<ResolvedVc<ImportContext>>, | |
| pub issue_source: IssueSource, | |
| } | |
| impl ImportAssetReference { | |
| pub fn new( | |
| origin: ResolvedVc<Box<dyn ResolveOrigin>>, | |
| request: ResolvedVc<Request>, | |
| attributes: ResolvedVc<ImportAttributes>, | |
| import_context: Option<ResolvedVc<ImportContext>>, | |
| issue_source: IssueSource, | |
| ) -> Vc<Self> { | |
| Self::cell(ImportAssetReference { | |
| origin, | |
| request, | |
| attributes, | |
| import_context, | |
| issue_source, | |
| }) | |
| } | |
| } | |
| impl ModuleReference for ImportAssetReference { | |
| async fn resolve_reference(&self) -> Result<Vc<ModuleResolveResult>> { | |
| let own_attrs = self.attributes.await?.as_reference_import_attributes(); | |
| let import_context = match (&self.import_context, own_attrs.is_empty()) { | |
| (Some(import_context), true) => Some(*import_context), | |
| (None, false) => Some( | |
| ImportContext::new( | |
| own_attrs.layer.iter().cloned().collect(), | |
| own_attrs.media.iter().cloned().collect(), | |
| own_attrs.supports.iter().cloned().collect(), | |
| ) | |
| .to_resolved() | |
| .await?, | |
| ), | |
| (Some(import_context), false) => Some( | |
| import_context | |
| .add_attributes(own_attrs.layer, own_attrs.media, own_attrs.supports) | |
| .to_resolved() | |
| .await?, | |
| ), | |
| (None, true) => None, | |
| }; | |
| Ok(css_resolve( | |
| *self.origin, | |
| *self.request, | |
| CssReferenceSubType::AtImport(import_context), | |
| Some(self.issue_source), | |
| )) | |
| } | |
| } | |
| impl ValueToString for ImportAssetReference { | |
| async fn to_string(&self) -> Result<Vc<RcStr>> { | |
| Ok(Vc::cell( | |
| format!("import(url) {}", self.request.to_string().await?,).into(), | |
| )) | |
| } | |
| } | |
| impl CodeGenerateable for ImportAssetReference { | |
| async fn code_generation( | |
| self: Vc<Self>, | |
| _context: Vc<Box<dyn ChunkingContext>>, | |
| ) -> Result<Vc<CodeGeneration>> { | |
| let this = &*self.await?; | |
| let mut imports = vec![]; | |
| if let Request::Uri { | |
| protocol, | |
| remainder, | |
| .. | |
| } = &*this.request.await? | |
| { | |
| imports.push(CssImport::External(ResolvedVc::cell( | |
| format!("{protocol}{remainder}").into(), | |
| ))) | |
| } | |
| Ok(CodeGeneration { imports }.into()) | |
| } | |
| } | |
| impl ChunkableModuleReference for ImportAssetReference {} | |