Skip to content

Commit

Permalink
Revert "refactor: implement ConcatSource (#611)" (#612)
Browse files Browse the repository at this point in the history
This reverts commit a05eb3d.
  • Loading branch information
hyf0 committed Mar 19, 2024
1 parent a05eb3d commit dc32a76
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 121 deletions.
40 changes: 22 additions & 18 deletions crates/rolldown/src/chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ use rolldown_common::{
};
use rolldown_error::BuildError;
use rolldown_rstr::Rstr;
use rolldown_sourcemap::{
collapse_sourcemaps, ConcatSource, RawSource, SourceMap, SourceMapSource,
};
use rolldown_sourcemap::{collapse_sourcemaps, concat_sourcemaps, SourceMap};
use rolldown_utils::BitSet;
use rustc_hash::FxHashMap;

Expand Down Expand Up @@ -89,11 +87,10 @@ impl Chunk {
) -> BatchedResult<ChunkRenderReturn> {
use rayon::prelude::*;
let mut rendered_modules = FxHashMap::default();
let mut concat_source = ConcatSource::default();
let mut content_and_sourcemaps = vec![];

concat_source.add_source(Box::new(RawSource::new(
self.render_imports_for_esm(graph, chunk_graph).to_string(),
)));
content_and_sourcemaps
.push((self.render_imports_for_esm(graph, chunk_graph).to_string(), None));

self
.modules
Expand Down Expand Up @@ -142,26 +139,33 @@ impl Chunk {
.try_for_each(
|(module_path, rendered_module, rendered_content, map)| -> Result<(), BuildError> {
if let Some(rendered_content) = rendered_content {
if let Some(map) = match map {
None => None,
Some(v) => v?,
} {
concat_source.add_source(Box::new(SourceMapSource::new(rendered_content, map)));
} else {
concat_source.add_source(Box::new(RawSource::new(rendered_content)));
}
content_and_sourcemaps.push((
rendered_content,
match map {
None => None,
Some(v) => v?,
},
));
}
rendered_modules.insert(module_path, rendered_module);
Ok(())
},
)?;

if let Some(exports) = self.render_exports(graph, output_options) {
concat_source.add_source(Box::new(RawSource::new(exports.to_string())));
content_and_sourcemaps.push((exports.to_string(), None));
}

if output_options.sourcemap.is_hidden() {
return Ok(ChunkRenderReturn {
code: content_and_sourcemaps.into_iter().map(|(c, _)| c).collect::<Vec<_>>().join("\n"),
map: None,
rendered_modules,
});
}

let (content, map) = concat_source.content_and_sourcemap();
let (content, map) = concat_sourcemaps(&content_and_sourcemaps)?;

Ok(ChunkRenderReturn { code: content, map, rendered_modules })
Ok(ChunkRenderReturn { code: content, map: Some(map), rendered_modules })
}
}
136 changes: 34 additions & 102 deletions crates/rolldown_sourcemap/src/concat_sourcemap.rs
Original file line number Diff line number Diff line change
@@ -1,71 +1,30 @@
// cSpell:disable
use rolldown_error::BuildError;
use sourcemap::{SourceMap, SourceMapBuilder};

pub trait Source {
fn sourcemap(&self) -> Option<&SourceMap>;
#[allow(clippy::wrong_self_convention)]
fn into_concat_source(
&self,
final_source: &mut String,
sourcemap_builder: &mut Option<SourceMapBuilder>,
);
}

pub struct RawSource {
content: String,
}

impl RawSource {
pub fn new(content: String) -> Self {
Self { content }
}
}

impl Source for RawSource {
fn sourcemap(&self) -> Option<&SourceMap> {
None
}

fn into_concat_source(
&self,
final_source: &mut String,
_sourcemap_builder: &mut Option<SourceMapBuilder>,
) {
final_source.push_str(&self.content);
}
}

pub struct SourceMapSource {
content: String,
sourcemap: SourceMap,
}

impl SourceMapSource {
pub fn new(content: String, sourcemap: SourceMap) -> Self {
Self { content, sourcemap }
}
}

impl Source for SourceMapSource {
fn sourcemap(&self) -> Option<&SourceMap> {
Some(&self.sourcemap)
}
#[allow(clippy::cast_possible_truncation)]
pub fn concat_sourcemaps(
content_and_sourcemaps: &[(String, Option<SourceMap>)],
) -> Result<(String, SourceMap), BuildError> {
let mut s = String::new();
let mut sourcemap_builder = SourceMapBuilder::new(None);
let mut line_offset = 0;

for (index, (content, sourcemap)) in content_and_sourcemaps.iter().enumerate() {
s.push_str(content);
if index < content_and_sourcemaps.len() - 1 {
s.push('\n');
}

#[allow(clippy::cast_possible_truncation)]
fn into_concat_source(
&self,
final_source: &mut String,
sourcemap_builder: &mut Option<SourceMapBuilder>,
) {
if let Some(sourcemap_builder) = sourcemap_builder {
for (index, source) in self.sourcemap.sources().enumerate() {
if let Some(sourcemap) = sourcemap {
for (index, source) in sourcemap.sources().enumerate() {
let source_id = sourcemap_builder.add_source(source);
sourcemap_builder
.set_source_contents(source_id, self.sourcemap.get_source_contents(index as u32));
.set_source_contents(source_id, sourcemap.get_source_contents(index as u32));
}
for token in self.sourcemap.tokens() {
for token in sourcemap.tokens() {
sourcemap_builder.add(
token.get_dst_line() + final_source.lines().count() as u32,
token.get_dst_line() + line_offset,
token.get_dst_col(),
token.get_src_line(),
token.get_src_col(),
Expand All @@ -74,52 +33,18 @@ impl Source for SourceMapSource {
);
}
}

final_source.push_str(&self.content);
}
}

#[derive(Default)]
pub struct ConcatSource {
inner: Vec<Box<dyn Source>>,
enabel_sourcemap: bool,
}

impl ConcatSource {
pub fn add_source(&mut self, source: Box<dyn Source>) {
if source.sourcemap().is_some() {
self.enabel_sourcemap = true;
}
self.inner.push(source);
line_offset += (content.lines().count() + 1) as u32;
}

pub fn content_and_sourcemap(self) -> (String, Option<SourceMap>) {
let mut final_source = String::new();
let mut sourcemap_builder = self.enabel_sourcemap.then_some(SourceMapBuilder::new(None));

for (index, source) in self.inner.iter().enumerate() {
source.into_concat_source(&mut final_source, &mut sourcemap_builder);
if index < self.inner.len() - 1 {
final_source.push('\n');
}
}

(final_source, sourcemap_builder.map(sourcemap::SourceMapBuilder::into_sourcemap))
}
Ok((s, sourcemap_builder.into_sourcemap()))
}

#[cfg(test)]
mod tests {
pub use sourcemap::SourceMap;

use crate::{ConcatSource, RawSource, SourceMapSource};
#[test]
fn concat_sourcemaps_works() {
let mut concat_source = ConcatSource::default();
concat_source.add_source(Box::new(RawSource::new("\nconsole.log()".to_string())));
concat_source.add_source(Box::new(SourceMapSource::new(
"function sayHello(name: string) {\n console.log(`Hello, ${name}`);\n}\n".to_string(),
SourceMap::from_slice(
let map = SourceMap::from_slice(
r#"{
"version":3,
"sourceRoot":"",
Expand All @@ -129,21 +54,28 @@ mod tests {
"names":[]
}"#.as_bytes()
)
.unwrap(),
)));
.unwrap();
let content_and_sourcemaps = vec![
("\nconsole.log()".to_string(), None),
(
"function sayHello(name: string) {\n console.log(`Hello, ${name}`);\n}\n".to_string(),
Some(map),
),
];

let (content, map) = {
let (content, map) = concat_source.content_and_sourcemap();
let (content, map) =
super::concat_sourcemaps(&content_and_sourcemaps).expect("should not fail");
let mut buf = vec![];
map.expect("should have sourcemap").to_writer(&mut buf).unwrap();
map.to_writer(&mut buf).unwrap();
(content, unsafe { String::from_utf8_unchecked(buf) })
};

assert_eq!(
content,
"\nconsole.log()\nfunction sayHello(name: string) {\n console.log(`Hello, ${name}`);\n}\n"
);
let expected = "{\"version\":3,\"sources\":[\"index.ts\"],\"sourcesContent\":[\"function sayHello(name: string) {\\n console.log(`Hello, ${name}`);\\n}\\n\"],\"names\":[],\"mappings\":\";;AAAA,SAAS,QAAQ,CAAC,IAAY;IAC5B,OAAO,CAAC,GAAG,CAAC,iBAAU,IAAI,CAAE,CAAC,CAAC;AAChC,CAAC\"}";
let expected = "{\"version\":3,\"sources\":[\"index.ts\"],\"sourcesContent\":[\"function sayHello(name: string) {\\n console.log(`Hello, ${name}`);\\n}\\n\"],\"names\":[],\"mappings\":\";;;AAAA,SAAS,QAAQ,CAAC,IAAY;IAC5B,OAAO,CAAC,GAAG,CAAC,iBAAU,IAAI,CAAE,CAAC,CAAC;AAChC,CAAC\"}";
assert_eq!(map, expected);
}
}
2 changes: 1 addition & 1 deletion crates/rolldown_sourcemap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pub use sourcemap::{SourceMap, SourceMapBuilder};
mod concat_sourcemap;

pub use concat_sourcemap::{ConcatSource, RawSource, SourceMapSource};
pub use concat_sourcemap::concat_sourcemaps;
use rolldown_error::BuildError;

pub fn collapse_sourcemaps(
Expand Down

0 comments on commit dc32a76

Please sign in to comment.