Skip to content

fix(react): skip raw-only .tsrx on HMR and reuse authored Suspense and Fragment imports - #1102

Merged
jonkwheeler merged 2 commits into
tsrx-org:mainfrom
jonkwheeler:fix/react-raw-hmr-and-imports
Nov 12, 2026
Merged

jonkwheeler merged 2 commits into
tsrx-org:mainfrom
jonkwheeler:fix/react-raw-hmr-and-imports

Conversation

@jonkwheeler

@jonkwheeler jonkwheeler commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Raw-only .tsrx assets on HMR (#566). Before: the React and Preact Vite plugins compiled every changed .tsrx file in handleHotUpdate to refresh its stylesheet, before they checked whether a stylesheet module existed. A file imported only as ?raw (an incomplete docs example, say) failed to compile, and Vite sent an HMR error instead of the raw-string update. After: the stylesheet module is looked up first, and the file is compiled only when one exists, as the Hono plugin already does. Vue and Solid compile in load, so they were not affected.

Authored Suspense / Fragment imports (#862). Before: the shared JSX import injector in @tsrx/core always added import { Suspense } from '<source>' for @try … @pending and import { Fragment } from '<source>' for a keyed @for with a fragment body. A module that already imported the same name from the same source ended up with two declarations, and the build failed with Identifier `Suspense` has already been declared. After: the injector skips its import when the module already has a value import of that name, with the same local name, from the same source. React, Preact and Hono share this injector. Vue's injector already merged imports this way.

Verification

  • packages/vite-plugin-react/tests/basic.test.js and packages/vite-plugin-preact/tests/basic.test.js: a hot update for a file whose only module is Example.tsrx?raw, with incomplete source, returns the modules unchanged. It threw Unclosed tag '<section>' before the fix.
  • packages/tsrx-react/tests/basic.test.js: an authored Suspense import with @try … @pending, and an authored Fragment import with a keyed fragment @for, each compile to exactly one import of that name. Both had two before the fix.
  • The reproduction scripts from both issues, rerun against this branch: the [Bug]: React Vite HMR compiles raw-only .tsrx assets and rejects incomplete examples #566 HMR script now gets an update for /Example.tsrx?raw with React and Preact, and the [Bug]: React Suspense and Fragment imports collide with generated template imports #862 build-and-render script renders <b>native</b><p>ready</p> and both keyed rows.
  • npx vitest run --project tsrx-react --project tsrx-preact --project tsrx-hono --project vite-plugin-react --project vite-plugin-preact: 2273 passed.
  • On a local branch that merges this PR with the four other PRs in this batch: the full npx vitest run passed 10038 tests. Three content-mapper tests that spawn tsc hit the 5s timeout under machine load (load average about 60) and pass when rerun with a longer timeout. pnpm typecheck is clean, pnpm changeset:check passes, and Prettier is clean on every changed file.

Related

Fixes #566
Fixes #862

🤖 Generated with Claude Code


Note

Low Risk
Targeted compiler and Vite HMR guard changes with new regression tests; no auth, data, or broad API surface changes.

Overview
Fixes two build/HMR edge cases in the React/Preact stack.

Authored Suspense / Fragment imports: @tsrx/core’s JSX import injector now skips adding Suspense or Fragment when the module already has a matching value import from the same source with the same local name. That lets you mix native <Suspense> / <Fragment> with @try … @pending or keyed @for fragment bodies without a duplicate-binding compile error.

?raw-only .tsrx on HMR: The React and Preact Vite plugins look up the stylesheet module before reading the file for CSS cache updates. Files with no scoped CSS module (typical when the only consumer is ?raw) no longer get compiled on hot update, so incomplete snippet sources can refresh as raw text instead of failing the TSRX parser.

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

jonkwheeler and others added 2 commits October 26, 2026 14:45
…srx on HMR

handleHotUpdate compiled every changed .tsrx file to refresh its
stylesheet before checking whether a stylesheet module existed. A file
imported only as `?raw` (an incomplete docs example, say) failed to
compile, and Vite sent an HMR error instead of the raw update.

Look the stylesheet module up first and compile only when it exists,
as the Hono plugin already does.

Fixes tsrx-org#566

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The shared JSX import injector always added
`import { Suspense } from '<source>'` for `@try … @pending` and
`import { Fragment } from '<source>'` for keyed fragment bodies, even
when the module already imported that name from the same source. The
duplicate declaration failed the build with "Identifier `Suspense` has
already been declared".

Skip the injected import when the module already has a value import of
the same name and local from the same source. React, Preact and Hono
share this injector.

Fixes tsrx-org#862

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jonkwheeler
jonkwheeler marked this pull request as ready for review October 26, 2026 19:57
@jonkwheeler
jonkwheeler merged commit 6b60554 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#1102'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 added a commit to jonkwheeler/tsrx that referenced this pull request Nov 12, 2026
main now has tsrx-org#1102'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 added a commit that referenced this pull request Nov 12, 2026
…under private names (#1175)

* fix(core): import generated Suspense, Fragment and TsrxErrorBoundary privately

The import injector added `Suspense`, `Fragment` and `TsrxErrorBoundary`
under their public names, and the generated elements referenced those
names. Any other authored binding with one of them broke the build
("Identifier `Suspense` has already been declared") for a type-only,
default, namespace or other-source import, or silently replaced the
generated element for a setup local or a `@for (const Fragment of ...)`
loop variable.

Import them as `__Suspense`, `__Fragment` and `__TsrxErrorBoundary`, as
the injector already does for `mergeRefs`, and reference those aliases
from the generated elements, including Hono's `fallbackRender` boundary.
This replaces the same-name import reuse from #1102, which no longer has
anything to reuse.

Vue imports `TsrxErrorBoundary` itself for the core-generated boundary,
so its injector takes the alias too, and it no longer merges that value
import into an authored `import type` declaration, where it would be
erased.

Fixes #1141

* fix(core,vue,solid): keep directives first and bind Vue's generated Suspense

Every import injector put its imports at index 0, ahead of a
`'use client'` or `'use server'` directive, which then stopped being a
directive. Any module using `@pending`, `@catch`, a keyed fragment row,
a dynamic tag or another construct with a generated import lost its
directive; Next.js treated a `'use client'` .tsrx module as a server
component ("You're importing a module that depends on `useState` into
a React Server Component module").

Insert generated imports (and the type-only style anchors) after the
directive prologue in the core, Vue and Solid injectors.

Vue's `ensure_named_import` also counted an inline `type` specifier as
the value import, so `import { type Suspense } from 'vue'` with
`@pending` left `Suspense` unbound. Require a value specifier, and
import Vue's generated `Suspense` as `__Suspense` like the core
transform does, so the value import never redeclares an authored
binding. vue-jsx-vapor compiles the aliased element the same way.

* test: expect the private generated names in main's @Try and @pending tests

This branch imports the generated Suspense and TsrxErrorBoundary as
`__Suspense` and `__TsrxErrorBoundary`. Tests that main added since (the
TsrxTryBody fallback, @Try site keys, inline @pending bodies, Vue's
keyed-slot computed import and element-child boundary) matched the public
names. They now match the aliases, the react-server import check reads the
imported name of each specifier, and Vue's Suspense import tests expect the
generated `Suspense as __Suspense` beside any authored `Suspense` import.

* test: expect the private Fragment alias in main's nested keyed @for test

Main's #1136 added a test that matches `<Fragment key={group.id}>` for a
keyed row that renders a nested @for. This branch imports the generated
Fragment as `__Fragment`, so the test now matches that name.

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