Skip to content

Commit

Permalink
feat: add ChunkRenderReturn struct to make code clear
Browse files Browse the repository at this point in the history
  • Loading branch information
hyf0 committed Mar 7, 2024
1 parent b710be6 commit 87f02c2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
18 changes: 13 additions & 5 deletions crates/rolldown/src/chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ pub struct Chunk {
pub exports_to_other_chunks: FxHashMap<SymbolRef, Rstr>,
}

pub struct ChunkRenderReturn {
pub code: String,
pub map: Option<SourceMap>,
pub rendered_modules: FxHashMap<String, RenderedModule>,
}

impl Chunk {
pub fn new(
name: Option<String>,
Expand Down Expand Up @@ -74,7 +80,7 @@ impl Chunk {
graph: &LinkStageOutput,
chunk_graph: &ChunkGraph,
output_options: &OutputOptions,
) -> BatchedResult<((String, Option<SourceMap>), FxHashMap<String, RenderedModule>)> {
) -> BatchedResult<ChunkRenderReturn> {
use rayon::prelude::*;
let mut rendered_modules = FxHashMap::default();
let mut content_and_sourcemaps = vec![];
Expand Down Expand Up @@ -139,13 +145,15 @@ impl Chunk {
}

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

let (content, map) = concat_sourcemaps(&content_and_sourcemaps)?;
Ok(((content, Some(map)), rendered_modules))

Ok(ChunkRenderReturn { code: content, map: Some(map), rendered_modules })
}
}
8 changes: 4 additions & 4 deletions crates/rolldown/src/stages/bundle_stage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ impl<'a> BundleStage<'a> {
});

let chunks = chunk_graph.chunks.iter().map(|c| {
let ((content, map), rendered_modules) =
let ret =
c.render(self.input_options, self.link_output, &chunk_graph, self.output_options).unwrap();
(
content,
map,
c.get_rendered_chunk_info(self.link_output, self.output_options, rendered_modules),
ret.code,
ret.map,
c.get_rendered_chunk_info(self.link_output, self.output_options, ret.rendered_modules),
)
});

Expand Down

0 comments on commit 87f02c2

Please sign in to comment.