Skip to content

fix(turbopack): scoped CSS in Next builds and TSX parsing for platform flags - #1087

Merged
jonkwheeler merged 9 commits into
tsrx-org:mainfrom
jonkwheeler:fix/turbopack-scoped-css-and-platform
Nov 12, 2026
Merged

jonkwheeler merged 9 commits into
tsrx-org:mainfrom
jonkwheeler:fix/turbopack-scoped-css-and-platform

Conversation

@jonkwheeler

@jonkwheeler jonkwheeler commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Two Turbopack (Next.js) integration bugs.

Scoped styles in the App Router (#565). Before: any component with a <style> block failed next build with "server relative imports are not implemented yet". After: App Router builds pass and the scoped stylesheet is linked into the page.

  • The loader imported the generated stylesheet by its absolute path. Turbopack does not resolve absolute import paths. It now imports ./<basename>?tsrx-css&lang.css.
  • With only that change, the build passed but the CSS from a server component never reached the browser. The ?tsrx-css rule used type: 'css', so Turbopack bundled the CSS into the server chunk and Next never added a <link> for it. The rule now uses as: '*.css', which goes through Next's own CSS rule. Client-component imports already worked and still do.
  • API change (minor bump for @tsrx/turbopack-plugin-react): create_tsrx_react_turbopack_css_rule() and the TsrxReactTurbopackCssRule type now have as: '*.css' instead of type: 'css'. If you spread or inspect this rule, expect as: '*.css'.
  • Pages Router: a styled .tsrx component still fails with Next's "Global CSS cannot be imported from files other than your Custom ". It fails the same way with the old type: 'css' rule, so this PR didn't cause it. The README now says so.
  • Peer range: the next peer dependency is now >=16.2.0 (was >=15.3.0). The rules use condition.query, which Next's turbopack config docs list as added in 16.2.0. A real next build on Next 16.1.7 fails for every page, styled or not, with "Unrecognized key(s) in object: 'query'". On 16.2.0, styled and unstyled builds pass and the stylesheet is linked (b5448a2).

Platform flags in ordinary modules (#910). Before: with a platform selected, <pre>/*</pre> in an ordinary .jsx/.tsx page failed with "Unclosed tag '

'". A flag written as <pre>/* {import.meta.env.platform.web} */</pre> was read as a TSRX comment and left unreplaced, so the page crashed at runtime. After: these modules are read with TSX's grammar, so the text stays text and the flag is replaced.

  • replacePlatformFlags (only the Turbopack platform loader calls it) now parses with acorn-typescript instead of the TSRX parser.
  • acorn-typescript's JSX mode rejects every single-parameter generic arrow without a trailing comma. The new parser allows a constraint or default (<T extends object>(o: T) => o), mirroring the TSRX parser's existing exemption.
  • acorn-typescript lacks some syntax the TSRX parser reads: phase imports (import defer), a trailing comma in import(), and for ((a as T) of x). When it rejects a module, the code falls back to the TSRX parser, so modules that parsed before still parse. If both parsers fail, the acorn-typescript error is thrown, at the real location.
  • .ts/.mts/.cts files are parsed without JSX, so <number>value type assertions no longer fail with "Unterminated JSX contents" (d826f8f25).

Verification

  • Real next build (Next 16.2.6, issue #565 script, App Router): styled exits 0 with <link rel="stylesheet"> → div.tsrx-…{color:red}, and unstyled exits 0. Without the as: '*.css' change, the styled build passes but has no stylesheet link. next dev behaves the same way: the link is present with this change and absent with type: 'css'.
  • Real next build (issue #910 script plus extra cases), before → after:
    • web (<pre>/*</pre>): 1 → 0
    • web-tsx (<pre>// docs</pre>): 1 → 0
    • web-flag (flag between comment-like text): 1 → 0, renders /* true */. Before, prerender failed with "Cannot read properties of undefined (reading 'platform')".
    • web-generic-flag / web-generic-docs (constrained or defaulted generic arrow in page.tsx): both exit 0.
    • unconfigured and web-string stay at 0.
  • New and updated tests in packages/tsrx/tests/utils/platform.test.js and packages/turbopack-plugin-react/tests/*. They cover constrained and defaulted generic arrows, the error location after one, the error when neither parser reads a module, and .js/.mjs/.cjs filenames. Each failed before its fix.
  • npx vitest run --project tsrx-utils --project turbopack-plugin-react: 1560 passed.
  • Full npx vitest run (before the review fixes): 9986 passed, 28 failed. 27 were 5s/30s timeouts while the machine's load average was about 75–97, and 1 was a shiki tokenizer error in the vscode grammar test. All 13 affected files passed when re-run on their own; 4 of them needed --testTimeout=120000.
  • pnpm typecheck clean, prettier --check clean on changed files, pnpm changeset:check passes.

Issues

Fixes #565
Fixes #910

🤖 Generated with Claude Code


Note

Medium Risk
Touches Next/Turbopack integration (peer bump, rule API change) and platform transform parsing for all ordinary modules when a platform is set; behavior is well-tested but affects build-time CSS linking and flag rewriting.

Overview
Fixes App Router Turbopack builds for .tsrx components with scoped <style> blocks: the loader now injects a relative ?tsrx-css import (Turbopack rejects absolute paths), and the CSS Turbopack rule uses as: '*.css' instead of type: 'css' so Next links the stylesheet on server components in dev and build. create_tsrx_react_turbopack_css_rule() / TsrxReactTurbopackCssRule expose that shape change; next peer is >=16.2.0 for condition.query in turbopack rules. Pages Router behavior is unchanged (still hits Next’s global CSS restriction).

Platform flag replacement in ordinary .js/.jsx/.ts/.tsx now parses with acorn-typescript via new parse_ordinary_module, so comment-like JSX text (e.g. <pre>/*</pre>) is not treated as TSRX syntax, flags in {…} next to that text are rewritten, and .ts files keep <T>value as type assertions. Constrained/defaulted generic arrow params are allowed in TSX; syntax only the TSRX parser understands still falls back to parse_module, with acorn’s error surfaced if both fail.

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

jonkwheeler and others added 8 commits October 26, 2026 14:15
The loader appended `import "<absolute path>.tsrx?tsrx-css&lang.css"`
for components with a `<style>` block. Turbopack does not resolve
absolute import paths ("server relative imports are not implemented
yet"), so every Next build of a component with scoped styles failed.
Import the sibling resource as `./<basename>?tsrx-css&lang.css`.

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

The sibling `?tsrx-css&lang.css` rule set `type: 'css'`. Turbopack then
bundled the stylesheet into the server chunk, but Next never linked it
into the page when a server component imported it, so a component's
scoped styles were missing in `next dev` and `next build` unless a
client component imported it. Rename the module with `as: '*.css'`
instead, which sends it through Next's own CSS rule.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`replacePlatformFlags` parsed every module with the TSRX parser, so
TSRX's JSX-text rules applied to ordinary `.jsx`/`.tsx` files. Under the
Turbopack platform loader, `<pre>/*</pre>` failed with "Unclosed tag",
and a flag inside `<pre>/* {import.meta.env.platform.web} */</pre>` was
read as a comment and left unreplaced, crashing at runtime.

Parse with acorn-typescript's TSX grammar instead. It lacks some syntax
the TSRX parser reads (phase imports, a trailing comma in `import()`),
so a module it rejects falls back to the TSRX parser, as before.

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

TypeScript files have no JSX, but flag replacement parsed them with JSX
enabled, so `<number>value` in a `.ts`, `.mts` or `.cts` module failed
with "Unterminated JSX contents" under the Turbopack platform loader.
Parse those extensions without JSX.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
acorn-typescript in JSX mode rejects every single-parameter generic
arrow without a trailing comma, including `<T extends object>(o: T) => o`,
which is valid TSX. Such a module fell back to the TSRX parser, so the
JSX-text problems this parser exists to avoid came back, and a later
syntax error was reported at the arrow instead of where it is. Allow a
constraint or default, as the TSRX parser's own override does.

Also pin which error a module neither parser reads reports, and cover
`.js`, `.mjs` and `.cjs` filenames.

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

The README and llms.txt still said the stylesheet is handed back as a
"CSS module". It is now renamed to `*.css`, so Next links it into App
Router pages. The README also notes that the Pages Router rejects the
import as global CSS.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`create_tsrx_react_turbopack_css_rule()` and `TsrxReactTurbopackCssRule`
lose `type` and gain `as`, a public API removal. Say the scoped-styles
fix is for the App Router and add a migration note.

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

The Turbopack rules match the generated stylesheet with
`condition.query`, which `turbopack.rules` accept from Next 16.2.0 (the
version history in Next's `turbopack` config docs). Next 16.1.7 rejects
the config with "Unrecognized key(s) in object: 'query'", so every build
fails, styled or not. Raise the `next` peer range from `>=15.3.0` to
`>=16.2.0` and say so in the README and changeset.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jonkwheeler
jonkwheeler merged commit e423279 into tsrx-org:main Nov 12, 2026
8 checks passed
@github-actions github-actions Bot mentioned this pull request Nov 12, 2026
jonkwheeler added a commit to jonkwheeler/tsrx that referenced this pull request Nov 12, 2026
main now has tsrx-org#1087'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
In error-examples.js, WITHOUT_EXAMPLE keeps this PR's RESERVED_TYPE_ASSERTION entry and main's LET_DECLARATION_NAME in place of the LET_BINDING entry main removed, and KNOWN_PROBLEMS keeps this PR's UNCLOSED_TAG_GENERIC_ARROW record without the two CODE_BLOCK records main dropped. platform.test.js keeps this PR's TypeScript-module test and main's TSX and TypeScript platform tests from tsrx-org#1087.

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

Development

Successfully merging this pull request may close these issues.

1 participant