Skip to content

Commit f4bf515

Browse files
authored
Add sourcemap feature (parcel-bundler#372)
This commit introduces the "sourcemap" feature. Users that do not need sourcemaps can disable this feature and save roughly 15% on compile times. Related to parcel-bundler#357
1 parent 0445926 commit f4bf515

22 files changed

+42
-5
lines changed

Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@ path = "src/lib.rs"
2727
crate-type = ["rlib"]
2828

2929
[features]
30-
default = ["bundler", "grid", "nodejs"]
30+
default = ["bundler", "grid", "nodejs", "sourcemap"]
3131
browserslist = ["browserslist-rs"]
32-
bundler = ["dashmap", "rayon"]
32+
bundler = ["dashmap", "sourcemap", "rayon"]
3333
cli = ["clap", "serde_json", "browserslist", "jemallocator"]
3434
grid = []
35+
jsonschema = ["schemars", "serde", "parcel_selectors/jsonschema"]
3536
nodejs = ["dep:serde"]
3637
serde = ["dep:serde", "smallvec/serde", "cssparser/serde", "parcel_selectors/serde"]
37-
jsonschema = ["schemars", "serde", "parcel_selectors/jsonschema"]
38+
sourcemap = ["parcel_sourcemap"]
3839
visitor = ["lightningcss-derive"]
3940

4041
[dependencies]
@@ -44,7 +45,7 @@ parcel_selectors = { version = "0.24.9", path = "./selectors" }
4445
itertools = "0.10.1"
4546
smallvec = { version = "1.7.0", features = ["union"] }
4647
bitflags = "1.3.2"
47-
parcel_sourcemap = { version = "2.1.1", features = ["json"] }
48+
parcel_sourcemap = { version = "2.1.1", features = ["json"], optional = true }
4849
data-encoding = "2.3.2"
4950
lazy_static = "1.4.0"
5051
const-str = "0.3.1"

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22754,6 +22754,7 @@ mod tests {
2275422754
}
2275522755

2275622756
#[test]
22757+
#[cfg(feature = "sourcemap")]
2275722758
fn test_input_source_map() {
2275822759
let source = r#".imported {
2275922760
content: "yay, file support!";

src/printer.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::rules::Location;
77
use crate::targets::Browsers;
88
use crate::vendor_prefix::VendorPrefix;
99
use cssparser::{serialize_identifier, serialize_name};
10+
#[cfg(feature = "sourcemap")]
1011
use parcel_sourcemap::{OriginalLocation, SourceMap};
1112

1213
/// Options that control how CSS is serialized to a string.
@@ -15,6 +16,7 @@ pub struct PrinterOptions<'a> {
1516
/// Whether to minify the CSS, i.e. remove white space.
1617
pub minify: bool,
1718
/// An optional reference to a source map to write mappings into.
19+
#[cfg(feature = "sourcemap")]
1820
pub source_map: Option<&'a mut SourceMap>,
1921
/// An optional project root path, used to generate relative paths for sources used in CSS module hashes.
2022
pub project_root: Option<&'a str>,
@@ -62,7 +64,9 @@ pub struct PseudoClasses<'a> {
6264
pub struct Printer<'a, 'b, 'c, W> {
6365
pub(crate) sources: Option<&'c Vec<String>>,
6466
dest: &'a mut W,
67+
#[cfg(feature = "sourcemap")]
6568
pub(crate) source_map: Option<&'a mut SourceMap>,
69+
#[cfg(feature = "sourcemap")]
6670
pub(crate) source_maps: Vec<Option<SourceMap>>,
6771
pub(crate) loc: Location,
6872
indent: u8,
@@ -86,7 +90,9 @@ impl<'a, 'b, 'c, W: std::fmt::Write + Sized> Printer<'a, 'b, 'c, W> {
8690
Printer {
8791
sources: None,
8892
dest,
93+
#[cfg(feature = "sourcemap")]
8994
source_map: options.source_map,
95+
#[cfg(feature = "sourcemap")]
9096
source_maps: Vec::new(),
9197
loc: Location {
9298
source_index: 0,
@@ -209,6 +215,7 @@ impl<'a, 'b, 'c, W: std::fmt::Write + Sized> Printer<'a, 'b, 'c, W> {
209215
}
210216

211217
/// Adds a mapping to the source map, if any.
218+
#[cfg(feature = "sourcemap")]
212219
pub fn add_mapping(&mut self, loc: Location) {
213220
self.loc = loc;
214221

src/rules/container.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ impl<'a, 'i, T: ToCss> ToCssWithContext<'a, 'i, T> for ContainerRule<'i, T> {
7878
where
7979
W: std::fmt::Write,
8080
{
81+
#[cfg(feature = "sourcemap")]
8182
dest.add_mapping(self.loc);
8283
dest.write_str("@container ")?;
8384
if let Some(name) = &self.name {

src/rules/counter_style.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ impl<'i> ToCss for CounterStyleRule<'i> {
3131
where
3232
W: std::fmt::Write,
3333
{
34+
#[cfg(feature = "sourcemap")]
3435
dest.add_mapping(self.loc);
3536
dest.write_str("@counter-style ")?;
3637
self.name.to_css(dest)?;

src/rules/custom_media.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ impl<'i> ToCss for CustomMediaRule<'i> {
3030
where
3131
W: std::fmt::Write,
3232
{
33+
#[cfg(feature = "sourcemap")]
3334
dest.add_mapping(self.loc);
3435
dest.write_str("@custom-media ")?;
3536
self.name.to_css(dest)?;

src/rules/document.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ impl<'i, T: ToCss> ToCss for MozDocumentRule<'i, T> {
3737
where
3838
W: std::fmt::Write,
3939
{
40+
#[cfg(feature = "sourcemap")]
4041
dest.add_mapping(self.loc);
4142
dest.write_str("@-moz-document url-prefix()")?;
4243
dest.whitespace()?;

src/rules/font_face.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ impl<'i> ToCss for FontFaceRule<'i> {
420420
where
421421
W: std::fmt::Write,
422422
{
423+
#[cfg(feature = "sourcemap")]
423424
dest.add_mapping(self.loc);
424425
dest.write_str("@font-face")?;
425426
dest.whitespace()?;

src/rules/font_palette_values.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ impl<'i> ToCss for FontPaletteValuesRule<'i> {
342342
where
343343
W: std::fmt::Write,
344344
{
345+
#[cfg(feature = "sourcemap")]
345346
dest.add_mapping(self.loc);
346347
dest.write_str("@font-palette-values ")?;
347348
self.name.to_css(dest)?;

src/rules/import.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ impl<'i> ToCss for ImportRule<'i> {
4646
None
4747
};
4848

49+
#[cfg(feature = "sourcemap")]
4950
dest.add_mapping(self.loc);
5051
dest.write_str("@import ")?;
5152
if let Some(dep) = dep {

0 commit comments

Comments
 (0)