Skip to content

Import stylesheet dependencies in the Vite dev server - #100

Closed
nstepien wants to merge 2 commits into
mainfrom
fix-dev-stylesheet-dependencies
Closed

nstepien wants to merge 2 commits into
mainfrom
fix-dev-stylesheet-dependencies

Conversation

@nstepien

@nstepien nstepien commented Oct 15, 2026 •

Copy link
Copy Markdown
Owner

When a module's CSS interpolates a class name imported from another module, the plugin imports that module's stylesheet first, so the referenced CSS comes before the CSS that uses it. It relied on this.load() running the imported module's transform hook, which registers that stylesheet. Vite's dev server only does so when a plugin's load hook returned the module's code, which is never the case for files on disk. So the first transform of an importer lacked the import, and its CSS was injected before the CSS it references. Builds were not affected.

Changes

  • When the imported module has not been transformed yet, extractStylesheet extracts its stylesheet from source and registers it under the same content-hash ID that the module's own transform registers later, so both point at the same virtual stylesheet. This also covers builds where Stop builds from hanging on mutually interpolated class names #102 skips a load to break a cycle.
  • That on-demand extraction only resolves values: it loads no modules, collects no stylesheets, and leaves warnings to the module's own transform. So it cannot recurse, and it cannot wait for the transform it runs in, which would bring back the hang Stop builds from hanging on mutually interpolated class names #102 fixes.
  • Stylesheet registration moves into a shared registerStylesheet, keyed by the module ID without its query, which is what resolveValue looks up.

Tests

Test plan

  • node --run format:check, lint, typecheck, build
  • node --run test: 18/18 against the source and against the built dist (CI=true)
  • The new dev server tests fail without the fix

🤖 Generated with Claude Code

@nstepien
nstepien added this pull request to stack #101 October 15, 2026 16:42
@coderabbitai

coderabbitai Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 37c99eca-8048-431a-a6b0-0f7a724dbc1f
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@nstepien
nstepien removed this pull request from stack #101 October 15, 2026 17:11
@nstepien
nstepien force-pushed the fix-dev-stylesheet-dependencies branch from 9e4f41d to b08a6fb Compare October 15, 2026 17:15
@nstepien
nstepien changed the base branch from plugin-audit-follow-ups to fix-cyclic-interpolation-hang October 15, 2026 17:15
@nstepien
nstepien added this pull request to stack #103 October 15, 2026 17:15
@nstepien
nstepien force-pushed the fix-dev-stylesheet-dependencies branch from b08a6fb to ba578b6 Compare October 15, 2026 17:40
@nstepien
nstepien force-pushed the fix-cyclic-interpolation-hang branch from f4740c2 to 3cdc545 Compare October 15, 2026 17:40
Base automatically changed from fix-cyclic-interpolation-hang to main October 16, 2026 01:21
nstepien added a commit that referenced this pull request Oct 16, 2026
To resolve a class name imported from another module, a module's
transform waits for `this.load()` of that module, which runs that
module's transform. When two modules interpolate each other's class
names, each transform waited for the other to finish, and the build
never completed.

## Changes

- Track which module each running transform is waiting for
(`pendingLoads`), and skip loading a module whose transform is already
waiting for the current one, directly or through other transforms
(`isWaitingFor`). That module's transform parsed it before resolving
anything, so its exported values come from the cache.
- Only loads that would close a cycle are skipped. Transforms that run
concurrently without waiting for each other still wait for the loads
they need, so they register their stylesheets as before.
- Within a cycle, the module whose load was skipped has not registered
its stylesheet yet, so its importer cannot import that stylesheet ahead
of its own. Every rule is still emitted, in the order the other module
imports them. #100, stacked on top, extracts the skipped module's
stylesheet on demand.

## Tests

- New fixtures `cycle-a.ts` and `cycle-b.ts`, whose CSS interpolates
each other's class names. A Vite build of them timed out before this
change and now completes with both rules.
- A three-module cycle (A → B → C → A) also builds, with all three rules
when the package is not `sideEffects: false`.
- All existing snapshots pass, including the cross-module
interpolations.

## Test plan

- [x] `node --run format:check`, `lint`, `typecheck`, `build`
- [x] `node --run test`: 16/16 against the source and against the built
`dist` (`CI=true`)
- [x] The new test times out without the fix

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
When a module's CSS interpolates a class name imported from another module,
that module's stylesheet is imported first to keep the cascade order. The
plugin relied on `this.load()` to transform the imported module and register
its stylesheet, but Vite's dev server only runs transform hooks there when a
plugin's `load` hook returned the code. The first transform of an importer
then lacked the import, injecting its CSS before the CSS it references.

