Skip to content
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Various fixes & improvements

- feat: Sourcemaps now support debug ids (#66) by @loewenheim

## 6.2.3

### Various fixes & improvements
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ unicode-id = "0.3"
if_chain = "1.0.0"
scroll = { version = "0.10.1", features = ["derive"], optional = true }
data-encoding = "2.3.3"
debugid = {version = "0.8.0", features = ["serde"] }

[build-dependencies]
rustc_version = "0.2.3"
Expand Down
9 changes: 9 additions & 0 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::fs;
use std::io::Read;
use std::path::{Path, PathBuf};

use debugid::DebugId;
use url::Url;

use crate::errors::Result;
Expand All @@ -27,6 +28,7 @@ pub struct SourceMapBuilder {
sources: Vec<String>,
source_contents: Vec<Option<String>>,
sources_mapping: Vec<u32>,
debug_id: Option<DebugId>,
}

#[cfg(any(unix, windows, target_os = "redox"))]
Expand Down Expand Up @@ -59,9 +61,15 @@ impl SourceMapBuilder {
sources: vec![],
source_contents: vec![],
sources_mapping: vec![],
debug_id: None,
}
}

/// Sets the debug id for the sourcemap (optional)
pub fn set_debug_id(&mut self, debug_id: Option<DebugId>) {
self.debug_id = debug_id;
}

/// Sets the file for the sourcemap (optional)
pub fn set_file<T: Into<String>>(&mut self, value: Option<T>) {
self.file = value.map(Into::into);
Expand Down Expand Up @@ -285,6 +293,7 @@ impl SourceMapBuilder {

let mut sm = SourceMap::new(self.file, self.tokens, self.names, self.sources, contents);
sm.set_source_root(self.source_root);
sm.set_debug_id(self.debug_id);

sm
}
Expand Down
1 change: 1 addition & 0 deletions src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ pub fn decode_regular(rsm: RawSourceMap) -> Result<SourceMap> {

let mut sm = SourceMap::new(file, tokens, names, sources, rsm.sources_content);
sm.set_source_root(rsm.source_root);
sm.set_debug_id(rsm.debug_id);

Ok(sm)
}
Expand Down
2 changes: 2 additions & 0 deletions src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ impl Encodable for SourceMap {
x_facebook_offsets: None,
x_metro_module_paths: None,
x_facebook_sources: None,
debug_id: self.get_debug_id(),
}
}
}
Expand Down Expand Up @@ -124,6 +125,7 @@ impl Encodable for SourceMapIndex {
x_facebook_offsets: None,
x_metro_module_paths: None,
x_facebook_sources: None,
debug_id: None,
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/jsontypes.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use debugid::DebugId;
use serde::de::IgnoredAny;
use serde::{Deserialize, Serialize};
use serde_json::Value;
Expand Down Expand Up @@ -49,6 +50,8 @@ pub struct RawSourceMap {
pub x_metro_module_paths: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub x_facebook_sources: FacebookSources,
#[serde(skip_serializing_if = "Option::is_none")]
pub debug_id: Option<DebugId>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the type is not #[non_exhaustive], but I believe it is not public so this should be fine.

}

#[derive(Deserialize)]
Expand Down
46 changes: 46 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ use crate::hermes::SourceMapHermes;
use crate::sourceview::SourceView;
use crate::utils::{find_common_prefix, greatest_lower_bound};

use debugid::DebugId;

/// Controls the `SourceMap::rewrite` behavior
///
/// Default configuration:
///
/// * `with_names`: true
/// * `with_source_contents`: true
/// * `load_local_source_contents`: false
#[derive(Debug, Clone)]
pub struct RewriteOptions<'a> {
/// If enabled, names are kept in the rewritten sourcemap.
pub with_names: bool,
Expand Down Expand Up @@ -461,6 +464,7 @@ pub struct SourceMap {
source_root: Option<String>,
sources: Vec<String>,
sources_content: Vec<Option<SourceView<'static>>>,
debug_id: Option<DebugId>,
}

impl SourceMap {
Expand Down Expand Up @@ -569,9 +573,20 @@ impl SourceMap {
.into_iter()
.map(|opt| opt.map(SourceView::from_string))
.collect(),
debug_id: None,
}
}

/// Returns the embedded debug id.
pub fn get_debug_id(&self) -> Option<DebugId> {
self.debug_id
}

/// Sets a new value for the debug id.
pub fn set_debug_id(&mut self, debug_id: Option<DebugId>) {
self.debug_id = debug_id
}

/// Returns the embedded filename in case there is one.
pub fn get_file(&self) -> Option<&str> {
self.file.as_deref()
Expand Down Expand Up @@ -766,6 +781,7 @@ impl SourceMap {
options: &RewriteOptions<'_>,
) -> Result<(SourceMap, Vec<u32>)> {
let mut builder = SourceMapBuilder::new(self.get_file());
builder.set_debug_id(self.debug_id);

for token in self.tokens() {
let raw = builder.add_token(&token, options.with_names);
Expand Down Expand Up @@ -1065,3 +1081,33 @@ impl SourceMapSection {
self.map = sm.map(Box::new);
}
}

#[cfg(test)]
mod tests {
use super::{RewriteOptions, SourceMap};
use debugid::DebugId;

#[test]
fn test_rewrite_debugid() {
let input: &[_] = br#"{
"version":3,
"sources":["coolstuff.js"],
"names":["x","alert"],
"mappings":"AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM",
"debug_id":"00000000-0000-0000-0000-000000000000"
}"#;

let sm = SourceMap::from_slice(input).unwrap();

assert_eq!(sm.debug_id, Some(DebugId::default()));

let new_sm = sm
.rewrite(&RewriteOptions {
with_names: false,
..Default::default()
})
.unwrap();

assert_eq!(new_sm.debug_id, Some(DebugId::default()));
}
}