Skip to content

Commit

Permalink
Add sourcemap feature
Browse files Browse the repository at this point in the history
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
  • Loading branch information
chinedufn committed Dec 29, 2022
1 parent 0445926 commit 5313652
Show file tree
Hide file tree
Showing 22 changed files with 42 additions and 5 deletions.
9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ path = "src/lib.rs"
crate-type = ["rlib"]

[features]
default = ["bundler", "grid", "nodejs"]
default = ["bundler", "grid", "nodejs", "sourcemap"]
browserslist = ["browserslist-rs"]
bundler = ["dashmap", "rayon"]
bundler = ["dashmap", "sourcemap", "rayon"]
cli = ["clap", "serde_json", "browserslist", "jemallocator"]
grid = []
jsonschema = ["schemars", "serde", "parcel_selectors/jsonschema"]
nodejs = ["dep:serde"]
serde = ["dep:serde", "smallvec/serde", "cssparser/serde", "parcel_selectors/serde"]
jsonschema = ["schemars", "serde", "parcel_selectors/jsonschema"]
sourcemap = ["parcel_sourcemap"]
visitor = ["lightningcss-derive"]

[dependencies]
Expand All @@ -44,7 +45,7 @@ parcel_selectors = { version = "0.24.9", path = "./selectors" }
itertools = "0.10.1"
smallvec = { version = "1.7.0", features = ["union"] }
bitflags = "1.3.2"
parcel_sourcemap = { version = "2.1.1", features = ["json"] }
parcel_sourcemap = { version = "2.1.1", features = ["json"], optional = true }
data-encoding = "2.3.2"
lazy_static = "1.4.0"
const-str = "0.3.1"
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22754,6 +22754,7 @@ mod tests {
}

