Skip to content

fix(bun-plugins): make the Vue and Hono plugins work as bun:test preloads - #1186

Merged
jonkwheeler merged 18 commits into
tsrx-org:mainfrom
jonkwheeler:fix/bun-runtime-preload
Nov 12, 2026
Merged

jonkwheeler merged 18 commits into
tsrx-org:mainfrom
jonkwheeler:fix/bun-runtime-preload

Conversation

@jonkwheeler

@jonkwheeler jonkwheeler commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #1091; review the commits after 01eae48; merge after #1091.

Summary

The Bun plugins now work as bun:test preloads, that is, Bun.plugin(...) in a --preload file.

Under Bun.plugin() registration, Bun calls only the first onLoad hook whose filter matches. If that hook returns nothing, Bun fails with "onLoad() expects an object returned" (or "Expected module mock to return an object" for an async hook) instead of trying the next hook. Bun.build() falls through to the next hook. The plugins relied on falling through in three places:

  • @tsrx/bun-plugin-vue ([Bug]: bun-plugin-vue can't be used as a bun:test preload ("Expected module mock to return an object") #1151). Before, every test file failed before its first test, even files that import no .tsrx module. The createVaporApp interop hook returned nothing for files it leaves alone and for node_modules.
    • The hook is now registered only for Bun.build(). A preload compiles .tsrx modules and leaves every other module to Bun.
    • Under Bun.plugin(), an async hook that answered for project files would make Bun load them as async modules, and require() of a .cjs, .js or .cts file would throw "require() async module … is unsupported". A sync hook would still break module.exports in .cjs files.
    • The README says to add vaporInteropPlugin to createVaporApp() by hand under a preload, and that Vue preloads cover .tsrx modules exporting plain values (see below).
    • In Bun.build(), the hook's filter now leaves out node_modules with a negative lookahead, which Bun evaluates before calling the hook. Rewritten modules use Bun's default loader, which is jsx for .js.
  • @tsrx/bun-plugin-hono ([Bug]: bun-plugin-hono as a bun:test preload fails to load a styled component: Expected loader to be one of "js", … #1152). Before, a component with a <style> block failed with Expected loader to be one of "js", "jsx", … on Bun 1.3.14, or Cannot find module '@tsrx/bun-plugin-hono-css:…' on 1.3.10. Bun's runtime accepts the css loader only in the file namespace, so the virtual stylesheet now stays there, as in the other four plugins.
  • include/exclude and multiple instances, all five plugins. Under a preload, a .tsrx file left out by exclude failed, as did another instance's stylesheet, so fix(bun-plugins): per-instance CSS, production JSX runtime, inferred root, and preload platform flags #1091's multi-instance split broke there. Instances registered with Bun.plugin() now share state on Symbol.for('@tsrx/bun-plugin.runtime.v1'): the compiled stylesheets, and one loader per instance. The version in the key lets a later change to the state's shape take a new key without breaking older plugins installed alongside. Bun.plugin.clearAll() gives plugins no notice, so instances registered before it stay in the shared list.
    • The instance Bun calls hands a .tsrx file it leaves out to the first registered instance that compiles it. That can be an instance of another TSRX Bun plugin.
    • When no instance compiles the file, the hook returns what Bun loads without a plugin, a module whose default export is the file's path.
    • The stylesheet hook serves any instance's sheet and never returns nothing.
    • Bun.build() keeps its per-instance stylesheets and still lets the next hook take a request.

I didn't make this hook "return the file unchanged" for .tsrx. Bun calls only the first hook, so returning the raw file would stop the instance that includes the file from ever compiling it.

Two limits are outside the plugins. A Vapor .tsrx component still can't be evaluated under bun test, because Vue 3.6.0-beta.12's Node entry has no Vapor exports (Export named 'template' not found), and Bun's runtime doesn't call plugin onResolve for the bare vue specifier. Solid's server build, which Bun's runtime resolves, rejects compiled DOM components with "Client-only API called on the server side". So the Vue and Solid native tests use .tsrx modules that export plain values.

Not fixed here: addVaporInteropToCreateVaporApp skips the rewrite when another named import comes before the vue import. #1101 replaces that regex with a parse and fixes it.

Verification

  • New unit tests in all five plugins:
    • Under Bun.plugin() dispatch (first matching hook only, which must return an object), two instances split the source files and each one's stylesheet loads.
    • A file every instance leaves out loads as the file's path.
    • Vue's unchanged modules come back with Bun's loaders, and its filter leaves out POSIX and Windows node_modules paths.
    • Hono serves another instance's stylesheet under Bun.plugin(), and still leaves it alone under Bun.build().
  • New native tests (real bun test --preload in a temporary fixture):
    • React and Preact: two instances split styled components, and a file left out of both loads as its path.
    • Vue and Solid: the same split with plain-value modules. Vue also runs a plain module plus a .tsrx module importing .js and .ts helpers, and require()s a .cjs, .js and .cts file directly and through createRequire (0 of 3 passed before the interop hook moved to Bun.build()).
    • Hono: a styled component loads; a React instance that leaves a file out hands it to a Hono instance, which compiles it and serves its stylesheet.
    • Each new test failed before its fix with the runtime error above.
    • Solid and Vue get test:native scripts and CI steps.
  • Native suites on Bun 1.3.10 (the workspace's bun package) and 1.3.14: React 9/9, Preact 8/8, Solid 1/1, Vue 3/3, Hono 9/9. Bun vitest projects: 70 passed.
  • Full npx vitest run (before the review fixes): 10037 passed, 82 skipped, 13 failed. All 13 were timeouts in typescript-plugin and tsrx-vue while other suites ran in parallel. Those 3 files pass with --testTimeout=120000 (34 tests). Before this round, the full run had no failures (10041 passed).
  • pnpm typecheck, pnpm changeset:check and prettier are clean.

Related

Fixes #1151
Fixes #1152

🤖 Generated with Claude Code


Note

Medium Risk
Touches module loading for all framework Bun plugins and global shared state at runtime; mis-handling could break builds, tests, or CSS under Bun.plugin(), though behavior is heavily tested.

Overview
All five TSRX Bun plugins now register correctly for bun:test via Bun.plugin() in a --preload script. Under that mode Bun invokes only the first matching onLoad and requires a return value, so the plugins no longer rely on returning undefined to fall through.

Instances share Symbol.for('@tsrx/bun-plugin.runtime.v1'): a global stylesheet map and a per-instance loader list. Excluded .tsrx files are delegated to another registered instance (including cross-plugin, e.g. React → Hono); files no instance owns export the path as default. CSS virtual modules stay in the file namespace so the css loader works at runtime (@tsrx/bun-plugin-hono drops its custom CSS namespace).

@tsrx/bun-plugin-vue limits createVaporApp / vaporInteropPlugin rewriting to Bun.build() only so preloads do not break require() on project CommonJS; README documents manual vaporInteropPlugin for preloads. The build-time interop filter skips node_modules and treats .js as jsx.

CI adds test:native for Vue and Solid; unit and native tests cover preload behavior, multi-instance splits, and styled components.

Reviewed by Cursor Bugbot for commit 2421f3a. Bugbot is set up for automated code reviews on this repo. Configure here.

jonkwheeler and others added 17 commits October 26, 2026 14:17
…t files

Every Bun plugin instance registers hooks for the shared `?tsrx-css&lang.css`
query. The first instance claimed every virtual stylesheet and served an empty
one for files another instance compiled, so builds that split `.tsrx` files
across instances with `include`/`exclude` silently lost the later instance's
styles.

An instance now resolves and loads only the stylesheets it compiled and
returns `undefined` for the rest, so Bun passes them on to the instance that
owns them. The React, Preact, Solid, and Vue plugins share this code. The
virtual CSS stays in the `file` namespace: a custom namespace would make
`Bun.plugin()` preloads reject the `css` loader.

Fixes tsrx-org#940

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The React, Preact, and Hono Bun plugins pre-transformed compiled `.tsrx`
output with a separate `Bun.Transpiler`, which has no production mode and
always emits `jsxDEV` calls from `<source>/jsx-dev-runtime`. React's
production dev runtime leaves `jsxDEV` undefined, so a production React build
succeeded and then threw when a component rendered. The same happened under
`NODE_ENV=production` with a `Bun.plugin()` preload.

The plugins now return the compiled TSX to Bun with a `@jsxImportSource`
pragma, so Bun's own TSX transform picks the development or production
runtime for the build, as it does for `.tsx` files. The pragma keeps the
configured import source and the automatic runtime whatever the project
tsconfig sets. The Hono plugin no longer requires `Bun.Transpiler`. The Solid
and Vue plugins do not emit JSX runtime calls and are unchanged.

Fixes tsrx-org#859

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…thout root

When `Bun.build()` omitted `root`, the Bun plugins searched for the nearest
tsconfig from the working directory. Bun builds from the common ancestor
directory of the entrypoints instead, so a nested project whose tsconfig
selects `ios` built with the parent tsconfig's `web` platform, while Bun
resolved path aliases from the nested tsconfig.

Without `root`, the plugins now search from the entrypoints' common ancestor
directory. An explicit `root` is still used as is, and a relative `tsconfig`
option still resolves from the working directory. All five Bun plugins share
this code.

Fixes tsrx-org#567

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ation

The Bun plugins hand `import.meta.env.platform.*` values to Bun through
`build.config.define`, which only `Bun.build()` provides. Under
`Bun.plugin()`, including the documented `bun:test` preload, a configured
platform still pruned exact `if` guards at compile time, but a flag read as a
value stayed in the output and threw `undefined is not an object` at runtime.

Without a build config, the plugins now replace the flags left in compiled
`.tsrx` output with their values, as the Turbopack integration does. All five
Bun plugins share this code.

Fixes tsrx-org#865

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The entrypoint-derived tsconfig base from the previous commit took the
directory of a virtual `BuildConfig.files` entry, such as `/virtual`, which
holds no tsconfig. Bun builds from the working directory when every
entrypoint is an in-memory file, so the plugins now search from there in that
case, and the working directory's `tsrx.platform` applies again.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The `@jsxImportSource` pragma from the production-runtime fix regressed two
cases against main in the React, Preact, and Hono Bun plugins:

- Under a classic `"jsx": "react"` tsconfig, Bun ignores a lone
  `@jsxImportSource` and emits `React.createElement`, `h(...)`, or
  `jsx(...)` with no import, so components threw when they rendered, in
  builds and in `bun:test` preloads.
- The pragma went above a leading `#!` line, which made the module a syntax
  error.

The plugins now read the `jsx` option of the tsconfig Bun applies: the one
nearest each file for `Bun.build()`, and the working directory's for
`Bun.plugin()`, whose runtime transpiler ignores nested tsconfigs. Under a
classic setting they add `@jsxRuntime automatic`, which gives main's output,
the automatic development runtime. Other settings keep the build-selected
runtime. The pragma now goes after a leading shebang line.

`@tsrx/core/config` exports `resolveTsconfigJsx()` for the lookup, because
the Bun plugins cannot depend on `get-tsconfig` without a lockfile change.

React and Preact get real-Bun `native.test.ts` suites like Hono's, wired into
CI. They cover both runtimes, classic tsconfigs, shebangs, `bun:test`
preloads, and in-memory entrypoints. The READMEs now say that a production
JSX runtime needs an automatic JSX setting.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Under `Bun.plugin()` and `bun:test` preloads, the classic-JSX check
searched parent folders from the working directory, but Bun's runtime
applies only `cwd/tsconfig.json`. A classic tsconfig in a parent folder
therefore made the plugins force the development runtime, and production
React modules threw `jsxDEV is not a function`, while plain `.tsx` files
rendered.

Runtime registration now consults `cwd/tsconfig.json` only when it exists
and otherwise treats the setting as unset. `Bun.build()` keeps the nearest
tsconfig lookup per file.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ation

As a `bun:test` preload, the plugin failed every test file with "Expected
module mock to return an object", including files that import no `.tsrx`
module. The `createVaporApp` interop hook returned nothing for modules it
leaves unchanged and for files in node_modules. `Bun.build()` then loads
the file itself, but `Bun.plugin()` registration requires an object from
every onLoad hook.

The hook now skips node_modules in its filter, which Bun evaluates before
calling it. Without a build config it returns an unchanged module's
source with Bun's default loader, which is `jsx` for `.js` files. The
transformed `.js` case uses that loader too.

The new native test runs a preload over an ordinary module and a `.tsrx`
module. CI runs it like the Hono, React and Preact native tests.

Fixes tsrx-org#1151

Co-Authored-By: Claude Opus 5.5 <[email protected]>
With the plugin as a `bun:test` preload, importing a component with a
`<style>` block failed with `Expected loader to be one of "js", …` (Bun
1.3.14) or "Cannot find module '@tsrx/bun-plugin-hono-css:…'" (Bun
1.3.10). The plugin resolved the virtual stylesheet into its own
namespace, and Bun's runtime accepts the `css` loader only in the `file`
namespace.

The stylesheet now stays in the `file` namespace, as in the React,
Preact, Solid and Vue plugins. The onResolve hook still claims only
stylesheets this instance compiled. The onLoad hook now leaves other
stylesheets to the instance that compiled them instead of serving an
empty sheet.

Fixes tsrx-org#1152

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ugin()

Under `Bun.plugin()` registration, including `bun:test` preloads, Bun
calls only the first onLoad hook whose filter matches. If that hook
returns nothing, Bun fails with "onLoad() expects an object returned"
instead of trying the next hook. The `.tsrx` hook returned nothing for
files left out by `include`/`exclude`. The stylesheet hook returned
nothing for sheets another instance compiled. So `exclude` and setups
with several instances failed at runtime in all five plugins.

Instances registered at runtime now share state on a global symbol that
every TSRX Bun plugin uses: the compiled stylesheets, and one loader per
instance. An instance hands a `.tsrx` file it leaves out to the first
registered instance that compiles it, including instances from another
TSRX plugin. When none does, it returns what Bun loads without a plugin,
a module whose default export is the file's path. Its stylesheet hook
serves any instance's sheet and never returns nothing. `Bun.build()`
keeps per-instance stylesheets and lets the next hook take a request.

New native tests run a preload that splits files between two instances,
leaves one file out of both, and hands a file from a React instance to a
Hono instance. Solid gets a native test file, and CI runs the Solid and
Vue native tests.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Under `Bun.plugin()`, the async interop hook answered for every project
`.js`, `.ts`, `.cjs` and `.cts` file. Bun loads a module an async hook
returns as an async module, and `require()` can't load one. So
`require('./legacy.cjs')`, directly or through `createRequire`, threw
"require() async module … is unsupported" under the preload. A sync hook
would still break `module.exports` in `.cjs` files.

The hook is now registered only for `Bun.build()`. A preload compiles
`.tsrx` modules and leaves every other module to Bun. The README says to
install `vaporInteropPlugin` by hand there. It also says that Vue
preloads cover `.tsrx` modules that export plain values: Vue 3.6's
`node` entry, which Bun's runtime resolves, has no Vapor APIs for a
component with a template.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The Bun plugins share their runtime state on a global symbol. Name it
`@tsrx/bun-plugin.runtime.v1`, so a later change to the state's shape can
take a new key without breaking older plugins installed alongside.
`Bun.plugin.clearAll()` gives plugins no notice, so instances registered
before it stay in the shared list. The comment says so.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
main now has tsrx-org#1091's squash commit; merged against the parent commit this branch has so git doesn't redo that work.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46
@jonkwheeler
jonkwheeler merged commit 1fe10c9 into tsrx-org:main Nov 12, 2026
8 checks passed
jonkwheeler added a commit to jonkwheeler/tsrx that referenced this pull request Nov 12, 2026
main now has tsrx-org#1186's squash commit; merged against the parent commit this branch has so git doesn't redo that work.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant