Skip to content

Commit 0ea028d

Browse files
committed
Expose functionality related to source map using the feature gate
Closes #2
1 parent bf16ed5 commit 0ea028d

File tree

11 files changed

+32
-19
lines changed

11 files changed

+32
-19
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"cSpell.words": [
33
"outro"
4+
],
5+
"rust-analyzer.cargo.features": [
6+
"source_map"
47
]
58
}

Cargo.toml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ description = "manipulate string like wizards"
88
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
99

1010
[dependencies]
11-
glob = "0.3.1"
1211
index_vec = { version = "0.1.3" }
1312
rustc-hash = { version = "1.1.0" }
14-
vlq = "0.5.1"
15-
serde = {version = "1.0", features = ["derive"]}
16-
serde_json = "1.0"
13+
serde = { version = "1.0", features = ["derive"], optional = true }
14+
serde_json = { version = "1.0", optional = true }
15+
16+
[features]
17+
# Enable source map functionality
18+
source_map = ["serde", "serde_json"]
1719

1820
[dev-dependencies]
1921
glob = "0.3.1"

examples/source_map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fn main() {
55
let mut s = MagicString::new(demo);
66

77
let update_options = UpdateOptions {
8-
store_name: true,
8+
keep_original: true,
99
..Default::default()
1010
};
1111
s.prepend("import React from 'react';\n")

src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1+
mod basic_types;
12
mod chunk;
2-
mod decoded_map;
33
mod joiner;
44
mod locator;
55
mod magic_string;
6-
mod mappings;
6+
#[cfg(feature = "source_map")]
77
mod source_map;
88
mod span;
9-
mod basic_types;
109

1110
type CowStr<'text> = BasicCowStr<'text>;
1211

@@ -16,7 +15,8 @@ use basic_types::BasicCowStr;
1615

1716
pub use crate::{
1817
joiner::{Joiner, JoinerOptions},
19-
magic_string::{
20-
mutation::UpdateOptions, source_map::SourceMapOptions, MagicString, MagicStringOptions,
21-
},
18+
magic_string::{mutation::UpdateOptions, MagicString, MagicStringOptions},
2219
};
20+
21+
#[cfg(feature = "source_map")]
22+
pub use crate::magic_string::source_map::SourceMapOptions;

src/magic_string/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod mutation;
2+
#[cfg(feature = "source_map")]
23
pub mod source_map;
34

45
use std::collections::VecDeque;
@@ -22,7 +23,6 @@ pub struct MagicString<'s> {
2223
intro: VecDeque<CowStr<'s>>,
2324
outro: VecDeque<CowStr<'s>>,
2425

25-
stored_names: Vec<Span>,
2626
source: CowStr<'s>,
2727
chunks: ChunkVec<'s>,
2828
first_chunk_idx: ChunkIdx,
@@ -54,7 +54,6 @@ impl<'s> MagicString<'s> {
5454
chunk_by_end: Default::default(),
5555
// setup options
5656
filename: options.filename,
57-
stored_names: Default::default(),
5857
};
5958

6059
magic_string.chunk_by_start.insert(0, initial_chunk_idx);

src/magic_string/mutation.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use crate::{chunk::EditOptions, CowStr, MagicString};
22

33
#[derive(Debug, Default, Clone)]
44
pub struct UpdateOptions {
5-
pub store_name: bool,
5+
/// `true` will store the original content in the `name` field of the generated sourcemap.
6+
pub keep_original: bool,
67

78
/// `true` will clear the `intro` and `outro` for the corresponding range.
89
pub overwrite: bool,
@@ -36,7 +37,7 @@ impl<'text> MagicString<'text> {
3637
end,
3738
"".into(),
3839
UpdateOptions {
39-
store_name: false,
40+
keep_original: false,
4041
overwrite: true,
4142
},
4243
false,
@@ -74,7 +75,7 @@ impl<'text> MagicString<'text> {
7475
content.into(),
7576
EditOptions {
7677
overwrite: opts.overwrite,
77-
store_name: opts.store_name,
78+
store_name: opts.keep_original,
7879
},
7980
);
8081

src/magic_string/source_map.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::{
2-
decoded_map::DecodedMap, locator::Locator, mappings::Mappings, source_map::SourceMap,
2+
locator::Locator,
3+
source_map::{decoded_map::DecodedMap, mappings::Mappings, SourceMap},
34
MagicString,
45
};
56

src/decoded_map.rs renamed to src/source_map/decoded_map.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::{mappings::Mappings, source_map::SourceMap};
1+
use crate::source_map::SourceMap;
2+
3+
use super::mappings::Mappings;
24

35
#[derive(Debug)]
46
pub struct DecodedMap {
File renamed without changes.

src/source_map/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
// Notice that source map is designed based on utf-16 index, while rust [String] is based on utf-8 index.
2+
3+
pub mod decoded_map;
4+
pub mod mappings;
15
use serde::Serialize;
6+
27
#[derive(Debug, Serialize)]
38
#[serde(rename_all = "camelCase")]
49
pub struct SourceMap {

0 commit comments

Comments
 (0)