Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions scripts/build/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,11 @@ function emitNodeFallbacks({ n, cfg, sources, o, dirStamp }: Ctx): void {
const installStamp = emitBunInstall(n, cfg, sourceDir);

const outDir = resolve(cfg.codegenDir, "node-fallbacks");
// One output per source, same basename.
const outputs = sources.nodeFallbacks.map(s => resolve(outDir, basename(s)));
// Two outputs per source: the bundled `.js` (read at runtime by debug
// builds) and its zstd-compressed `.js.zst` twin (embedded by release
// builds — see src/resolver/node_fallbacks.rs).
const jsOutputs = sources.nodeFallbacks.map(s => resolve(outDir, basename(s)));
const outputs = [...jsOutputs, ...jsOutputs.map(o => `${o}.zst`)];

// The script (build-fallbacks.ts) reads its args as [outdir, ...sources]
// but actually ignores the sources — it does readdirSync(".") to discover
Expand Down
5 changes: 5 additions & 0 deletions src/node-fallbacks/build-fallbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ for (let fileIndex = 0; fileIndex < allFiles.length; fileIndex++) {
}

await Bun.write(`${outdir}/${name}`, outfile);
// Release builds embed the zstd-compressed copy (see
// src/resolver/node_fallbacks.rs) so the ~1 MB of polyfill text doesn't
// sit uncompressed in the binary; debug builds keep reading the plain
// `.js` at runtime.
await Bun.write(`${outdir}/${name}.zst`, Bun.zstdCompressSync(Buffer.from(outfile), { level: 19 }));
}),
);
}
Expand Down
1 change: 1 addition & 0 deletions src/resolver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ bun_watcher.workspace = true
bun_url.workspace = true
bun_wyhash.workspace = true
bun_hash.workspace = true
bun_zstd.workspace = true
bumpalo.workspace = true
40 changes: 37 additions & 3 deletions src/resolver/node_fallbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,47 @@ pub(crate) struct FallbackModule {
// `*const fn () string` by defining a nested struct with a `get` fn closing over the
// comptime path. Rust fn pointers cannot close over const-generic `&str` on stable, so
// this is expressed as a macro that expands to a local `fn get()` and yields its pointer.
//
// Release builds (`bun_codegen_embed`) embed the zstd-compressed `<name>.js.zst`
// written by the codegen step (src/node-fallbacks/build-fallbacks.ts) and
// decompress it lazily on first access; these polyfills are only ever read when
// bundling with `--target=browser`, so everything else stops paying ~1 MB of
// .rodata for the plain text. Debug builds keep loading the uncompressed `.js`
// from `BUN_CODEGEN_DIR` at runtime so JS-only edits don't need a native rebuild.
macro_rules! create_source_code_getter {
($code_path:literal) => {{
// `$code_path` is relative to `BUN_CODEGEN_DIR` (codegen output, not
// the source tree). The `cfg(bun_codegen_embed)` split lives inside
// `runtime_embed_file!` itself.
// the source tree).
fn get() -> &'static str {
::bun_core::runtime_embed_file!(Codegen, $code_path)
// `bun_codegen_embed` is set via RUSTFLAGS by scripts/build/rust.ts;
// plain `cargo check` doesn't pass `--check-cfg` for it.
#[allow(unexpected_cfgs)]
let source: &'static str = {
#[cfg(bun_codegen_embed)]
{
static SOURCE: ::bun_core::Once<String> = ::bun_core::Once::new();
SOURCE
.get_or_init(|| {
let compressed: &'static [u8] =
::core::include_bytes!(::core::concat!(
::core::env!("BUN_CODEGEN_DIR"),
"/",
$code_path,
".zst"
));
let bytes = ::bun_zstd::decompress_alloc(compressed)
.expect("embedded node-fallback polyfill: invalid zstd frame");
String::from_utf8(bytes)
.expect("embedded node-fallback polyfill: invalid UTF-8")
})
.as_str()
}
#[cfg(not(bun_codegen_embed))]
{
::bun_core::runtime_embed_file!(Codegen, $code_path)
}
};
source
}
get as fn() -> &'static str
}};
Expand Down
Loading