Skip to content

fix: resolve relative url() in style blocks and reject @import (#153) - #198

Merged
leonidaz merged 4 commits into
mainfrom
fix/css-relative-urls-153
Sep 23, 2026
Merged

leonidaz merged 4 commits into
mainfrom
fix/css-relative-urls-153

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #153.

Problem

The issue reports two failures with relative references in a component's <style> block under Vite:

  • url(./asset.svg) stayed unresolved and Vite printed a warning.
  • @import "./shared.css" failed the build with [postcss] ENOENT.

Both came from the virtual module id: each Vite plugin served the extracted CSS as \0/abs/App.tsrx?tsrx-css&lang.css. Vite's CSS plugin resolves relative references against the id's directory, which began with \0 and wasn't a real path. The issue reported this for Hono, but the React, Preact, Solid, and Vue Vite plugins build the same id. The Rspack, Turbopack, and Bun plugins already use the component's real path.

Looking at @import more closely showed a bigger problem than the resolution failure. The compiler scopes only the rules written in the block and passes @import through untouched. The bundler then inlines the imported file after scoping, so its rules come out unscoped and apply to the whole page. That defeats scoped style blocks, where :global is the explicit escape hatch. So this PR rejects @import instead of making it work.

Changes

Vite plugins (patch): the stylesheet id is now the component path plus the style query, with no \0. Vue's and Svelte's style blocks use the same form. Relative url() now resolves from the component's directory in builds and in vite dev. During hot updates, Vite now lists the CSS module under the component's file, so the plugins no longer add it a second time. The React plugin's published resolveId/load types drop the \0 from their overloads.

Core (minor, language removal): @import in a <style> block is the new tsrx-css-import diagnostic (DIAGNOSTIC_CODES.CSS_IMPORT). It's reported during style analysis at the rule's position, for standalone, assigned, <head>, and resource blocks, and in any case (@IMPORT). Only top-level rules are checked, since @import is only valid there. Editors collect it like the other style diagnostics. The message points to assigned blocks with apply for shared scoped styles, and, for global CSS, to :global or a JavaScript import './global.css'. A plain-TSX <style>{css}</style> is an ordinary element and isn't checked.

Docs: the spec's style constraints (prose and the diagnostics block), llms.txt, the core README, and the RuleSync agent rules, with generated files and the MCP docs index regenerated.

Tests

  • New shared harness @tsrx/core/test-harness/css-references. It writes a component in a subdirectory whose <style> uses url(./asset.svg), then either runs a Vite library build or starts a Vite dev server and fetches the component and its stylesheet module.
  • Each Vite plugin runs a build test and a dev test. Hono runs builds in both server and DOM modes. The tests check that the SVG becomes a data URL and that the dev stylesheet URL is /components/Styled.tsrx?…&lang.css. All of them fail without the plugin change.
  • Core analysis tests cover the diagnostic's position, every block kind and @import form, multiple rules in one block, cases that must not be reported, and strict mode throwing a coded CompileError.
  • Updated existing unit tests that hard-coded the \0 id.
  • Checked by hand, not in the test suite: ssrLoadModule works for Hono and React, and dev HMR picks up <style> edits on all five plugins.

Commands run: pnpm vitest run over the core analyze/utils, target compiler, runtime, vite-plugin, tsrx-vite, language-server, MCP, and website projects (all pass), pnpm typecheck, pnpm format:check, and pnpm changeset:check.

Follow-up

Ripple's compiler lives in Ripple-TS/ripple. If it runs the core style analysis, it picks up the new error on the next core bump. Otherwise it needs its own check.

🤖 Generated with Claude Code


Note

Medium Risk
Adds a breaking compile error for @import in <style> blocks (minor core bump) and changes virtual CSS module ids across all Vite plugins, which could affect custom tooling that assumed \0-prefixed ids.

Overview
Fixes scoped <style> behavior under Vite and closes a scoping hole for @import.

Vite plugins (React, Preact, Solid, Vue, Hono): Extracted component CSS is no longer registered with a \0-prefixed virtual id. The stylesheet module id is now the component file path plus the existing style query (e.g. ?tsrx-css&lang.css), so Vite resolves relative url() (and @import paths) from the component’s directory in dev and production. HMR lookup uses the same id; plugins avoid duplicating the CSS module when Vite already lists it.

Core (minor): @import at the top level of a scoped <style> body is a compile error tsrx-css-import (DIAGNOSTIC_CODES.CSS_IMPORT), with source positions on each import rule. Imported sheets would not be scoped and could leak globally; the message points to assigned blocks + apply for shared scoped CSS and to JS import './global.css' or :global for global styles.

Tooling & docs: Spec, llms.txt, agent rules, MCP style docs, and README list the new diagnostic. A shared test-harness/css-references fixture exercises build + dev URL resolution across Vite plugins; core analysis tests cover @import reporting.

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

)

The Vite plugins served each component's extracted `<style>` CSS under a
`\0`-prefixed virtual id. Vite resolves relative CSS references against the
module id's directory, which then began with `\0` and did not exist, so a
relative `@import` failed the build with ENOENT and a relative `url()` stayed
unresolved.

Use the component path plus the style query as the id, like Vue and Svelte
style blocks, so references resolve from the component's directory in dev and
builds. Vite now finds the CSS module under the component file on hot update,
so the plugins skip adding it again when it is already listed.

Add a shared build and dev-server harness and cover all five Vite plugins.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@vercel

vercel Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
tsrx-website Ready Ready Preview Sep 23, 2026 9:57pm UTC
tsrx-website-mcp Ready Ready Preview Sep 23, 2026 9:57pm UTC

Request Review

The compiler scopes only the rules written in a `<style>` block and passed
`@import` through, so the bundler inlined the imported rules unscoped and they
applied to the whole page. That defeats scoped style blocks, where `:global` is
the explicit escape hatch.

Report `@import` as the `tsrx-css-import` diagnostic (`CSS_IMPORT`) during
style analysis, at the rule's position, for standalone, assigned, head, and
resource blocks. Editors collect it like the other style diagnostics.

Drop `@import` from the Vite reference fixture so it covers relative `url()`
only, and document the rule in the specification, llms.txt, the core README,
and the agent rules.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@leonidaz leonidaz changed the title fix(vite-plugins): resolve relative references in extracted styles (#153) fix: resolve relative url() in style blocks and reject @import (#153) Sep 23, 2026
…ng (#153)

`@import` is only valid at the top level of a stylesheet, so scanning the
style block's top-level rules is enough; the extra walk over every sheet is
not needed.

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

Name both routes to global CSS in the message and docs: `:global` in the
block, or a JavaScript `import './global.css'`, which the bundler adds to the
page unscoped.

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

This branch was successfully deployed

2 active deployments
Preview – tsrx-website — 509e8f12 Deployed Sep 23, 2026 by vercel[bot]
Preview – tsrx-website-mcp — 509e8f12 Deployed Sep 23, 2026 by vercel[bot]
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.

[Bug]: Vite fails to resolve relative imports and assets in extracted TSRX styles

1 participant