Commit ee0d401f authored by Niklas Mischkulnig's avatar Niklas Mischkulnig

fixup

parent 625d338d
...@@ -23,7 +23,7 @@ use crate::{ ...@@ -23,7 +23,7 @@ use crate::{
code_gen::CodeGenerateable, code_gen::CodeGenerateable,
process::{ process::{
CssWithPlaceholderResult, FinalCssResult, ParseCss, ParseCssResult, ProcessCss, CssWithPlaceholderResult, FinalCssResult, ParseCss, ParseCssResult, ProcessCss,
finalize_css, finalize_css_module, parse_css, process_css_with_placeholder, finalize_css, parse_css, process_css_with_placeholder,
}, },
references::{ references::{
compose::CssModuleComposeReference, import::ImportAssetReference, url::ReferencedAsset, compose::CssModuleComposeReference, import::ImportAssetReference, url::ReferencedAsset,
...@@ -135,6 +135,7 @@ impl ProcessCss for CssModule { ...@@ -135,6 +135,7 @@ impl ProcessCss for CssModule {
self: Vc<Self>, self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>, chunking_context: Vc<Box<dyn ChunkingContext>>,
minify_type: MinifyType, minify_type: MinifyType,
ecmascript_module: Option<Vc<Box<dyn Module>>>,
) -> Result<Vc<FinalCssResult>> { ) -> Result<Vc<FinalCssResult>> {
let process_result = self.get_css_with_placeholder(); let process_result = self.get_css_with_placeholder();
...@@ -151,31 +152,6 @@ impl ProcessCss for CssModule { ...@@ -151,31 +152,6 @@ impl ProcessCss for CssModule {
origin_source_map, origin_source_map,
this.environment.as_deref().copied(), this.environment.as_deref().copied(),
this.lightningcss_features, this.lightningcss_features,
))
}
#[turbo_tasks::function]
async fn finalize_css_module(
self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
minify_type: MinifyType,
ecmascript_module: Vc<Box<dyn Module>>,
) -> Result<Vc<FinalCssResult>> {
let process_result = self.get_css_with_placeholder();
let this = self.await?;
let origin_source_map =
match ResolvedVc::try_sidecast::<Box<dyn GenerateSourceMap>>(this.source) {
Some(gsm) => gsm.generate_source_map(),
None => FileContent::NotFound.cell(),
};
Ok(finalize_css_module(
process_result,
chunking_context,
minify_type,
origin_source_map,
this.environment.as_deref().copied(),
this.lightningcss_features,
ecmascript_module, ecmascript_module,
)) ))
} }
...@@ -405,15 +381,14 @@ impl CssChunkItem for CssModuleChunkItem { ...@@ -405,15 +381,14 @@ impl CssChunkItem for CssModuleChunkItem {
} }
let minify_type = *chunking_context.minify_type().await?; let minify_type = *chunking_context.minify_type().await?;
let result = if let Some(ecmascript_module) = self.ecmascript_module { let result = self
self.module .module
.finalize_css_module(*chunking_context, minify_type, *ecmascript_module) .finalize_css(
.await? *chunking_context,
} else { minify_type,
self.module self.ecmascript_module.map(|m| *m),
.finalize_css(*chunking_context, minify_type) )
.await? .await?;
};
if let FinalCssResult::Ok { if let FinalCssResult::Ok {
output_code, output_code,
......
use std::sync::{Arc, RwLock}; use std::{
collections::HashSet,
sync::{Arc, RwLock},
};
use anyhow::{Result, bail}; use anyhow::{Result, bail};
use async_trait::async_trait; use async_trait::async_trait;
...@@ -268,97 +271,7 @@ pub async fn finalize_css( ...@@ -268,97 +271,7 @@ pub async fn finalize_css(
origin_source_map: Vc<FileContent>, origin_source_map: Vc<FileContent>,
environment: Option<ResolvedVc<Environment>>, environment: Option<ResolvedVc<Environment>>,
feature_flags: LightningCssFeatureFlags, feature_flags: LightningCssFeatureFlags,
) -> Result<Vc<FinalCssResult>> { ecmascript_module: Option<ResolvedVc<Box<dyn Module>>>,
let result = result.await?;
match &*result {
CssWithPlaceholderResult::Ok {
parse_result,
url_references,
..
} => {
let (mut stylesheet, code) = match &*parse_result.await? {
ParseCssResult::Ok {
stylesheet,
options,
code,
..
} => (stylesheet_into_static(stylesheet, options.clone()), *code),
ParseCssResult::Unparsable => return Ok(FinalCssResult::Unparsable.cell()),
ParseCssResult::NotFound => return Ok(FinalCssResult::NotFound.cell()),
};
let url_references = *url_references;
let mut url_map = FxHashMap::default();
for (src, reference) in (*url_references.await?).iter() {
let resolved = resolve_url_reference(**reference, chunking_context).await?;
if let Some(v) = resolved.as_ref().cloned() {
url_map.insert(RcStr::from(src.as_str()), v);
}
}
replace_url_references(&mut stylesheet, &url_map);
let code = code.await?;
let code = match &*code {
FileContent::Content(v) => v.content().to_str()?,
_ => bail!("this case should be filtered out while parsing"),
};
let origin_source_map = if let Some(rope) = origin_source_map.await?.as_content() {
Some(parcel_sourcemap::SourceMap::from_json(
"",
&rope.content().to_str()?,
)?)
} else {
None
};
let (result, srcmap) = stylesheet_to_css(
&stylesheet,
&code,
minify_type,
true,
true,
origin_source_map,
environment,
feature_flags,
)
.await?;
Ok(FinalCssResult::Ok {
output_code: result.code,
source_map: if let Some(srcmap) = srcmap {
FileContent::Content(File::from(srcmap)).resolved_cell()
} else {
FileContent::NotFound.resolved_cell()
},
}
.cell())
}
CssWithPlaceholderResult::Unparsable => Ok(FinalCssResult::Unparsable.cell()),
CssWithPlaceholderResult::NotFound => Ok(FinalCssResult::NotFound.cell()),
}
}
/// Like [`finalize_css`], but also performs CSS tree shaking for CSS Modules by removing CSS rules
/// for class names that are unused according to the JS module binding analysis.
///
/// The `ecmascript_module` parameter is the [`EcmascriptCssModule`] that owns this CSS, used to
/// query [`ChunkingContext::module_export_usage`] and determine which class names are actually
/// referenced in JS.
///
/// [`EcmascriptCssModule`]: crate::EcmascriptCssModule
#[turbo_tasks::function]
pub async fn finalize_css_module(
result: Vc<CssWithPlaceholderResult>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
minify_type: MinifyType,
origin_source_map: Vc<FileContent>,
environment: Option<ResolvedVc<Environment>>,
feature_flags: LightningCssFeatureFlags,
ecmascript_module: Vc<Box<dyn Module>>,
) -> Result<Vc<FinalCssResult>> { ) -> Result<Vc<FinalCssResult>> {
let result = result.await?; let result = result.await?;
match &*result { match &*result {
...@@ -399,41 +312,40 @@ pub async fn finalize_css_module( ...@@ -399,41 +312,40 @@ pub async fn finalize_css_module(
// class whose original name is not used gets its hashed CSS name added to the // class whose original name is not used gets its hashed CSS name added to the
// `unused_symbols` set passed to lightningcss, which then removes the corresponding // `unused_symbols` set passed to lightningcss, which then removes the corresponding
// CSS rules. // CSS rules.
if let Some(exports) = exports { if let Some(exports) = exports
&& let Some(ecmascript_module) = ecmascript_module
{
let export_usage = chunking_context let export_usage = chunking_context
.module_export_usage(ecmascript_module) .module_export_usage(*ecmascript_module)
.await?; .await?;
let export_usage_info = export_usage.export_usage.await?;
if !export_usage.is_circuit_breaker {
let export_usage_info = export_usage.export_usage.await?; let unused_symbols: HashSet<String> = match &*export_usage_info {
let unused_symbols: std::collections::HashSet<String> = // All exports used, or unknown — keep every class.
match &*export_usage_info { ModuleExportUsageInfo::All => Default::default(),
// All exports used, or unknown — keep every class. // Some or no exports used — collect hashed names of unused classes.
ModuleExportUsageInfo::All => Default::default(), ModuleExportUsageInfo::Evaluation | ModuleExportUsageInfo::Exports(_) => {
// Some or no exports used — collect hashed names of unused classes. exports
ModuleExportUsageInfo::Evaluation .iter()
| ModuleExportUsageInfo::Exports(_) => exports .filter(|(orig_name, _)| {
.iter() !export_usage_info.is_export_used(&RcStr::from(orig_name.as_str()))
.filter(|(orig_name, _)| { })
!export_usage_info .map(|(orig_name, _)| orig_name.clone())
.is_export_used(&RcStr::from(orig_name.as_str())) .collect()
})
.map(|(_, export)| export.name.clone())
.collect(),
};
if !unused_symbols.is_empty() {
let targets = *get_lightningcss_browser_targets(
environment.as_deref().copied(),
true,
feature_flags,
)
.await?;
let _ = stylesheet.minify(MinifyOptions {
targets,
unused_symbols,
});
} }
};
if !unused_symbols.is_empty() {
let targets = *get_lightningcss_browser_targets(
environment.as_deref().copied(),
true,
feature_flags,
)
.await?;
stylesheet.minify(MinifyOptions {
targets,
unused_symbols,
})?;
} }
} }
...@@ -495,14 +407,7 @@ pub trait ProcessCss: ParseCss { ...@@ -495,14 +407,7 @@ pub trait ProcessCss: ParseCss {
self: Vc<Self>, self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>, chunking_context: Vc<Box<dyn ChunkingContext>>,
minify_type: MinifyType, minify_type: MinifyType,
) -> Result<Vc<FinalCssResult>>; ecmascript_module: Option<Vc<Box<dyn Module>>>,
#[turbo_tasks::function]
async fn finalize_css_module(
self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
minify_type: MinifyType,
ecmascript_module: Vc<Box<dyn Module>>,
) -> Result<Vc<FinalCssResult>>; ) -> Result<Vc<FinalCssResult>>;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment