Repository navigation
Load actor bundles without Bun re-transpiling them (DUR-138) - #465
Merged
oliviersm199 merged 2 commits intoNov 10, 2026
Merged
oliviersm199 merged 2 commits into
oliviersm199 merged 2 commits into
Conversation
Co-authored-by: Cursor <cursoragent@cursor.com>
Bun's bundler marks its output with // @Bun, so Bun loads actor bundles without re-transpiling them, and it handles CommonJS require natively. Actor class names are restored in the generated entry because Bun renames classes that collide with a dependency's names even with keepNames, and CommonJS __dirname and __filename read the bundle's location instead of the build machine's paths. Co-authored-by: Cursor <cursoragent@cursor.com>
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes DUR-138
Problem
Actor hosts run on Bun, and Bun re-transpiles every JavaScript file it loads unless the file starts with
// @bun. Our esbuild actor bundles don't start with it, so every load parses and reprints the whole bundle in memory before running it. That costs several times the bundle's size for a moment.A Mastra chat actor (14.6 MB bundle) peaked about 206 MiB above an idle spare while loading. It was OOM-killed on 256 MiB spare pods.
Change
Actor bundles are now built with Bun's own bundler (
Bun.buildwithtarget: "bun") instead of esbuild. esbuild targets Node and doesn't know about// @bun. Bun's bundler writes it automatically and loads CommonJS throughimport.meta.require, so the bundle needs norequireshim.Three Bun bundler behaviors needed handling:
Chatthat imports a dependency'sChatbecomesChat2. Bun'skeepNamesdoesn't restore the name yet (oven-sh/bun#40503 is open). Actors are registered by class name, so the worker would reject the bundle with "actor entrypoint export Chat must have the same class name".nameto its export name. The compiler already requires the two to match in source.__dirnameand__filename. Bun inlines the build machine's absolute paths for these. They now read the bundle's own location throughdefineand a one-line banner. Bun'sdefinetreatsimport.meta.dirnameas a string, so the values go through identifiers the banner sets.build.resolve()in plugins. Bun doesn't implement it (oven-sh/bun#2771). The local-build plugin now resolves SDK imports withimport-meta-resolve, as the Drizzle adapter plugin already did.Smaller differences:
node:prefix from built-in imports, sonode:cryptobecomescrypto.es2022target no longer applies. The output only ever runs on Bun.filesinstead of esbuild'sstdin.@types/bunisn't added. Its types redefine Node's globals for the whole program, which broke the type-preservation tests and everytypeof fetchparameter.actor-build.tsdeclares only the bundler API it uses. esbuild stays a dependency because build scripts and tests still use it.Results
I used the demo AI SDK chat actor (
aiplus@ai-sdk/*providers) and built it with the deployment compiler three ways. Each bundle was loaded through the host's worker supervisor on Bun 1.4.2 (Linux, Docker) with a cold transpiler cache. Figures are medians of 5 fresh processes, with peak memory measured above a trivial actor.main(esbuild, no// @bun)// @bunbanner (first commit)Bun.build(this PR)For the Mastra chat actor, an earlier run on Bun 1.4.4 measured a 206 MiB peak before and 85 MiB with the esbuild banner. In a separate comparison of the two bundlers on the same code, Bun's bundle peaked 12 MiB lower (80 versus 92 MiB) and was 12.4 MB instead of 14.5 MB.
Verification
require, a relativerequire,__dirnameand__filenamebehave as before and that the bundle starts with// @bun. I dropped itsrequire.resolve("node:fs")check because Bun's bundler rewrites it to"fs".importlines now have no banner import and usecrypto.bun run --bun --cwd sdk teston Bun 1.4.4 gave 377 pass and 1 fail.mainwith Bun 1.4.4 and passes with the pinned 1.4.2.format:checkflagsscripts/check-pr-ticket.mjs,scripts/release.mjsandtests/scripts/release-workflow.test.mjs, which this PR doesn't touch.cargo test -- --ignored).