Skip to content

Commit b344266

Browse files
committed
Merge master and adapt URL regression tests to custom source maps
2 parents 35d6b3c + 724c806 commit b344266

97 files changed

Lines changed: 4471 additions & 2120 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ crate-type = ["rlib"]
3636
[features]
3737
default = ["bundler", "nodejs", "sourcemap"]
3838
browserslist = ["browserslist-rs"]
39-
bundler = ["dashmap", "sourcemap", "rayon"]
40-
cli = ["clap", "serde_json", "browserslist", "jemallocator"]
39+
bundler = ["dashmap", "custom_sourcemap", "rayon"]
40+
cli = ["clap", "serde_json", "browserslist", "jemallocator", "sourcemap"]
4141
jsonschema = ["schemars", "serde", "parcel_selectors/jsonschema"]
4242
nodejs = ["dep:serde", "dep:serde-content"]
4343
serde = [
@@ -49,7 +49,8 @@ serde = [
4949
"parcel_selectors/serde",
5050
"into_owned",
5151
]
52-
sourcemap = ["parcel_sourcemap"]
52+
sourcemap = ["custom_sourcemap", "dep:parcel_sourcemap"]
53+
custom_sourcemap = ["dep:rustc-hash"]
5354
visitor = []
5455
into_owned = [
5556
"static-self",
@@ -74,6 +75,7 @@ lazy_static = "1.4.0"
7475
const-str = "1.1.0"
7576
pathdiff = "0.2.1"
7677
ahash = "0.8.7"
78+
rustc-hash = { version = "2", optional = true }
7779
pastey = "0.1.0"
7880
indexmap = { version = "2.2.6", features = ["serde"] }
7981
# CLI deps

‎benches/common/mod.rs‎

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use criterion::black_box;
2+
use parcel_sourcemap::SourceMap;
23
use rspack_lightningcss::stylesheet::{MinifyOptions, ParserOptions, PrinterOptions, StyleSheet};
34
use std::fmt::Write;
45

@@ -37,14 +38,43 @@ pub fn transform_stylesheet(css: &str) {
3738
let mut stylesheet = StyleSheet::parse(css, ParserOptions::default()).unwrap();
3839
stylesheet.minify(MinifyOptions::default()).unwrap();
3940
let result = stylesheet
40-
.to_css(PrinterOptions {
41-
minify: true,
42-
..PrinterOptions::default()
43-
})
41+
.to_css(
42+
PrinterOptions {
43+
minify: true,
44+
..PrinterOptions::default()
45+
},
46+
None::<&mut ()>,
47+
)
4448
.unwrap();
4549
black_box(result);
4650
}
4751

52+
pub fn transform_stylesheet_with_source_map(css: &str) {
53+
let mut source_map = SourceMap::new("/");
54+
let source_index = source_map.add_source("bench.css");
55+
source_map.set_source_content(source_index as usize, css).unwrap();
56+
57+
let mut stylesheet = StyleSheet::parse(
58+
css,
59+
ParserOptions {
60+
source_index,
61+
..ParserOptions::default()
62+
},
63+
)
64+
.unwrap();
65+
stylesheet.minify(MinifyOptions::default()).unwrap();
66+
let result = stylesheet
67+
.to_css(
68+
PrinterOptions {
69+
minify: true,
70+
..PrinterOptions::default()
71+
},
72+
Some(&mut source_map),
73+
)
74+
.unwrap();
75+
black_box((result, source_map));
76+
}
77+
4878
fn small_fixture() -> String {
4979
r#"
5080
:root {

‎benches/transform.rs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,19 @@ fn bench_stylesheet_transform(c: &mut Criterion) {
2727
for fixture in &fixtures {
2828
group.throughput(Throughput::Bytes(fixture.css.len() as u64));
2929
group.bench_with_input(
30-
BenchmarkId::from_parameter(fixture.name),
30+
BenchmarkId::new("no-source-map", fixture.name),
3131
fixture.css.as_str(),
3232
|b, css| {
3333
b.iter(|| common::transform_stylesheet(black_box(css)));
3434
},
3535
);
36+
group.bench_with_input(
37+
BenchmarkId::new("source-map", fixture.name),
38+
fixture.css.as_str(),
39+
|b, css| {
40+
b.iter(|| common::transform_stylesheet_with_source_map(black_box(css)));
41+
},
42+
);
3643
}
3744

3845
group.finish();

‎c/src/lib.rs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,6 @@ pub extern "C" fn lightningcss_stylesheet_to_css(
338338
} else {
339339
Some(unsafe { std::str::from_utf8_unchecked(CStr::from_ptr(options.project_root).to_bytes()) })
340340
},
341-
source_map: source_map.as_mut(),
342341
targets: if options.targets != Targets::default() {
343342
Some(options.targets.into()).into()
344343
} else {
@@ -356,7 +355,11 @@ pub extern "C" fn lightningcss_stylesheet_to_css(
356355
},
357356
};
358357

359-
let res = unwrap!(wrapper.stylesheet.to_css(opts), error, ToCssResult::default());
358+
let res = unwrap!(
359+
wrapper.stylesheet.to_css(opts, source_map.as_mut()),
360+
error,
361+
ToCssResult::default()
362+
);
360363

361364
let map = if let Some(mut source_map) = source_map {
362365
if !options.input_source_map.is_null() {

‎derive/src/to_css.rs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ pub fn derive_to_css(input: TokenStream) -> TokenStream {
2525

2626
let output = quote! {
2727
impl #impl_generics ToCss for #ident #ty_generics #where_clause {
28-
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
29-
where
30-
W: std::fmt::Write,
31-
{
28+
fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
3229
#imp
3330
}
3431
}
@@ -121,7 +118,10 @@ fn derive_enum(data: &DataEnum, opts: &CssOptions) -> TokenStream2 {
121118
Fields::Unit => {
122119
let s = Literal::string(&variant.ident.to_string().to_case(opts.case));
123120
quote! {
124-
Self::#name => dest.write_str(#s)
121+
Self::#name => {
122+
dest.write_str(#s)?;
123+
Ok(())
124+
}
125125
}
126126
}
127127
Fields::Named(_) => {

‎examples/custom_at_rule.rs‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use cssparser::*;
44
use rspack_lightningcss::{
55
declaration::DeclarationBlock,
66
error::PrinterError,
7-
printer::Printer,
7+
printer::PrinterTrait,
88
properties::custom::{Token, TokenOrValue},
99
rules::{style::StyleRule, CssRule, CssRuleList, Location},
1010
selector::{Component, Selector},
@@ -42,14 +42,17 @@ fn main() {
4242
stylesheet.visit(&mut ApplyVisitor { rules: &style_rules }).unwrap();
4343

4444
let result = stylesheet
45-
.to_css(PrinterOptions {
46-
targets: Browsers {
47-
chrome: Some(100 << 16),
48-
..Browsers::default()
49-
}
50-
.into(),
51-
..PrinterOptions::default()
52-
})
45+
.to_css(
46+
PrinterOptions {
47+
targets: Browsers {
48+
chrome: Some(100 << 16),
49+
..Browsers::default()
50+
}
51+
.into(),
52+
..PrinterOptions::default()
53+
},
54+
None::<&mut ()>,
55+
)
5356
.unwrap();
5457
println!("{}", result.code);
5558
}
@@ -294,7 +297,7 @@ impl<'i, V: Visitor<'i, AtRule>> Visit<'i, AtRule, V> for AtRule {
294297
}
295298

296299
impl ToCss for AtRule {
297-
fn to_css<W: std::fmt::Write>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError> {
300+
fn to_css<PrinterT: PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
298301
match self {
299302
AtRule::Tailwind(rule) => {
300303
let _ = rule.loc; // TODO: source maps
@@ -304,7 +307,8 @@ impl ToCss for AtRule {
304307
TailwindDirective::Utilities => "TAILWIND UTILITIES HERE",
305308
TailwindDirective::Variants => "TAILWIND VARIANTS HERE",
306309
};
307-
dest.write_str(directive)
310+
dest.write_str(directive)?;
311+
Ok(())
308312
}
309313
AtRule::Apply(_) => Ok(()),
310314
}

‎napi/src/at_rule_parser.rs‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use cssparser::*;
44
use lightningcss::{
55
declaration::DeclarationBlock,
66
error::ParserError,
7+
printer::PrinterTrait,
78
rules::{CssRuleList, Location},
89
stylesheet::ParserOptions,
910
traits::{AtRuleParser, ToCss},
@@ -161,13 +162,7 @@ impl<'i> AtRuleParser<'i> for CustomAtRuleParser {
161162
}
162163

163164
impl<'i> ToCss for AtRule<'i> {
164-
fn to_css<W>(
165-
&self,
166-
dest: &mut lightningcss::printer::Printer<W>,
167-
) -> Result<(), lightningcss::error::PrinterError>
168-
where
169-
W: std::fmt::Write,
170-
{
165+
fn to_css<PrinterT: PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), lightningcss::error::PrinterError> {
171166
dest.write_char('@')?;
172167
serialize_identifier(&self.name, dest)?;
173168
if let Some(prelude) = &self.prelude {

‎napi/src/lib.rs‎

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -763,24 +763,26 @@ fn compile<'i>(
763763
unused_symbols: config.unused_symbols.clone().unwrap_or_default(),
764764
})?;
765765

766-
stylesheet.to_css(PrinterOptions {
767-
minify: config.minify.unwrap_or_default(),
768-
source_map: source_map.as_mut(),
769-
project_root,
770-
targets,
771-
analyze_dependencies: if let Some(d) = &config.analyze_dependencies {
772-
match d {
773-
AnalyzeDependenciesOption::Bool(b) if *b => Some(DependencyOptions { remove_imports: true }),
774-
AnalyzeDependenciesOption::Config(c) => Some(DependencyOptions {
775-
remove_imports: !c.preserve_imports,
776-
}),
777-
_ => None,
778-
}
779-
} else {
780-
None
766+
stylesheet.to_css(
767+
PrinterOptions {
768+
minify: config.minify.unwrap_or_default(),
769+
project_root,
770+
targets,
771+
analyze_dependencies: if let Some(d) = &config.analyze_dependencies {
772+
match d {
773+
AnalyzeDependenciesOption::Bool(b) if *b => Some(DependencyOptions { remove_imports: true }),
774+
AnalyzeDependenciesOption::Config(c) => Some(DependencyOptions {
775+
remove_imports: !c.preserve_imports,
776+
}),
777+
_ => None,
778+
}
779+
} else {
780+
None
781+
},
782+
pseudo_classes: config.pseudo_classes.as_ref().map(|p| p.into()),
781783
},
782-
pseudo_classes: config.pseudo_classes.as_ref().map(|p| p.into()),
783-
})?
784+
source_map.as_mut(),
785+
)?
784786
};
785787

786788
let map = if let Some(mut source_map) = source_map {
@@ -898,24 +900,26 @@ fn compile_bundle<'i, 'o, P: SourceProvider, F: FnOnce(&mut StyleSheet<'i, AtRul
898900
unused_symbols: config.unused_symbols.clone().unwrap_or_default(),
899901
})?;
900902

901-
stylesheet.to_css(PrinterOptions {
902-
minify: config.minify.unwrap_or_default(),
903-
source_map: source_map.as_mut(),
904-
project_root,
905-
targets,
906-
analyze_dependencies: if let Some(d) = &config.analyze_dependencies {
907-
match d {
908-
AnalyzeDependenciesOption::Bool(b) if *b => Some(DependencyOptions { remove_imports: true }),
909-
AnalyzeDependenciesOption::Config(c) => Some(DependencyOptions {
910-
remove_imports: !c.preserve_imports,
911-
}),
912-
_ => None,
913-
}
914-
} else {
915-
None
903+
stylesheet.to_css(
904+
PrinterOptions {
905+
minify: config.minify.unwrap_or_default(),
906+
project_root,
907+
targets,
908+
analyze_dependencies: if let Some(d) = &config.analyze_dependencies {
909+
match d {
910+
AnalyzeDependenciesOption::Bool(b) if *b => Some(DependencyOptions { remove_imports: true }),
911+
AnalyzeDependenciesOption::Config(c) => Some(DependencyOptions {
912+
remove_imports: !c.preserve_imports,
913+
}),
914+
_ => None,
915+
}
916+
} else {
917+
None
918+
},
919+
pseudo_classes: config.pseudo_classes.as_ref().map(|p| p.into()),
916920
},
917-
pseudo_classes: config.pseudo_classes.as_ref().map(|p| p.into()),
918-
})?
921+
source_map.as_mut(),
922+
)?
919923
};
920924

921925
let map = if let Some(source_map) = &mut source_map {
@@ -1022,7 +1026,6 @@ fn compile_attr<'i>(
10221026
});
10231027
attr.to_css(PrinterOptions {
10241028
minify: config.minify,
1025-
source_map: None,
10261029
project_root: None,
10271030
targets,
10281031
analyze_dependencies: if config.analyze_dependencies {

0 commit comments

Comments
 (0)