Skip to content

Commit

Permalink
feat: add (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin committed Oct 30, 2023
1 parent 7709cd4 commit ca07f3e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
7 changes: 6 additions & 1 deletion crates/rolldown/src/bundler/graph/linker.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use index_vec::IndexVec;
use oxc::span::Atom;
use rolldown_common::{ExportsKind, ImportKind, LocalOrReExport, ModuleId, StmtInfo, SymbolRef};
use rolldown_common::{
ExportsKind, ImportKind, LocalOrReExport, ModuleId, StmtInfo, SymbolRef, WrapKind,
};
use rustc_hash::FxHashMap;

use super::{graph::Graph, symbols::NamespaceAlias};
Expand All @@ -14,6 +16,7 @@ use crate::bundler::{
pub struct LinkingInfo {
// The symbol for wrapped module
pub wrap_symbol: Option<SymbolRef>,
pub wrap_kind: WrapKind,
pub facade_stmt_infos: Vec<StmtInfo>,
pub resolved_exports: FxHashMap<Atom, SymbolRef>,
pub resolved_star_exports: Vec<ModuleId>,
Expand Down Expand Up @@ -172,6 +175,8 @@ impl<'graph> Linker<'graph> {
// Case esm, eg var init_a = __esm()
match &self.graph.modules[target] {
Module::Normal(module) => {
linking_info.wrap_kind =
if module.exports_kind == ExportsKind::CommonJs { WrapKind::CJS } else { WrapKind::ESM };
module.create_wrap_symbol(linking_info, symbols);

let name = if module.exports_kind == ExportsKind::CommonJs {
Expand Down
12 changes: 5 additions & 7 deletions crates/rolldown/src/bundler/module/normal_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use oxc::{
};
use rolldown_common::{
ExportsKind, ImportRecord, ImportRecordId, LocalOrReExport, ModuleId, ModuleType, NamedImport,
ResourceId, StmtInfo, SymbolRef,
ResourceId, StmtInfo, SymbolRef, WrapKind,
};
use rolldown_oxc::OxcProgram;
use rustc_hash::{FxHashMap, FxHashSet};
Expand Down Expand Up @@ -71,12 +71,10 @@ impl NormalModule {
self_linking_info,
);

if self.exports_kind == ExportsKind::CommonJs {
CommonJsSourceRender::new(ctx).apply();
} else if self_linking_info.wrap_symbol.is_some() {
EsmWrapSourceRender::new(ctx).apply();
} else {
EsmSourceRender::new(ctx).apply();
match &self_linking_info.wrap_kind {
WrapKind::None => EsmSourceRender::new(ctx).apply(),
WrapKind::CJS => CommonJsSourceRender::new(ctx).apply(),
WrapKind::ESM => EsmWrapSourceRender::new(ctx).apply(),
}

source.prepend(format!("// {}\n", self.resource_id.prettify()));
Expand Down
2 changes: 2 additions & 0 deletions crates/rolldown_common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod named_import;
mod raw_path;
mod stmt_info;
mod symbol_ref;
mod wrap_kind;
pub use crate::{
exports_kind::ExportsKind,
import_record::{ImportKind, ImportRecord, ImportRecordId},
Expand All @@ -19,4 +20,5 @@ pub use crate::{
raw_path::RawPath,
stmt_info::StmtInfo,
symbol_ref::SymbolRef,
wrap_kind::WrapKind,
};
7 changes: 7 additions & 0 deletions crates/rolldown_common/src/wrap_kind.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[derive(Debug, Default)]
pub enum WrapKind {
#[default]
None,
CJS,
ESM,
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@
"engines": {
"node": ">=18.18.0"
}
}
}

0 comments on commit ca07f3e

Please sign in to comment.