Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support emitting source map for Joiner #10 #12

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 43 additions & 2 deletions src/joiner.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{MagicString, CowStr};
use crate::{CowStr, MagicString, SourceMapOptions};
use crate::source_map::decoded_map::DecodedMap;
use crate::source_map::mappings::Mappings;
use crate::source_map::SourceMap;

pub struct JoinerOptions {
pub separator: Option<String>,
Expand Down Expand Up @@ -45,6 +48,11 @@ impl<'s> Joiner<'s> {
ret
}

pub fn source_map(&'s self, opts: SourceMapOptions) -> SourceMap {
let decoded_map = self.generate_source_map(opts);
decoded_map.into_source_map()
}

// --- private

fn fragments(&'s self) -> impl Iterator<Item = &'s str> {
Expand All @@ -59,5 +67,38 @@ impl<'s> Joiner<'s> {
iter
}


fn generate_source_map(&'s self, opts: SourceMapOptions) -> DecodedMap {
let mut mappings = Mappings::new();
let mut names = vec![];

let separator = self.separator.as_deref().unwrap_or("\n");

for (index, source) in self.sources.iter().enumerate() {
if index > 0 {
mappings.advance(&separator);
}

let name = source.source_map_to_mapping(&mut mappings);

names.extend(name.iter().cloned());
}

// TODO: need uniqueSources https://github.com/Rich-Harris/magic-string/blob/6f6cd52270fdc8b62b1b94c73a5d19ba37b3d4dd/src/Bundle.js#L156
DecodedMap {
version: 3,
sources: self.sources.iter()
.filter_map(|source| source.filename.clone())
.collect(),
sources_content: self.sources.iter()
.map(|source| {
opts
.include_content
.then(|| source.to_string())
.unwrap_or_default()
})
.collect(),
mappings,
names,
}
}
}
46 changes: 46 additions & 0 deletions src/magic_string/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,52 @@ impl<'s> MagicString<'s> {
}
}

pub fn source_map_to_mapping(&self, mut mappings: &mut Mappings) -> Vec<String> {
let locator = Locator::new(&self.source);

self.intro.iter().for_each(|frag| {
mappings.advance(frag);
});

let mut names = vec![];

self.iter_chunks().for_each(|chunk| {
chunk.intro.iter().for_each(|frag| {
mappings.advance(frag);
});

let original_content = chunk.span.text(&self.source);

if self.filename.is_some() {
let name_idx = if chunk.keep_in_mappings && chunk.is_edited() {
let idx = names
.iter()
.enumerate()
.find_map(|(idx, name)| (name == original_content).then_some(idx))
.unwrap_or_else(|| {
let next_idx = names.len();
names.push(original_content.to_string());
next_idx
});
debug_assert!(idx < names.len());
Some(idx as u32)
} else {
None
};

mappings.add_chunk(chunk, &locator, 0, &self.source, name_idx);
} else {
mappings.advance(original_content);
}

chunk.outro.iter().for_each(|frag| {
mappings.advance(frag);
});
});

names
}

pub fn source_map(&self, opts: SourceMapOptions) -> SourceMap {
let decoded_map = self.generate_decoded_source_map(opts);
decoded_map.into_source_map()
Expand Down
40 changes: 40 additions & 0 deletions tests/joiner_source_map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use string_wizard::{Joiner, JoinerOptions, MagicString, SourceMapOptions};
mod append {
use string_wizard::{MagicStringOptions, UpdateOptions};
use super::*;

#[test]
fn should_append_content() {
let mut j = Joiner::default();

let input = "<div>\n hello, world\n</div>";
let mut s1 = MagicString::with_options(input, MagicStringOptions{
filename: Some("bar.js".to_string()),
});
let update_options = UpdateOptions {
keep_original: true,
..Default::default()
};
s1.update_with(1, 2, "v", update_options.clone())
.update_with(3, 4, "d", update_options.clone())
.update_with(
input.len() - 4,
input.len() - 1,
"h1",
update_options.clone(),
);

let s2 = MagicString::with_options("import React from 'react';\n", MagicStringOptions{
filename: Some("bar.js".to_string()),
});

j.append(s1);
j.append(s2);

let sm = j.source_map(SourceMapOptions {
include_content: true,
});

assert_eq!(sm.mappings, "AAAA,CAACA,CAAC,CAACC,CAAC;AACJ;AACA,EAAEC,EAAG;AAFL;");
}
}