Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
bada17a
bundler: evaluate a wrapped module's dependencies in source order and…
robobun Sep 6, 2026
44fb00d
test: update the libraries.js bytecode snapshot for the dependency or…
robobun Sep 6, 2026
5f7d42a
bundler: shorten the comments on the wrapper prefix and async propaga…
robobun Sep 6, 2026
be90257
bundler: mark __promiseAll live for the __reExport of the first async…
robobun Sep 6, 2026
6904566
bundler: join async dependencies with Promise.all and call import() t…
robobun Sep 6, 2026
46f615f
test: regenerate the async wrapper snapshots for Promise.all and the …
robobun Sep 6, 2026
9f8c400
ci: retrigger
robobun Sep 6, 2026
184b4e0
bundler: discard the value of a joined require or __reExport so Promi…
robobun Sep 6, 2026
d47c137
Merge branch 'main' into robobun/46e46b5b/bundler-wrapper-init-order
robobun Oct 7, 2026
50c815f
test: a CommonJS import before a wrapped ES module import runs first …
robobun Oct 7, 2026
de69ca3
test: update the libraries.js bytecode snapshot for the dependency or…
robobun Oct 7, 2026
479d178
test: expect the import() form of an async module in UsingWithHoistab…
robobun Oct 7, 2026
2e3caf9
test: pin the rejection that ExportStarOfRejectedAsyncDependencyRejec…
robobun Oct 7, 2026
37b107f
bundler: join statements that can throw through a generator so a star…
robobun Oct 7, 2026
4100a21
bundler: optional spaces around the import() retry, and a test for a …
robobun Oct 7, 2026
5402f83
bundler: convert a joined declaration only when every binding is an i…
robobun Oct 7, 2026
990b9e9
bundler: shorten the doc comment of the wrapper prefix
robobun Oct 7, 2026
31cd4dd
bundler: keep the await of one async dependency bare, and skip it for…
robobun Oct 12, 2026
6700468
test: regenerate the async wrapper snapshots for the conditional await
robobun Oct 12, 2026
d104be9
test: build the async import cycle with both source index orders of i…
robobun Oct 12, 2026
ccbc1ab
bundler: shorten the comments of the wrapper prefix
robobun Oct 12, 2026
184d3e6
bundler: one-line comments at the statements the wrapper prefix emits…
robobun Oct 12, 2026
1aefaeb
Merge remote-tracking branch 'origin/main' into robobun/46e46b5b/bund…
robobun Oct 12, 2026
fc0581c
Merge remote-tracking branch 'origin/main' into robobun/46e46b5b/bund…
robobun Oct 23, 2026
09d9c21
test: pin the libraries.js bundle and bytecode hashes of the tree mer…
robobun Oct 23, 2026
dba3c27
Merge remote-tracking branch 'origin/main' into robobun/46e46b5b/bund…
robobun Oct 26, 2026
f117e32
test: pin the libraries.js bytecode hash of the tree merged with main
robobun Oct 26, 2026
49b0642
bundler: start an import()ed async module after the caller's synchron…
robobun Oct 26, 2026
587e1d7
Merge remote-tracking branch 'origin/main' into robobun/46e46b5b/bund…
robobun Oct 27, 2026
b41fec6
bundler: await an async dependency with a bare await, and drop the as…
robobun Oct 27, 2026
a3a5113
test: pin the libraries.js bundle and bytecode hashes of the tree mer…
robobun Oct 27, 2026
2c90d79
bundler: print shorter forms for the rejection handler and the import…
robobun Oct 27, 2026
3c299f9
test: import() cases for a caller in an import cycle and for an impor…
robobun Oct 27, 2026
8771df1
Merge remote-tracking branch 'origin/main' into robobun/46e46b5b/bund…
robobun Oct 28, 2026
41ba934
bundler: keep main's prefix layout after the first async dependency
robobun Oct 28, 2026
2e5aedc
test: re-cut the order cases, pin the known losses, and add an unspli…
robobun Oct 28, 2026
2fb1b77
test: a todo case for the importer of an async import cycle member
robobun Oct 28, 2026
75c5638
test: pin an import() in an export * dependency that runs before the …
robobun Oct 28, 2026
38ddf82
bundler: leave an export star of an async module as on main
robobun Oct 28, 2026
c347be9
bundler: print main's prefix layout from an async export star on
robobun Oct 28, 2026
87491b1
Merge remote-tracking branch 'origin/main' into robobun/46e46b5b/bund…
robobun Oct 29, 2026
612deb6
bundler: keep source order only in a wrapper prefix with no async dep…
robobun Oct 29, 2026
9eda6aa
bundler: shorten the doc comment of leave_source_order
robobun Oct 29, 2026
6d64d37
Merge remote-tracking branch 'origin/main' into robobun/46e46b5b/bund…
robobun Oct 29, 2026
076cd9f
test: a plain bundle in code-from-disk now runs its CommonJS import f…
robobun Oct 29, 2026
e3a598a
bundler: test the wrapper prefix layout for every short list, and the…
robobun Oct 29, 2026
392f790
Merge remote-tracking branch 'origin/main' into robobun/46e46b5b/bund…
robobun Oct 29, 2026
ba6d620
test: say when bun runs the sources of the case with a CommonJS file …
robobun Oct 29, 2026
c9eff65
bundler: one line for the comment of the expected layout in the wrapp…
robobun Oct 29, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
290 changes: 131 additions & 159 deletions src/bundler/LinkerContext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2298,15 +2298,11 @@ impl<'a> LinkerContext<'a> {
loc,
);

