Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 76a0f3f

Browse files
authored
feat: support OutputChunk#imports and RenderedChunk#imports (#999)
1 parent e3b6d4d commit 76a0f3f

17 files changed

Lines changed: 123 additions & 3 deletions

File tree

crates/rolldown/src/stages/generate_stage/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ impl<'a> GenerateStage<'a> {
173173
modules: rendered_chunk.modules,
174174
exports: rendered_chunk.exports,
175175
module_ids: rendered_chunk.module_ids,
176+
imports: rendered_chunk.imports,
176177
map,
177178
sourcemap_file_name,
178179
})));

crates/rolldown/src/utils/chunk/finalize_chunks.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,18 @@ pub fn finalize_chunks(
9090
},
9191
);
9292

93+
// Replace hash placeholder in `imports`
94+
chunk_graph.chunks.iter().zip(chunks.iter_mut()).par_bridge().for_each(
95+
|(chunk, chunk_render_return)| {
96+
chunk_render_return.rendered_chunk.imports = chunk
97+
.cross_chunk_imports
98+
.iter()
99+
.map(|id| {
100+
chunk_graph.chunks[*id].filename.as_ref().expect("should have file name").to_string()
101+
})
102+
.collect();
103+
},
104+
);
105+
93106
chunks
94107
}

crates/rolldown/src/utils/chunk/mod.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rolldown_common::{
33
};
44
use rustc_hash::FxHashMap;
55

6-
use crate::{stages::link_stage::LinkStageOutput, SharedOptions};
6+
use crate::{chunk_graph::ChunkGraph, stages::link_stage::LinkStageOutput, SharedOptions};
77

88
use self::render_chunk_exports::get_chunk_export_names;
99

@@ -41,6 +41,7 @@ pub fn generate_rendered_chunk(
4141
graph: &LinkStageOutput,
4242
output_options: &SharedOptions,
4343
render_modules: FxHashMap<FilePath, RenderedModule>,
44+
chunk_graph: &ChunkGraph,
4445
) -> RenderedChunk {
4546
let pre_rendered_chunk = generate_pre_rendered_chunk(chunk, graph, output_options);
4647
RenderedChunk {
@@ -55,5 +56,16 @@ pub fn generate_rendered_chunk(
5556
.expect("should have preliminary_filename")
5657
.to_string(),
5758
modules: render_modules,
59+
imports: chunk
60+
.cross_chunk_imports
61+
.iter()
62+
.map(|id| {
63+
chunk_graph.chunks[*id]
64+
.preliminary_filename
65+
.as_ref()
66+
.expect("should have preliminary_filename")
67+
.to_string()
68+
})
69+
.collect(),
5870
}
5971
}

crates/rolldown/src/utils/chunk/render_chunk.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub async fn render_chunk(
7373
}
7474
rendered_modules.insert(module_path, rendered_module);
7575
});
76-
let rendered_chunk = generate_rendered_chunk(this, graph, options, rendered_modules);
76+
let rendered_chunk = generate_rendered_chunk(this, graph, options, rendered_modules, chunk_graph);
7777

7878
// add banner
7979
if let Some(banner) = options.banner.as_ref() {

crates/rolldown_binding/src/types/binding_output_chunk.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ impl BindingOutputChunk {
5757
.collect()
5858
}
5959

60+
#[napi(getter)]
61+
pub fn imports(&self) -> Vec<String> {
62+
self.inner.imports.clone()
63+
}
64+
6065
// OutputChunk
6166
#[napi(getter)]
6267
pub fn code(&self) -> String {

crates/rolldown_binding/src/types/binding_rendered_chunk.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub struct RenderedChunk {
2020
pub file_name: String,
2121
#[serde(skip)]
2222
pub modules: HashMap<String, BindingRenderedModule>,
23+
pub imports: Vec<String>,
2324
}
2425

2526
impl From<rolldown_common::RenderedChunk> for RenderedChunk {
@@ -36,6 +37,7 @@ impl From<rolldown_common::RenderedChunk> for RenderedChunk {
3637
.into_iter()
3738
.map(|(key, value)| (key.to_string(), value.into()))
3839
.collect(),
40+
imports: value.imports,
3941
}
4042
}
4143
}

crates/rolldown_common/src/types/output_chunk.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub struct OutputChunk {
1717
// RenderedChunk
1818
pub file_name: String,
1919
pub modules: FxHashMap<FilePath, RenderedModule>,
20+
pub imports: Vec<String>,
2021
// OutputChunk
2122
pub code: String,
2223
pub map: Option<SourceMap>,

crates/rolldown_common/src/types/rendered_chunk.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ pub struct RenderedChunk {
1313
// RenderedChunk
1414
pub file_name: String,
1515
pub modules: FxHashMap<FilePath, RenderedModule>,
16+
pub imports: Vec<String>,
1617
}

packages/rolldown/src/binding.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export class BindingOutputChunk {
2424
get exports(): Array<string>
2525
get fileName(): string
2626
get modules(): Record<string, BindingRenderedModule>
27+
get imports(): Array<string>
2728
get code(): string
2829
get map(): string | null
2930
get sourcemapFileName(): string | null
@@ -158,5 +159,6 @@ export interface RenderedChunk {
158159
exports: Array<string>
159160
fileName: string
160161
modules: Record<string, BindingRenderedModule>
162+
imports: Array<string>
161163
}
162164

packages/rolldown/src/types/rolldown-output.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export interface RolldownOutputChunk {
3636
modules: {
3737
[id: string]: RenderedModule
3838
}
39+
imports: string[]
3940
facadeModuleId: string | null
4041
isDynamicEntry: boolean
4142
moduleIds: string[]

0 commit comments

Comments
 (0)