Skip to content

Commit 24e7c7e

Browse files
authored
refactor: using sourcemap as sourcemap builder (#11)
* refactor: using sourcemap as sourcemap builder * refactor: update Cargo.tom; * chore: update version * chore: update version
1 parent 00ff3ef commit 24e7c7e

File tree

10 files changed

+129
-295
lines changed

10 files changed

+129
-295
lines changed

Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ description = "manipulate string like wizards"
1111
index_vec = { version = "0.1.3" }
1212
rustc-hash = { version = "1.1.0" }
1313
# regex = "1.10.2"
14-
serde = { version = "1.0", features = ["derive"], optional = true }
15-
serde_json = { version = "1.0", optional = true }
1614
once_cell = "1.18.0"
15+
sourcemap = { version = "7.1.1", optional = true }
1716

1817
[features]
1918
# Enable source map functionality
20-
source_map = ["serde", "serde_json"]
19+
source_map = ["sourcemap"]
2120

2221
[dev-dependencies]
2322
glob = "0.3.1"

examples/source_map.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ fn main() {
1717
include_content: true,
1818
});
1919

20-
std::fs::write("./demo.map.json", sm.to_string()).unwrap();
20+
std::fs::write("./demo.map.json", {
21+
let mut buf = vec![];
22+
sm.to_writer(&mut buf).unwrap();
23+
String::from_utf8(buf).unwrap()
24+
}).unwrap();
2125
std::fs::write("./demo.jsx", s.to_string()).unwrap();
2226

2327
println!("{:#?}", s.to_string());

src/basic_types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{fmt::Debug};
1+
use std::fmt::Debug;
22

33
// This is basically doing the same thing as `TryInto<u32>`.
44
// If we use `TryInto<u32>`, we need to put `where <T as TryInto<u32>>::Error: Debug` everywhere.

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ mod joiner;
44
mod locator;
55
mod magic_string;
66
#[cfg(feature = "source_map")]
7-
mod source_map;
7+
mod sourcemap_builder;
88
mod span;
99

1010
type CowStr<'text> = Cow<'text, str>;

src/magic_string/source_map.rs

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
locator::Locator,
3-
source_map::{decoded_map::DecodedMap, mappings::Mappings, SourceMap},
43
MagicString,
4+
sourcemap_builder::SourcemapBuilder,
55
};
66

77
#[derive(Debug, Default)]
@@ -10,60 +10,38 @@ pub struct SourceMapOptions {
1010
}
1111

1212
impl<'s> MagicString<'s> {
13-
fn generate_decoded_source_map(&self, opts: SourceMapOptions) -> DecodedMap {
14-
let mut mappings = Mappings::new();
13+
pub fn source_map(&self, opts: SourceMapOptions) -> sourcemap::SourceMap {
14+
let mut source_builder = SourcemapBuilder::new();
15+
16+
source_builder.set_source("");
17+
if opts.include_content {
18+
source_builder.set_source_contents(&self.source);
19+
}
20+
1521
let locator = Locator::new(&self.source);
1622

1723
self.intro.iter().for_each(|frag| {
18-
mappings.advance(frag);
24+
source_builder.advance(frag);
1925
});
2026

21-
let mut names = vec![];
22-
2327
self.iter_chunks().for_each(|chunk| {
2428
chunk.intro.iter().for_each(|frag| {
25-
mappings.advance(frag);
29+
source_builder.advance(frag);
2630
});
2731

28-
let name_idx = if chunk.keep_in_mappings && chunk.is_edited() {
29-
let original_content = chunk.span.text(&self.source);
30-
31-
let idx = names
32-
.iter()
33-
.enumerate()
34-
.find_map(|(idx, name)| (name == original_content).then_some(idx))
35-
.unwrap_or_else(|| {
36-
let next_idx = names.len();
37-
names.push(original_content.to_string());
38-
next_idx
39-
});
40-
debug_assert!(idx < names.len());
41-
Some(idx as u32)
32+
let name = if chunk.keep_in_mappings && chunk.is_edited() {
33+
Some(chunk.span.text(&self.source))
4234
} else {
4335
None
4436
};
4537

46-
mappings.add_chunk(chunk, &locator, 0, &self.source, name_idx);
38+
source_builder.add_chunk(chunk, &locator, &self.source, name);
4739

4840
chunk.outro.iter().for_each(|frag| {
49-
mappings.advance(frag);
41+
source_builder.advance(frag);
5042
});
5143
});
5244

53-
DecodedMap {
54-
version: 3,
55-
sources: vec!["".to_string()],
56-
sources_content: opts
57-
.include_content
58-
.then(|| vec![self.source.as_ref().to_string()])
59-
.unwrap_or_default(),
60-
mappings,
61-
names,
62-
}
63-
}
64-
65-
pub fn source_map(&self, opts: SourceMapOptions) -> SourceMap {
66-
let decoded_map = self.generate_decoded_source_map(opts);
67-
decoded_map.into_source_map()
45+
source_builder.into_source_map()
6846
}
6947
}

src/source_map/decoded_map.rs

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/source_map/mappings.rs

Lines changed: 0 additions & 202 deletions
This file was deleted.

src/source_map/mod.rs

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)