if other_flags.is_async_or_has_async_dependency {
stmts
.inside_wrapper_prefix
.append_async_dependency(init_call, self.promise_all_runtime_ref)?;
} else {
stmts
.inside_wrapper_prefix
.append_sync_dependency(init_call)?;
}
stmts.inside_wrapper_prefix.append_dependency(
init_call,
other_flags.is_async_or_has_async_dependency,
self.promise_all_runtime_ref,
)?;
}
}

Expand Down Expand Up @@ -3538,29 +3534,8 @@ impl<'a> LinkerContext<'a> {
// This depends on the "__esm" symbol and declares the "init_foo" symbol
// for similar reasons to the CommonJS closure above.

// Count async dependencies to determine if we need __promiseAll
let mut async_import_count: usize = 0;
{
let import_records =
self.graph.ast.items_import_records()[source_index as usize].as_slice();
let meta_flags = self.graph.meta.items_flags();

for record in import_records {
if !record.source_index.is_valid() {
continue;
}
let other_flags = meta_flags[record.source_index.get() as usize];
if other_flags.is_async_or_has_async_dependency {
async_import_count += 1;
if async_import_count >= 2 {
break;
}
}
}
}

let needs_promise_all = async_import_count >= 2;

// The `__promiseAll` that joins the dependencies is a use of the
// parts that hold the import statements (`scan_imports_and_exports`).
Comment thread
robobun marked this conversation as resolved.
Resolved
let esm_parts: &[u32] = if wrapper_ref.is_valid()
&& self.options.output_format != Format::InternalBakeDev
{
Expand All @@ -3569,30 +3544,14 @@ impl<'a> LinkerContext<'a> {
&[]
};

let promise_all_parts: &[u32] = if needs_promise_all
&& wrapper_ref.is_valid()
&& self.options.output_format != Format::InternalBakeDev
{
self.top_level_symbols_to_parts_for_runtime(self.promise_all_runtime_ref)
} else {
&[]
};

// generate a dummy part that depends on the "__esm" and optionally "__promiseAll" symbols
let mut dependencies =
DependencyList::init_capacity(esm_parts.len() + promise_all_parts.len());
// generate a dummy part that depends on the "__esm" symbol
let mut dependencies = DependencyList::init_capacity(esm_parts.len());
for &part in esm_parts {
dependencies.append_assume_capacity(Dependency {
part_index: part,
source_index: bun_ast::Index::RUNTIME,
});
}
for &part in promise_all_parts {
dependencies.append_assume_capacity(Dependency {
part_index: part,
source_index: bun_ast::Index::RUNTIME,
});
}

let mut symbol_uses = PartSymbolUseMap::default();
symbol_uses
Expand Down Expand Up @@ -3626,19 +3585,6 @@ impl<'a> LinkerContext<'a> {
crate::Index::RUNTIME,
)
.expect("OOM");

// Only mark __promiseAll as used if we have multiple async dependencies
if needs_promise_all {
self.graph
.generate_symbol_import_and_use(
source_index,
part_index,
self.promise_all_runtime_ref,
1,
crate::Index::RUNTIME,
)
.expect("OOM");
}
}
}
WrapKind::None => {}
Expand Down Expand Up @@ -5157,157 +5103,183 @@ pub struct StmtList {
pub(crate) all_stmts: Vec<Stmt>,
}

