Skip to content

Commit

Permalink
feat: add OutputChunk exports (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin authored Nov 13, 2023
1 parent ccf06fe commit 46fe0bf
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 22 deletions.
1 change: 1 addition & 0 deletions crates/rolldown/src/bundler/bundle/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ impl<'a> Bundle<'a> {
.entry_module
.map(|id| self.graph.modules[id].expect_normal().resource_id.prettify().to_string()),
modules: rendered_modules,
exports: c.get_export_names(self.graph, self.output_options),
}))
})
.collect::<Vec<_>>();
Expand Down
1 change: 1 addition & 0 deletions crates/rolldown/src/bundler/bundle/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct OutputChunk {
pub is_entry: bool,
pub facade_module_id: Option<String>,
pub modules: FxHashMap<String, RenderedModule>,
pub exports: Vec<String>,
}

#[derive(Debug)]
Expand Down
60 changes: 42 additions & 18 deletions crates/rolldown/src/bundler/chunk/render_chunk_exports.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use rolldown_common::WrapKind;
use oxc::span::Atom;
use rolldown_common::{SymbolRef, WrapKind};
use string_wizard::MagicString;

use crate::{bundler::graph::graph::Graph, OutputFormat, OutputOptions};
Expand All @@ -23,22 +24,7 @@ impl Chunk {
}
}

let export_items = self.entry_module.map_or_else(
|| {
self
.exports_to_other_chunks
.iter()
.map(|(export_ref, alias)| (alias, *export_ref))
.collect::<Vec<_>>()
},
|entry_module_id| {
let linking_info = &graph.linking_infos[entry_module_id];
linking_info
.sorted_exports()
.map(|(name, export)| (name, export.symbol_ref))
.collect::<Vec<_>>()
},
);
let export_items = self.get_export_items(graph);

if export_items.is_empty() {
return None;
Expand All @@ -55,7 +41,7 @@ impl Chunk {
let property_name = &ns_alias.property_name;
s.append(format!("var {canonical_name} = {canonical_ns_name}.{property_name};\n"));
}
if canonical_name == exported_name {
if canonical_name == &exported_name {
format!("{canonical_name}")
} else {
format!("{canonical_name} as {exported_name}")
Expand All @@ -65,4 +51,42 @@ impl Chunk {
s.append(format!("export {{ {} }};", rendered_items.join(", "),));
Some(s)
}

fn get_export_items(&self, graph: &Graph) -> Vec<(Atom, SymbolRef)> {
self.entry_module.map_or_else(
|| {
self
.exports_to_other_chunks
.iter()
.map(|(export_ref, alias)| (alias.clone(), *export_ref))
.collect::<Vec<_>>()
},
|entry_module_id| {
let linking_info = &graph.linking_infos[entry_module_id];
linking_info
.sorted_exports()
.map(|(name, export)| (name.clone(), export.symbol_ref))
.collect::<Vec<_>>()
},
)
}

pub fn get_export_names(&self, graph: &Graph, output_options: &OutputOptions) -> Vec<String> {
if let Some(entry) = self.entry_module {
let linking_info = &graph.linking_infos[entry];
if matches!(linking_info.wrap_kind, WrapKind::Cjs) {
match output_options.format {
OutputFormat::Esm => {
return vec!["default".to_string()];
}
}
}
}

self
.get_export_items(graph)
.into_iter()
.map(|(exported_name, _)| exported_name.to_string())
.collect::<Vec<_>>()
}
}
3 changes: 2 additions & 1 deletion crates/rolldown_binding/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export interface OutputOptions {
format?: 'esm' | 'cjs'
}
export interface RenderedModule {
code?: string | null
code?: string
removedExports: Array<string>
renderedExports: Array<string>
originalLength: number
Expand All @@ -55,6 +55,7 @@ export interface OutputChunk {
isEntry: boolean
facadeModuleId?: string
modules: Record<string, RenderedModule>
exports: Array<string>
}
export interface OutputAsset {
fileName: string
Expand Down
2 changes: 2 additions & 0 deletions crates/rolldown_binding/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub struct OutputChunk {
pub is_entry: bool,
pub facade_module_id: Option<String>,
pub modules: HashMap<String, RenderedModule>,
pub exports: Vec<String>,
}

impl From<Box<rolldown::OutputChunk>> for OutputChunk {
Expand All @@ -49,6 +50,7 @@ impl From<Box<rolldown::OutputChunk>> for OutputChunk {
is_entry: chunk.is_entry,
facade_module_id: chunk.facade_module_id,
modules: chunk.modules.into_iter().map(|(key, value)| (key, value.into())).collect(),
exports: chunk.exports,
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions packages/node/src/utils/transform-to-rollup-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function transformToRollupOutputChunk(chunk: OutputChunk): RollupOutputChunk {
fileName: chunk.fileName,
// @ts-expect-error undefined can't assign to null
modules: chunk.modules,
exports: chunk.exports,
get dynamicImports() {
return unimplemented()
},
Expand All @@ -32,9 +33,6 @@ function transformToRollupOutputChunk(chunk: OutputChunk): RollupOutputChunk {
get map() {
return unimplemented()
},
get exports() {
return unimplemented()
},
get facadeModuleId() {
return chunk.facadeModuleId || null
},
Expand Down

0 comments on commit 46fe0bf

Please sign in to comment.