Skip to content

Commit 2f864d5

Browse files
authored
fix: Correctly handle protocol-only sourceRoot values (#61)
1 parent 0899720 commit 2f864d5

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/decoder.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,12 @@ pub fn decode_regular(rsm: RawSourceMap) -> Result<SourceMap> {
189189

190190
let sources = match rsm.source_root {
191191
Some(ref source_root) if !source_root.is_empty() => {
192-
let source_root = source_root.trim_end_matches('/');
192+
let source_root = if source_root.ends_with('/') {
193+
source_root[..source_root.len() - 1].to_string()
194+
} else {
195+
source_root.clone()
196+
};
197+
193198
sources
194199
.into_iter()
195200
.map(|x| {

tests/test_decoder.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,32 @@ fn test_basic_sourcemap_with_root() {
7272
assert!(iter.next().is_none());
7373
}
7474

75+
#[test]
76+
fn test_basic_sourcemap_with_absolute_uri_root() {
77+
let input: &[_] = b"{
78+
\"version\":3,
79+
\"sources\":[\"coolstuff.js\", \"./evencoolerstuff.js\"],
80+
\"sourceRoot\":\"webpack:///\",
81+
\"names\":[\"x\",\"alert\"],
82+
\"mappings\":\"AAAA,GAAIA,GAAI,EACR,ICAIA,GAAK,EAAG,CACVC,MAAM\"
83+
}";
84+
let sm = SourceMap::from_reader(input).unwrap();
85+
let mut iter = sm.tokens().filter(Token::has_name);
86+
assert_eq!(
87+
iter.next().unwrap().to_tuple(),
88+
("webpack:///coolstuff.js", 0, 4, Some("x"))
89+
);
90+
assert_eq!(
91+
iter.next().unwrap().to_tuple(),
92+
("webpack:///./evencoolerstuff.js", 1, 4, Some("x"))
93+
);
94+
assert_eq!(
95+
iter.next().unwrap().to_tuple(),
96+
("webpack:///./evencoolerstuff.js", 2, 2, Some("alert"))
97+
);
98+
assert!(iter.next().is_none());
99+
}
100+
75101
#[test]
76102
fn test_sourcemap_data_url() {
77103
let url = "data:application/json;base64,\

0 commit comments

Comments
 (0)