#[test]
#[cfg(feature = "sourcemap")]
fn test_input_source_map() {
let source = r#".imported {
content: "yay, file support!";
Expand Down
7 changes: 7 additions & 0 deletions src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::rules::Location;
use crate::targets::Browsers;
use crate::vendor_prefix::VendorPrefix;
use cssparser::{serialize_identifier, serialize_name};
#[cfg(feature = "sourcemap")]
use parcel_sourcemap::{OriginalLocation, SourceMap};

/// Options that control how CSS is serialized to a string.
Expand All @@ -15,6 +16,7 @@ pub struct PrinterOptions<'a> {
/// Whether to minify the CSS, i.e. remove white space.
pub minify: bool,
/// An optional reference to a source map to write mappings into.
#[cfg(feature = "sourcemap")]
pub source_map: Option<&'a mut SourceMap>,
/// An optional project root path, used to generate relative paths for sources used in CSS module hashes.
pub project_root: Option<&'a str>,
Expand Down Expand Up @@ -62,7 +64,9 @@ pub struct PseudoClasses<'a> {
pub struct Printer<'a, 'b, 'c, W> {
pub(crate) sources: Option<&'c Vec<String>>,
dest: &'a mut W,
#[cfg(feature = "sourcemap")]
pub(crate) source_map: Option<&'a mut SourceMap>,
#[cfg(feature = "sourcemap")]
pub(crate) source_maps: Vec<Option<SourceMap>>,
pub(crate) loc: Location,
indent: u8,
Expand All @@ -86,7 +90,9 @@ impl<'a, 'b, 'c, W: std::fmt::Write + Sized> Printer<'a, 'b, 'c, W> {
Printer {
sources: None,
dest,
#[cfg(feature = "sourcemap")]
source_map: options.source_map,
#[cfg(feature = "sourcemap")]
source_maps: Vec::new(),
loc: Location {
source_index: 0,
Expand Down Expand Up @@ -209,6 +215,7 @@ impl<'a, 'b, 'c, W: std::fmt::Write + Sized> Printer<'a, 'b, 'c, W> {
}

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

Expand Down
1 change: 1 addition & 0 deletions src/rules/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ impl<'a, 'i, T: ToCss> ToCssWithContext<'a, 'i, T> for ContainerRule<'i, T> {
where
W: std::fmt::Write,
{
#[cfg(feature = "sourcemap")]
dest.add_mapping(self.loc);
dest.write_str("@container ")?;
if let Some(name) = &self.name {
Expand Down
1 change: 1 addition & 0 deletions src/rules/counter_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ impl<'i> ToCss for CounterStyleRule<'i> {
where
W: std::fmt::Write,
{
#[cfg(feature = "sourcemap")]
dest.add_mapping(self.loc);
dest.write_str("@counter-style ")?;
self.name.to_css(dest)?;
Expand Down
1 change: 1 addition & 0 deletions src/rules/custom_media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ impl<'i> ToCss for CustomMediaRule<'i> {
where
W: std::fmt::Write,
{
#[cfg(feature = "sourcemap")]
dest.add_mapping(self.loc);
dest.write_str("@custom-media ")?;
self.name.to_css(dest)?;
Expand Down
1 change: 1 addition & 0 deletions src/rules/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ impl<'i, T: ToCss> ToCss for MozDocumentRule<'i, T> {
where
W: std::fmt::Write,
{
#[cfg(feature = "sourcemap")]
dest.add_mapping(self.loc);
dest.write_str("@-moz-document url-prefix()")?;
dest.whitespace()?;
Expand Down
1 change: 1 addition & 0 deletions src/rules/font_face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ impl<'i> ToCss for FontFaceRule<'i> {
where
W: std::fmt::Write,
{
#[cfg(feature = "sourcemap")]
dest.add_mapping(self.loc);
dest.write_str("@font-face")?;
dest.whitespace()?;
Expand Down
1 change: 1 addition & 0 deletions src/rules/font_palette_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ impl<'i> ToCss for FontPaletteValuesRule<'i> {
where
W: std::fmt::Write,
{
#[cfg(feature = "sourcemap")]
dest.add_mapping(self.loc);
dest.write_str("@font-palette-values ")?;
self.name.to_css(dest)?;
Expand Down
1 change: 1 addition & 0 deletions src/rules/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ impl<'i> ToCss for ImportRule<'i> {
None
};

#[cfg(feature = "sourcemap")]
dest.add_mapping(self.loc);
dest.write_str("@import ")?;
if let Some(dep) = dep {
Expand Down
1 change: 1 addition & 0 deletions src/rules/keyframes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ impl<'i> ToCss for KeyframesRule<'i> {
where
W: std::fmt::Write,
{
#[cfg(feature = "sourcemap")]
dest.add_mapping(self.loc);
let mut first_rule = true;
macro_rules! write_prefix {
Expand Down
2 changes: 2 additions & 0 deletions src/rules/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ impl<'i> ToCss for LayerStatementRule<'i> {
where
W: std::fmt::Write,
{
#[cfg(feature = "sourcemap")]
dest.add_mapping(self.loc);
dest.write_str("@layer ")?;
self.names.to_css(dest)?;
Expand Down Expand Up @@ -146,6 +147,7 @@ impl<'a, 'i, T: ToCss> ToCssWithContext<'a, 'i, T> for LayerBlockRule<'i, T> {
where
W: std::fmt::Write,
{
#[cfg(feature = "sourcemap")]
dest.add_mapping(self.loc);
dest.write_str("@layer")?;
if let Some(name) = &self.name {
Expand Down
1 change: 1 addition & 0 deletions src/rules/media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ impl<'a, 'i, T: ToCss> ToCssWithContext<'a, 'i, T> for MediaRule<'i, T> {
return Ok(());
}

#[cfg(feature = "sourcemap")]
dest.add_mapping(self.loc);
dest.write_str("@media ")?;
self.query.to_css(dest)?;
Expand Down
1 change: 1 addition & 0 deletions src/rules/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl<'i> ToCss for NamespaceRule<'i> {
where
W: std::fmt::Write,
{
#[cfg(feature = "sourcemap")]
dest.add_mapping(self.loc);
dest.write_str("@namespace ")?;
if let Some(prefix) = &self.prefix {
Expand Down
1 change: 1 addition & 0 deletions src/rules/nesting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ impl<'a, 'i, T: ToCss> ToCssWithContext<'a, 'i, T> for NestingRule<'i, T> {
where
W: std::fmt::Write,
{
#[cfg(feature = "sourcemap")]
dest.add_mapping(self.loc);
if context.is_none() {
dest.write_str("@nest ")?;
Expand Down
2 changes: 2 additions & 0 deletions src/rules/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ impl<'i> ToCss for PageMarginRule<'i> {
where
W: std::fmt::Write,
{
#[cfg(feature = "sourcemap")]
dest.add_mapping(self.loc);
dest.write_char('@')?;
self.margin_box.to_css(dest)?;
Expand Down Expand Up @@ -211,6 +212,7 @@ impl<'i> ToCss for PageRule<'i> {
where
W: std::fmt::Write,
{
#[cfg(feature = "sourcemap")]
dest.add_mapping(self.loc);
dest.write_str("@page")?;
if let Some(first) = self.selectors.first() {
Expand Down
1 change: 1 addition & 0 deletions src/rules/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ impl<'i> ToCss for PropertyRule<'i> {
where
W: std::fmt::Write,
{
#[cfg(feature = "sourcemap")]
dest.add_mapping(self.loc);
dest.write_str("@property ")?;
self.name.to_css(dest)?;
Expand Down
1 change: 1 addition & 0 deletions src/rules/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ impl<'a, 'i, T: ToCss> StyleRule<'i, T> {
let has_declarations = supports_nesting || len > 0 || self.rules.0.is_empty();

if has_declarations {
#[cfg(feature = "sourcemap")]
dest.add_mapping(self.loc);
self.selectors.to_css_with_context(dest, context)?;
dest.whitespace()?;
Expand Down
1 change: 1 addition & 0 deletions src/rules/supports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ impl<'a, 'i, T: ToCss> ToCssWithContext<'a, 'i, T> for SupportsRule<'i, T> {
where
W: std::fmt::Write,
{
#[cfg(feature = "sourcemap")]
dest.add_mapping(self.loc);
dest.write_str("@supports ")?;
self.condition.to_css(dest)?;
Expand Down
1 change: 1 addition & 0 deletions src/rules/unknown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl<'i> ToCss for UnknownAtRule<'i> {
where
W: std::fmt::Write,
{
#[cfg(feature = "sourcemap")]
dest.add_mapping(self.loc);
dest.write_char('@')?;
dest.write_str(&self.name)?;
Expand Down
1 change: 1 addition & 0 deletions src/rules/viewport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl<'i> ToCss for ViewportRule<'i> {
where
W: std::fmt::Write,
{
#[cfg(feature = "sourcemap")]
dest.add_mapping(self.loc);
dest.write_char('@')?;
self.vendor_prefix.to_css(dest)?;
Expand Down
10 changes: 9 additions & 1 deletion src/stylesheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::traits::ToCss;
#[cfg(feature = "visitor")]
use crate::visitor::{Visit, VisitTypes, Visitor};
use cssparser::{AtRuleParser, Parser, ParserInput, RuleListParser};
#[cfg(feature = "sourcemap")]
use parcel_sourcemap::SourceMap;
use std::collections::{HashMap, HashSet};

Expand Down Expand Up @@ -178,6 +179,7 @@ where
}

/// Returns the inline source map associated with the source at the given index.
#[cfg(feature = "sourcemap")]
pub fn source_map(&self, source_index: usize) -> Option<SourceMap> {
SourceMap::from_data_url("/", self.source_map_url(source_index)?).ok()
}
Expand Down Expand Up @@ -233,7 +235,12 @@ where
let project_root = options.project_root.clone();
let mut printer = Printer::new(&mut dest, options);

printer.sources = Some(&self.sources);
#[cfg(feature = "sourcemap")]
{
printer.sources = Some(&self.sources);
}

#[cfg(feature = "sourcemap")]
if printer.source_map.is_some() {
printer.source_maps = self.sources.iter().enumerate().map(|(i, _)| self.source_map(i)).collect();
}
Expand Down Expand Up @@ -339,6 +346,7 @@ impl<'i> StyleAttribute<'i> {

/// Serializes the style attribute to a CSS string.
pub fn to_css(&self, options: PrinterOptions) -> Result<ToCssResult, PrinterError> {
#[cfg(feature = "sourcemap")]
assert!(
options.source_map.is_none(),
"Source maps are not supported for style attributes"
Expand Down

0 comments on commit 5313652

Please sign in to comment.