When the imported module has not been transformed yet, which also happens
in builds when its load is skipped to avoid a cycle, extract its stylesheet
from source under the same content-hash ID its own transform registers
later. That on-demand extraction only resolves values: it loads no modules,
collects no stylesheets, and leaves warnings to the module's own transform,
so it can neither recurse nor wait for the transform it runs in.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nstepien
nstepien force-pushed the fix-dev-stylesheet-dependencies branch from 96b2f88 to c7012f4 Compare October 16, 2026 01:21
#102 changed the CSS of the cycle fixtures, and with it their stylesheet IDs. Merging its branch into this one updated this snapshot, but GitHub restacked this branch onto the squash-merged #102 by replaying only its own commit, which dropped that merge.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nstepien
nstepien removed this pull request from stack #103 October 16, 2026 14:21
nstepien added a commit that referenced this pull request Oct 16, 2026
The comments suggested that the load populates the parse cache, which made
`pendingLoads` look unnecessary. The values come from the parse either way:
the load waits for the imported module's transform to register its stylesheet.
Also document that Vite's dev server does not transform modules loaded this
way, and why #100's workaround was dropped.

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

Copy link
Copy Markdown
Owner Author

Closing: the dev server order this restores only decides specificity ties, the README doesn't promise it, and Vite's dev server doesn't transform modules loaded through this.load() by design (vitejs/vite#11469). The dev server test helper and the docs moved to #104.

@nstepien nstepien closed this Oct 16, 2026
nstepien added a commit that referenced this pull request Oct 16, 2026
…ev server (#104)

The dev server only calls `buildEnd` when it closes, so the plugin's
caches outlive edits. Editing a module whose values or class names
another module's CSS interpolates could leave that CSS stale until the
server restarted:

- The cached parse of the edited module was reused until ecij
transformed that module again, which never happens for a module without
`css`, like a tokens file. Importers transformed again kept its old
values.
- Importers transformed again before the edited module still imported
its previous stylesheet, which kept its previous CSS on the page.
- Vite reuses the transforms of the modules that statically import an
edited module, only updating their imports. This did not happen when the
module was first loaded through ecij's `this.load()`, which registers it
under its file path rather than its URL, but it did when the app
requested the module before its importers, for example by also importing
it elsewhere. Their CSS was then not extracted again at all.

Builds are not affected: `buildEnd` clears the caches after each build,
including in watch mode.

## Changes

- `transform` records the modules that interpolate each module's values
in `dependentsPerFile`.
- `watchChange` forgets the parse of the edited module and the
stylesheet IDs that change with it: its own, and those of the modules
that interpolate its values. Modules transformed again before these no
longer import their previous stylesheets. The extracted CSS stays, as
cached transforms may still import it.
- The Vite-only `hotUpdate` hook adds the modules that interpolate the
edited module's values to the update, as if they were edited, and the
modules that interpolate their class names, as they import their
stylesheets. The declarations keep Rolldown's `Plugin` type, and
Rolldown ignores the hook.
- From #100, which was closed: the comment at the `this.load()` call and
AGENTS.md now say that the load waits for the imported module to
register its stylesheet, as the values come from its parse either way,
and that Vite's dev server does not transform modules loaded this way,
which is not worth working around.

## Tests

- New dev server tests edit copies of new fixtures, created next to
them: `card.ts` interpolates a class name of `button.ts`, which
interpolates a color from `tokens.ts`. Each test requests them before
their importers, edits `tokens.ts` or `button.ts`, and lets the server's
file watcher pick up the edit. `card.ts` is then requested first and
must no longer import the previous stylesheet of `button.ts`, whose new
stylesheet must have the new CSS.
- The dev server helper, from #100, returns a disposable server. As the
copies are inside this package, `ecij` resolves without an alias, and
`.gitignore` covers copies left behind by an interrupted run.

## Test plan

- [x] `node --run format:check`, `lint`, `typecheck`, `build`
- [x] `node --run test`: 18/18 against the source and against the built
`dist` (`CI=true`)
- [x] The new tests fail without the `hotUpdate` hook, when it only adds
the direct dependents, and without each deletion in `watchChange` or the
dependency recording
- [x] The dev server tests passed 30 runs in a row on Windows, and 18
more with six suites running at once

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
@nstepien
nstepien deleted the fix-dev-stylesheet-dependencies branch November 7, 2026 05:49
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