Repository navigation
fix(comp): a module exports its own defs, so a page ships what it uses - #2224
Merged
Lorenzobattistela merged 2 commits intoNov 23, 2026
Merged
Conversation
In module mode (the bun plugin, `bend file.bend -o f.mjs`), js_lib exported every live def of the whole book, imported files included, under keys like "lib:unused". Every def was a root, so a page that used one def of a large file shipped all of it, and the guide's "an ES module of its non-IO defs" did not hold. Imported defs now stay internal; the existing reachability pass keeps only what the module's own defs reach. comp.ts stays under its cap: `if (mod) FL = null!;` drops a brace block. tests/io/bundle_own_exports builds a page whose module imports a file with an unused def, and checks the bundle leaves it out and runs.
…ment A module now exports only its own defs, so module_export_keys checks that the imported dep:value stays internal while the root dep.value and the prototype-named keys are still exported. The js_lib comment says the same in one line. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Lorenzobattistela
approved these changes
Nov 23, 2026
nicolas-abril
added a commit
to nicolas-abril/bend
that referenced
this pull request
Nov 24, 2026
, bendlang#2224-bendlang#2226, bendlang#2231, bendlang#2285, bendlang#2286, bendlang#2288 and others) One conflict, in emit_unfold: bendlang#2288 annotates an unfolded call's lets with the argument types instantiated by the earlier arguments; bendlang#2162's deep unfold keeps its arguments unannotated there and annotates them the same way after folding (its type walk renamed D, beside bendlang#2288's T). Both kept. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.
Problem
In module mode (the bun plugin in
bend2/main.ts, andbend file.bend -o f.mjs),js_libexports every live, non-IO def of the whole book, the defs of imported files included (keys like"lib:unused"). Thenfile_bookcompiles everything reachable from all of them, and the module exports oneexport default { ... }object, which bundlers cannot shake.So a page that uses one def of a large imported file ships all of that file. The guide also says
-o f.mjsemits "an ES module of its non-IO defs", but the command exports the imported file's defs too.We hit this in a game engine written in Bend (opengameworld/engine). A module that uses one clip of a generated 42-clip animation library:
bun --hot/dev pays per change)Change
A module exports only its own defs. This uses the existing convention:
bend.tssaysns:nameis internal, andcli_verdictalready reads!k.includes(":")as "a def of this file". The reachability pass that already exists then keeps only what those exports reach. The whole-program build (-o x.js, rooted atmain) is unchanged.comp.tsmust stay under its 66k cap. On current main it is at 65,995 tokens (cl100k_base, the encodingttok -m gpt-3.5-turbouses, counted with js-tiktoken). The new condition costs 7 tokens, soif (mod) { FL = null!; }in the same function becomesif (mod) FL = null!;, a formcomp.tsalready uses. The result is 65,998.Test
tests/io/bundle_own_exportsfollowsbundle_inline. Its effect builds a page whose module imports a file with an unused def, then checks three things:pick.It prints
2on current main and0with this change. I ran it interpreted and compiled to JS (-o t.js, then bun).bundle_inlineandbundle_page_inputstill print0in both lanes.Behavior that changes
R["lib:used"](). Those keys contain:, so dot access never reached them. A module that wants to expose an imported def re-exports it with one line:def used() -> U32: L.used(). A GitHub code search found 5 public files that import.bendmodules, and none of them read such a key.def later() -> Unit -> IO(U32)is exported, becauseio_basedoes not see it as IO. It drags its effect file into the page, and that file's top-level code runs at load. With this change, an effect file that nothing reachable uses is not loaded. Base'seffs/*.jsonly callio_eff(...)at load time, so they are unaffected.Evidence
The full evidence, with reproducible harnesses, is at https://github.com/caiodomingues/poc-bend-treeshake. It was gathered on v2.0.40.
tests/corpus, differential in module mode. 1,852 files, 1,237 of them compiled and loaded, 4,605 zero-argument values compared: 0 mismatches. The patch drops 288 imported-def exports.Place.word,new,__proto__,default,then,__esModule, and effects: same output, except the two cases above.An alternative, not in this PR
Keeping the default object as it is and adding a pure named export per own def (under its exact Bend name, as an ES2022 string export name;
defaultandthenexcluded) breaks nothing for existing importers. It letsimport { x }drop even a file's own unused defs. But it costs about 206 tokens, which putscomp.tsover the cap, and it does not speed up compiling. The patch and its tests are in the repo linked above, if that direction is preferred.Not tested here
bundle_inline.gates/test.ts,gates/perf.ts).ttokitself. The count above uses the same encoding through js-tiktoken. With 2 tokens of room, the repo gate on this PR is the real check.