/// The statements that run inside a wrapper before the module body: the
/// `init_x()` calls of its wrapped ESM dependencies, the `require_x()` calls
/// of its CommonJS dependencies, and the `__reExport(...)` calls of its
/// `export *` statements. They stay in source order, because that is the
/// order in which the modules evaluate.
///
/// An async dependency returns a promise. The first one becomes
/// `await init_a()`. Everything that follows it joins that statement as
/// `await __promiseAll([init_a(), init_b(), ns = require_c()])`, so every
/// dependency starts before the wrapper suspends, the way the module graph
/// would evaluate them, and the body runs once all of them have settled.
Comment thread
robobun marked this conversation as resolved.
Resolved
pub struct InsideWrapperPrefix {
pub(crate) stmts: Vec<Stmt>,
pub(crate) sync_dependencies_end: usize,
// if true it will exist at `sync_dependencies_end`
pub(crate) has_async_dependency: bool,
/// Index in `stmts` of the `await` statement, once one exists.
await_index: Option<usize>,
promise_all_ref: Ref,
}

impl InsideWrapperPrefix {
fn init() -> Self {
Self {
stmts: Vec::new(),
sync_dependencies_end: 0,
has_async_dependency: false,
await_index: None,
promise_all_ref: Ref::NONE,
}
}

// deinit → Drop (Vec frees automatically); reset is explicit

pub(crate) fn reset(&mut self) {
self.stmts.clear();
self.sync_dependencies_end = 0;
self.has_async_dependency = false;
self.await_index = None;
}
}

