Skip to content

Commit 51711b9

Browse files
authored
feat: Skip encoding range mappings if it's empty (#86)
1 parent ce8b140 commit 51711b9

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

src/encoder.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ fn serialize_range_mappings(sm: &SourceMap) -> Option<String> {
6060
let mut buf = Vec::new();
6161
let mut prev_line = 0;
6262
let mut had_rmi = false;
63+
let mut empty = true;
6364

6465
let mut idx_of_first_in_line = 0;
6566

@@ -68,6 +69,7 @@ fn serialize_range_mappings(sm: &SourceMap) -> Option<String> {
6869
for (idx, token) in sm.tokens().enumerate() {
6970
if token.is_range() {
7071
had_rmi = true;
72+
empty = false;
7173

7274
let num = idx - idx_of_first_in_line;
7375

@@ -89,6 +91,10 @@ fn serialize_range_mappings(sm: &SourceMap) -> Option<String> {
8991
idx_of_first_in_line = idx;
9092
}
9193
}
94+
if empty {
95+
return None;
96+
}
97+
9298
if had_rmi {
9399
encode_rmi(&mut buf, &mut rmi_data);
94100
}

tests/test_builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn test_builder_into_sourcemap() {
1212
assert_eq!(sm.get_source(0), Some("/foo/bar/baz.js"));
1313
assert_eq!(sm.get_name(0), Some("x"));
1414

15-
let expected = br#"{"version":3,"sources":["baz.js"],"sourceRoot":"/foo/bar","names":["x"],"rangeMappings":"","mappings":""}"#;
15+
let expected = br#"{"version":3,"sources":["baz.js"],"sourceRoot":"/foo/bar","names":["x"],"mappings":""}"#;
1616
let mut output: Vec<u8> = vec![];
1717
sm.to_writer(&mut output).unwrap();
1818
assert_eq!(output, expected);

tests/test_encoder.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn test_basic_sourcemap() {
2323
fn test_sourcemap_data_url() {
2424
let input: &[_] = br#"{"version":3,"file":"build/foo.min.js","sources":["src/foo.js"],"names":[],"mappings":"AAAA","sourceRoot":"/"}"#;
2525
let sm = SourceMap::from_reader(input).unwrap();
26-
assert_eq!(sm.to_data_url().unwrap(), "data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYnVpbGQvZm9vLm1pbi5qcyIsInNvdXJjZXMiOlsic3JjL2Zvby5qcyJdLCJzb3VyY2VSb290IjoiLyIsIm5hbWVzIjpbXSwicmFuZ2VNYXBwaW5ncyI6IiIsIm1hcHBpbmdzIjoiQUFBQSJ9");
26+
assert_eq!(sm.to_data_url().unwrap(), "data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYnVpbGQvZm9vLm1pbi5qcyIsInNvdXJjZXMiOlsic3JjL2Zvby5qcyJdLCJzb3VyY2VSb290IjoiLyIsIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBIn0=");
2727
}
2828

2929
#[test]
@@ -45,3 +45,20 @@ fn test_basic_range() {
4545
assert_eq!(tok1, tok2);
4646
}
4747
}
48+
49+
#[test]
50+
fn test_empty_range() {
51+
let input = r#"{
52+
"version": 3,
53+
"sources": [null],
54+
"names": ["console","log","ab"],
55+
"mappings": "AACAA,QAAQC,GAAG,CAAC,OAAM,OAAM,QACxBD,QAAQC,GAAG,CAAC,QAEZD,QAAQC,GAAG,CAJD;IAACC,IAAI;AAAI,IAKnBF,QAAQC,GAAG,CAAC,YACZD,QAAQC,GAAG,CAAC",
56+
"rangeMappings": ""
57+
}"#;
58+
let sm = SourceMap::from_reader(input.as_bytes()).unwrap();
59+
let mut out: Vec<u8> = vec![];
60+
sm.to_writer(&mut out).unwrap();
61+
62+
let out = String::from_utf8(out).unwrap();
63+
assert!(!out.contains("rangeMappings"));
64+
}

0 commit comments

Comments
 (0)