Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 64 additions & 80 deletions turbopack/crates/turbopack-css/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,6 @@ use crate::{
},
};

#[derive(Debug)]
pub struct StyleSheetLike<'i, 'o>(pub(crate) StyleSheet<'i, 'o>);

impl PartialEq for StyleSheetLike<'_, '_> {
fn eq(&self, _: &Self) -> bool {
false
}
}

pub type CssOutput = (ToCssResult, Option<Rope>);

#[turbo_tasks::value(transparent)]
Expand Down Expand Up @@ -94,60 +85,49 @@ async fn get_lightningcss_browser_targets(
}
}

impl StyleSheetLike<'_, '_> {
pub fn to_static(
&self,
options: ParserOptions<'static, 'static>,
) -> StyleSheetLike<'static, 'static> {
StyleSheetLike(stylesheet_into_static(&self.0, options))
}
async fn stylesheet_to_css(
ss: &StyleSheet<'_, '_>,
code: &str,
minify_type: MinifyType,
enable_srcmap: bool,
handle_nesting: bool,
mut origin_source_map: Option<parcel_sourcemap::SourceMap>,
environment: Option<ResolvedVc<Environment>>,
) -> Result<CssOutput> {
let mut srcmap = if enable_srcmap {
Some(parcel_sourcemap::SourceMap::new(""))
} else {
None
};

pub async fn to_css(
&self,
code: &str,
minify_type: MinifyType,
enable_srcmap: bool,
handle_nesting: bool,
mut origin_source_map: Option<parcel_sourcemap::SourceMap>,
environment: Option<ResolvedVc<Environment>>,
) -> Result<CssOutput> {
let ss = &self.0;
let mut srcmap = if enable_srcmap {
Some(parcel_sourcemap::SourceMap::new(""))
let targets =
*get_lightningcss_browser_targets(environment.as_deref().copied(), handle_nesting).await?;

let result = ss.to_css(PrinterOptions {
minify: matches!(minify_type, MinifyType::Minify { .. }),
source_map: srcmap.as_mut(),
targets,
analyze_dependencies: None,
..Default::default()
})?;

if let Some(srcmap) = &mut srcmap {
debug_assert_eq!(ss.sources.len(), 1);

if let Some(origin_source_map) = origin_source_map.as_mut() {
let _ = srcmap.extends(origin_source_map);
} else {
None
};

let targets =
*get_lightningcss_browser_targets(environment.as_deref().copied(), handle_nesting)
.await?;

let result = ss.to_css(PrinterOptions {
minify: matches!(minify_type, MinifyType::Minify { .. }),
source_map: srcmap.as_mut(),
targets,
analyze_dependencies: None,
..Default::default()
})?;

if let Some(srcmap) = &mut srcmap {
debug_assert_eq!(ss.sources.len(), 1);

if let Some(origin_source_map) = origin_source_map.as_mut() {
let _ = srcmap.extends(origin_source_map);
} else {
srcmap.add_sources(ss.sources.clone());
srcmap.set_source_content(0, code)?;
}
srcmap.add_sources(ss.sources.clone());
srcmap.set_source_content(0, code)?;
}
}

let srcmap = match srcmap {
Some(srcmap) => Some(generate_css_source_map(&srcmap)?),
None => None,
};
let srcmap = match srcmap {
Some(srcmap) => Some(generate_css_source_map(&srcmap)?),
None => None,
};

Ok((result, srcmap))
}
Ok((result, srcmap))
}

/// Multiple [ModuleReference]s
Expand All @@ -161,7 +141,7 @@ pub enum ParseCssResult {
code: ResolvedVc<FileContent>,

#[turbo_tasks(trace_ignore)]
stylesheet: StyleSheetLike<'static, 'static>,
stylesheet: StyleSheet<'static, 'static>,

references: ResolvedVc<ModuleReferences>,

Expand Down Expand Up @@ -228,9 +208,16 @@ pub async fn process_css_with_placeholder(

// We use NoMinify because this is not a final css. We need to replace url references,
// and we do final codegen with proper minification.
let (result, _) = stylesheet
.to_css(&code, MinifyType::NoMinify, false, false, None, environment)
.await?;
let (result, _) = stylesheet_to_css(
stylesheet,
&code,
MinifyType::NoMinify,
false,
false,
None,
environment,
)
.await?;

let exports = result.exports.map(|exports| {
let mut exports = exports.into_iter().collect::<FxIndexMap<_, _>>();
Expand Down Expand Up @@ -275,7 +262,7 @@ pub async fn finalize_css(
options,
code,
..
} => (stylesheet.to_static(options.clone()), *code),
} => (stylesheet_into_static(stylesheet, options.clone()), *code),
ParseCssResult::Unparsable => return Ok(FinalCssResult::Unparsable.cell()),
ParseCssResult::NotFound => return Ok(FinalCssResult::NotFound.cell()),
};
Expand Down Expand Up @@ -305,16 +292,16 @@ pub async fn finalize_css(
None
};

let (result, srcmap) = stylesheet
.to_css(
&code,
minify_type,
true,
true,
origin_source_map,
environment,
)
.await?;
let (result, srcmap) = stylesheet_to_css(
&stylesheet,
&code,
minify_type,
true,
true,
origin_source_map,
environment,
)
.await?;

Ok(FinalCssResult::Ok {
output_code: result.code,
Expand Down Expand Up @@ -435,7 +422,7 @@ async fn process_content(
..Default::default()
};

let stylesheet = StyleSheetLike({
let stylesheet = {
let warnings: Arc<RwLock<_>> = Default::default();

match StyleSheet::parse(
Expand All @@ -455,10 +442,7 @@ async fn process_content(
}
}

// We need to collect here because we need to avoid holding the lock while calling
// `.await` in the loop.
let warnings = warnings.read().unwrap().iter().cloned().collect::<Vec<_>>();
for err in warnings.iter() {
for err in warnings.read().unwrap().iter() {
match err.kind {
lightningcss::error::ParserError::UnexpectedToken(_)
| lightningcss::error::ParserError::UnexpectedImportRule
Expand Down Expand Up @@ -546,10 +530,10 @@ async fn process_content(
return Ok(ParseCssResult::Unparsable.cell());
}
}
});
};

let config = without_warnings(config);
let mut stylesheet = stylesheet.to_static(config.clone());
let mut stylesheet = stylesheet_into_static(&stylesheet, config.clone());

let (references, url_references) =
analyze_references(&mut stylesheet, source, origin, import_context).await?;
Expand Down
14 changes: 6 additions & 8 deletions turbopack/crates/turbopack-css/src/references/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::convert::Infallible;
use anyhow::Result;
use lightningcss::{
rules::CssRule,
stylesheet::StyleSheet,
traits::IntoOwned,
values::url::Url,
visitor::{Visit, Visitor},
Expand All @@ -18,12 +19,9 @@ use turbopack_core::{
source_pos::SourcePos,
};

use crate::{
StyleSheetLike,
references::{
import::{ImportAssetReference, ImportAttributes},
url::UrlAssetReference,
},
use crate::references::{
import::{ImportAssetReference, ImportAttributes},
url::UrlAssetReference,
};

pub(crate) mod compose;
Expand All @@ -38,7 +36,7 @@ pub type AnalyzedRefs = (

/// Returns `(all_references, urls)`.
pub async fn analyze_references(
stylesheet: &mut StyleSheetLike<'static, 'static>,
stylesheet: &mut StyleSheet<'static, 'static>,
source: ResolvedVc<Box<dyn Source>>,
origin: ResolvedVc<Box<dyn ResolveOrigin>>,
import_context: Option<ResolvedVc<ImportContext>>,
Expand All @@ -48,7 +46,7 @@ pub async fn analyze_references(

let mut visitor =
ModuleReferencesVisitor::new(source, origin, import_context, &mut references, &mut urls);
stylesheet.0.visit(&mut visitor).unwrap();
stylesheet.visit(&mut visitor).unwrap();

tokio::try_join!(
references.into_iter().map(|v| v.to_resolved()).try_join(),
Expand Down
10 changes: 4 additions & 6 deletions turbopack/crates/turbopack-css/src/references/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::convert::Infallible;

use anyhow::Result;
use lightningcss::{
stylesheet::StyleSheet,
values::url::Url,
visit_types,
visitor::{Visit, Visitor},
Expand All @@ -18,7 +19,7 @@ use turbopack_core::{
resolve::{ModuleResolveResult, origin::ResolveOrigin, parse::Request, url_resolve},
};

use crate::{StyleSheetLike, embed::CssEmbed};
use crate::embed::CssEmbed;

#[turbo_tasks::value]
pub enum ReferencedAsset {
Expand Down Expand Up @@ -122,12 +123,9 @@ pub async fn resolve_url_reference(
Ok(Vc::cell(None))
}

pub fn replace_url_references(
ss: &mut StyleSheetLike<'static, 'static>,
urls: &FxHashMap<RcStr, RcStr>,
) {
pub fn replace_url_references<'i, 'o>(ss: &mut StyleSheet<'i, 'o>, urls: &FxHashMap<RcStr, RcStr>) {
let mut replacer = AssetReferenceReplacer { urls };
ss.0.visit(&mut replacer).unwrap();
ss.visit(&mut replacer).unwrap();
}

struct AssetReferenceReplacer<'a> {
Expand Down
Loading