Skip to content
Open
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ crate-type = ["rlib"]
default = ["bundler", "nodejs", "sourcemap"]
browserslist = ["browserslist-rs"]
bundler = ["dashmap", "custom_sourcemap", "rayon"]
cli = ["clap", "serde_json", "browserslist", "jemallocator"]
cli = ["clap", "serde_json", "browserslist", "jemallocator", "sourcemap"]
jsonschema = ["schemars", "serde", "rspack-lightningcss-selectors/jsonschema"]
nodejs = ["dep:serde", "dep:serde-content"]
serde = [
Expand All @@ -62,9 +62,8 @@ serde = [
"rspack-lightningcss-selectors/serde",
"into_owned",
]
# Keep the existing feature name as a compatibility alias.
sourcemap = ["custom_sourcemap"]
custom_sourcemap = ["parcel_sourcemap"]
sourcemap = ["custom_sourcemap", "dep:parcel_sourcemap"]
custom_sourcemap = ["dep:rustc-hash"]
visitor = []
into_owned = [
"rspack-lightningcss-static-self",
Expand All @@ -89,6 +88,7 @@ lazy_static = "1.4.0"
const-str = "1.1.0"
pathdiff = "0.2.1"
ahash = "0.8.7"
rustc-hash = { version = "2", optional = true }
pastey = "0.1.0"
indexmap = { version = "2.2.6", features = ["serde"] }
# CLI deps
Expand Down
24 changes: 24 additions & 0 deletions benches/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use codspeed_criterion_compat::black_box;
use parcel_sourcemap::SourceMap;
use rspack_lightningcss::stylesheet::{MinifyOptions, ParserOptions, PrinterOptions, StyleSheet};
use std::fmt::Write;

Expand Down Expand Up @@ -45,6 +46,29 @@ pub fn transform_stylesheet(css: &str) {
black_box(result);
}

pub fn transform_stylesheet_with_source_map(css: &str) {
let mut source_map = SourceMap::new("/");
let source_index = source_map.add_source("bench.css");
source_map.set_source_content(source_index as usize, css).unwrap();

let mut stylesheet = StyleSheet::parse(
css,
ParserOptions {
source_index,
..ParserOptions::default()
},
)
.unwrap();
stylesheet.minify(MinifyOptions::default()).unwrap();
let result = stylesheet
.to_css(PrinterOptions {
minify: true,
..PrinterOptions::default().with_source_map(&mut source_map)
})
.unwrap();
black_box((result, source_map));
}

fn small_fixture() -> String {
r#"
:root {
Expand Down
9 changes: 8 additions & 1 deletion benches/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,19 @@ fn bench_stylesheet_transform(c: &mut Criterion) {
for fixture in &fixtures {
group.throughput(Throughput::Bytes(fixture.css.len() as u64));
group.bench_with_input(
BenchmarkId::from_parameter(fixture.name),
BenchmarkId::new("no-source-map", fixture.name),
fixture.css.as_str(),
|b, css| {
b.iter(|| common::transform_stylesheet(black_box(css)));
},
);
group.bench_with_input(
BenchmarkId::new("source-map", fixture.name),
fixture.css.as_str(),
|b, css| {
b.iter(|| common::transform_stylesheet_with_source_map(black_box(css)));
},
);
}

group.finish();
Expand Down
35 changes: 18 additions & 17 deletions src/bundler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
//! };
//!
//! let fs = FileProvider::new();
//! let mut bundler = Bundler::new(&fs, None, ParserOptions::default());
//! let mut bundler = Bundler::new(&fs, None::<&mut ()>, ParserOptions::default());
//! let stylesheet = bundler.bundle(Path::new("style.css")).unwrap();
//! ```

Expand All @@ -43,6 +43,7 @@ use crate::{
use crate::{
error::{Error, ParserError},
media_query::MediaList,
printer::SourceMap,
rules::{
import::ImportRule,
media::MediaRule,
Expand All @@ -52,7 +53,6 @@ use crate::{
stylesheet::{ParserOptions, StyleSheet},
};
use dashmap::DashMap;
use parcel_sourcemap::SourceMap;
use rayon::prelude::*;
use std::{
collections::HashSet,
Expand All @@ -63,8 +63,8 @@ use std::{

/// A Bundler combines a CSS file and all imported dependencies together into
/// a single merged style sheet.
pub struct Bundler<'a, 's, P, T: AtRuleParser<'a>> {
source_map: Option<Mutex<&'s mut SourceMap>>,
pub struct Bundler<'a, 's, P, T: AtRuleParser<'a>, S = ()> {
source_map: Option<Mutex<&'s mut S>>,
fs: &'a P,
source_indexes: DashMap<PathBuf, u32>,
stylesheets: Mutex<Vec<BundleStyleSheet<'a, T::AtRule>>>,
Expand Down Expand Up @@ -236,15 +236,15 @@ impl<'i, T: std::error::Error> BundleErrorKind<'i, T> {
}
}

impl<'a, 's, P: SourceProvider> Bundler<'a, 's, P, DefaultAtRuleParser> {
impl<'a, 's, P: SourceProvider, S: SourceMap + Send> Bundler<'a, 's, P, DefaultAtRuleParser, S> {
/// Creates a new Bundler using the given source provider.
/// If a source map is given, the content of each source file included in the bundle will
/// be added accordingly.
pub fn new(
fs: &'a P,
source_map: Option<&'s mut SourceMap>,
source_map: Option<&'s mut S>,
options: ParserOptions<'a>,
) -> Bundler<'a, 's, P, DefaultAtRuleParser> {
) -> Bundler<'a, 's, P, DefaultAtRuleParser, S> {
Bundler {
source_map: source_map.map(Mutex::new),
fs,
Expand All @@ -256,7 +256,8 @@ impl<'a, 's, P: SourceProvider> Bundler<'a, 's, P, DefaultAtRuleParser> {
}
}

impl<'a, 's, P: SourceProvider, T: AtRuleParser<'a> + Clone + Sync + Send> Bundler<'a, 's, P, T>
impl<'a, 's, P: SourceProvider, T: AtRuleParser<'a> + Clone + Sync + Send, S: SourceMap + Send>
Bundler<'a, 's, P, T, S>
where
T::AtRule: Sync + Send + ToCss + Clone,
{
Expand All @@ -265,7 +266,7 @@ where
/// be added accordingly.
pub fn new_with_at_rule_parser(
fs: &'a P,
source_map: Option<&'s mut SourceMap>,
source_map: Option<&'s mut S>,
options: ParserOptions<'a>,
at_rule_parser: &'s mut T,
) -> Self {
Expand Down Expand Up @@ -471,10 +472,10 @@ where
if let Some(source_map) = &self.source_map {
// Only omit the CSS source when the input source map can actually be read.
// Otherwise, the printer needs this index as a fallback for generated mappings.
if stylesheet.source_map(0).is_none() {
if stylesheet.source_map::<S>(0).is_none() {
let mut source_map = source_map.lock().unwrap();
let source_index = source_map.add_source(filename);
let _ = source_map.set_source_content(source_index as usize, code);
source_map.set_source_content(source_index, code);
source_map_source_index = Some(source_index);
}
}
Expand Down Expand Up @@ -969,7 +970,7 @@ mod tests {
);

fn bundle<P: SourceProvider>(fs: P, entry: &str) -> String {
let mut bundler = Bundler::new(&fs, None, ParserOptions::default());
let mut bundler = Bundler::<_, _, ()>::new(&fs, None, ParserOptions::default());
let stylesheet = bundler.bundle(Path::new(entry)).unwrap();
stylesheet.to_css(PrinterOptions::default()).unwrap().code
}
Expand All @@ -988,7 +989,7 @@ mod tests {
project_root: Option<&str>,
pattern: &'static str,
) -> (String, CssModuleExports) {
let mut bundler = Bundler::new(
let mut bundler = Bundler::<_, _, ()>::new(
&fs,
None,
ParserOptions {
Expand All @@ -1012,7 +1013,7 @@ mod tests {
}

fn bundle_custom_media<P: SourceProvider>(fs: P, entry: &str) -> String {
let mut bundler = Bundler::new(
let mut bundler = Bundler::<_, _, ()>::new(
&fs,
None,
ParserOptions {
Expand Down Expand Up @@ -1048,7 +1049,7 @@ mod tests {
entry: &str,
maybe_cb: Option<Box<dyn FnOnce(BundleErrorKind<P::Error>) -> ()>>,
) {
let mut bundler = Bundler::new(&fs, None, ParserOptions::default());
let mut bundler = Bundler::<_, _, ()>::new(&fs, None, ParserOptions::default());
let res = bundler.bundle(Path::new(entry));
match res {
Ok(_) => unreachable!(),
Expand Down Expand Up @@ -2181,6 +2182,7 @@ mod tests {
}

#[test]
#[cfg(feature = "sourcemap")]
fn test_source_map() {
let source = r#".imported {
content: "yay, file support!";
Expand Down Expand Up @@ -2213,9 +2215,8 @@ mod tests {
stylesheet.minify(MinifyOptions::default()).unwrap();
stylesheet
.to_css(PrinterOptions {
source_map: Some(&mut sm),
minify: true,
..PrinterOptions::default()
..PrinterOptions::default().with_source_map(&mut sm)
})
.unwrap();
let map = sm.to_json(None).unwrap();
Expand Down
Loading
Loading