impl InsideWrapperPrefix {
pub(crate) fn append_non_dependency(&mut self, stmt: Stmt) -> Result<(), AllocError> {
self.stmts.push(stmt);
Ok(())
}
let Some(await_index) = self.await_index else {
self.stmts.push(stmt);
return Ok(());
};

pub(crate) fn append_non_dependency_slice(&mut self, stmts: &[Stmt]) -> Result<(), AllocError> {
self.stmts.extend_from_slice(stmts);
match stmt.data {
bun_ast::StmtData::SExpr(s) => self.join_awaited(await_index, s.value),
bun_ast::StmtData::SLocal(local)
if local.decls.iter().all(|decl| {
decl.value.is_some()
&& matches!(decl.binding.data, bun_ast::binding::Data::BIdentifier(_))
}) =>
{
// `var ns = require_x();` becomes `var ns;` before the await and
// `ns = require_x()` in the awaited list.
Comment thread
robobun marked this conversation as resolved.
Resolved
let mut hoisted = G::DeclList::init_capacity(local.decls.len());
for decl in local.decls.iter() {
let bun_ast::binding::Data::BIdentifier(id) = decl.binding.data else {
unreachable!()
};
hoisted.push(G::Decl {
binding: decl.binding,
value: None,
});
self.join_awaited(
await_index,
Expr::assign(
Expr::init_identifier(id.get().r#ref, decl.binding.loc),
decl.value.expect("infallible: checked above"),
),
);
Comment thread
robobun marked this conversation as resolved.
Resolved
}
self.stmts.insert(
await_index,
Stmt::alloc(
S::Local {
decls: hoisted,
..Default::default()
},
stmt.loc,
),
);
self.await_index = Some(await_index + 1);
}
_ => self.stmts.push(stmt),
}
Ok(())
}

fn append_sync_dependency(&mut self, call_expr: Expr) -> Result<(), AllocError> {
self.stmts.insert(
self.sync_dependencies_end,
Stmt::alloc(
S::SExpr {
value: call_expr,
..Default::default()
},
call_expr.loc,
),
);
self.sync_dependencies_end += 1;
pub(crate) fn append_non_dependency_slice(&mut self, stmts: &[Stmt]) -> Result<(), AllocError> {
for &stmt in stmts {
self.append_non_dependency(stmt)?;
}
Ok(())
}

fn append_async_dependency(
/// `init_call` is the `init_x()` of a wrapped ESM dependency. `is_async`
/// is that dependency's `is_async_or_has_async_dependency`.
Comment thread
robobun marked this conversation as resolved.
Resolved
pub(crate) fn append_dependency(
&mut self,
call_expr: Expr,
init_call: Expr,
is_async: bool,
promise_all_ref: Ref,
) -> Result<(), AllocError> {
if !self.has_async_dependency {
self.has_async_dependency = true;
self.stmts.insert(
self.sync_dependencies_end,
Stmt::alloc(
match self.await_index {
Some(await_index) => self.join_awaited(await_index, init_call),
Comment thread
robobun marked this conversation as resolved.
Resolved
None if is_async => {
self.promise_all_ref = promise_all_ref;
self.await_index = Some(self.stmts.len());
self.stmts.push(Stmt::alloc(
S::SExpr {
value: Expr::init(E::Await { value: call_expr }, Loc::EMPTY),
value: Expr::init(E::Await { value: init_call }, Loc::EMPTY),
..Default::default()
},
Loc::EMPTY,
),
);
return Ok(());
));
}
None => self.stmts.push(Stmt::alloc(
S::SExpr {
value: init_call,
..Default::default()
},
init_call.loc,
)),
}
Ok(())
}

// Note: deep AST mutation chain — `s_expr_mut`/`e_await_mut`/
// `e_call_mut`/`e_array_mut` return `Option`; `.unwrap()` panics on
// shape mismatch.
let mut first_dep_call_expr = self.stmts[self.sync_dependencies_end]
/// Appends `expr` to the list the statement at `await_index` awaits:
/// `await init_a()` becomes `await __promiseAll([init_a(), expr])`, and an
/// existing `__promiseAll` list grows by one element.
Comment thread
robobun marked this conversation as resolved.
Resolved
fn join_awaited(&mut self, await_index: usize, expr: Expr) {
let awaited: &mut Expr = &mut self.stmts[await_index]
.data
.s_expr_mut()
.unwrap()
.expect("infallible: the await statement is an expression statement")
.value
.data
.e_await_mut()
.expect("infallible: variant checked")
.expect("infallible: the statement awaits its dependencies")
.value;
let call = first_dep_call_expr

let call = awaited
.data
.e_call_mut()
.expect("infallible: variant checked");

.expect("infallible: the awaited expression is a call");
if call
.target
.data
.e_identifier()
.expect("infallible: variant checked")
.ref_
.eql(promise_all_ref)
.is_some_and(|id| id.ref_.eql(self.promise_all_ref))
{
// `await __promiseAll` already in place, append to the array argument
call.args
.mut_(0)
.data
.e_array_mut()
.expect("infallible: variant checked")
.expect("infallible: __promiseAll takes an array")
.items
.push(call_expr);
} else {
// convert single `await init_` to `await __promiseAll([init_1(), init_2()])`

let promise_all = Expr::init(
E::Identifier {
ref_: promise_all_ref,
..Default::default()
},
Loc::EMPTY,
);

let mut items = bun_ast::ExprNodeList::init_capacity(2);
items.append_slice_assume_capacity(&[first_dep_call_expr, call_expr]);
.push(expr);
return;
}

let mut args = bun_ast::ExprNodeList::init_capacity(1);
args.append_assume_capacity(Expr::init(
E::Array {
items,
..Default::default()
},
Loc::EMPTY,
));
let mut items = bun_ast::ExprNodeList::init_capacity(2);
items.append_slice_assume_capacity(&[*awaited, expr]);

let promise_all_call = Expr::init(
E::Call {
target: promise_all,
args,
..Default::default()
},
Loc::EMPTY,
);
let mut args = bun_ast::ExprNodeList::init_capacity(1);
args.append_assume_capacity(Expr::init(
E::Array {
items,
..Default::default()
},
Loc::EMPTY,
));

// replace the `await init_` expr with `await __promiseAll`
self.stmts[self.sync_dependencies_end] = Stmt::alloc(
S::SExpr {
value: Expr::init(
E::Await {
value: promise_all_call,
},
Loc::EMPTY,
),
..Default::default()
},
Loc::EMPTY,
);
}
Ok(())
*awaited = Expr::init(
E::Call {
target: Expr::init_identifier(self.promise_all_ref, Loc::EMPTY),
args,
..Default::default()
},
Loc::EMPTY,
);
}
}

Expand Down
Loading
Loading