Skip to content

Update the CSS of modules that depend on edited modules in the Vite dev server - #104

Merged
nstepien merged 7 commits into
mainfrom
fix-dev-stale-css
Oct 16, 2026
Merged

nstepien merged 7 commits into
mainfrom
fix-dev-stale-css

Conversation

@nstepien

@nstepien nstepien commented Oct 15, 2026 •

Copy link
Copy Markdown
Owner

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 Import stylesheet dependencies in the Vite dev server #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 Import stylesheet dependencies in the Vite dev server #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

  • node --run format:check, lint, typecheck, build
  • node --run test: 18/18 against the source and against the built dist (CI=true)
  • 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
  • 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

@nstepien
nstepien added this pull request to stack #103 October 15, 2026 23:22
@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: b745be7a-b7db-4629-9c0a-bf61025ab54f
  • 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 and others added 2 commits October 16, 2026 15:19
…ev server

- `watchChange` forgets the parse of an edited module, which the dev server
  kept until it closed: editing a module that ecij does not transform, like a
  tokens file, left the CSS of its importers unchanged. It also forgets the
  stylesheet IDs that change with it, so that the modules transformed again
  first do not import previous stylesheets.
- Vite reuses the transforms of the modules that statically import an edited
  module, only updating their imports. The new `hotUpdate` hook adds the
  modules whose CSS interpolates its values to the update, along with the
  modules that interpolate their class names, which import their stylesheets.
- The new dev server test edits copies of its fixtures in a temporary
  directory and emits the changes itself.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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
nstepien removed this pull request from stack #103 October 16, 2026 14:21
@nstepien
nstepien changed the base branch from fix-dev-stylesheet-dependencies to main October 16, 2026 14:21
nstepien and others added 5 commits October 16, 2026 16:51
A generator yields each dependent and then its dependents, as the
no-map-spread lint rule rejects spreading them into an array in flatMap.
Iterator helpers and Set methods need Node 22.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This reverts commit 189918c. The loops
read better until Iterator.concat can join each dependent with its
dependents.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The test now edits copies of its fixtures created next to them with
mkdtempDisposable, where ecij resolves to this package without an
alias, and lets the server's file watcher pick up the edits. The dev
server helper returns a disposable server instead of taking a callback,
and the test polls with expect.poll instead of vi.waitFor.

Each edit is its own case, starting from card.ts importing the
stylesheet of button.ts, so that polling for card.ts to stop importing
it waits for the edit. In a single sequence, card.ts no longer imports
any stylesheet of button.ts after the first edit, so the poll for the
second edit passed before the server picked it up.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
AGENTS.md keeps the decision not to work around the dev server's
stylesheet order, without its history.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
They also import its stylesheet, whose ID changes with its CSS, like
card.ts when button.ts is edited. The comment only said that their own
CSS may change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nstepien
nstepien merged commit d2af6da into main Oct 16, 2026
8 checks passed
@nstepien
nstepien deleted the fix-dev-stale-css branch October 16, 2026 20:19
nstepien added a commit that referenced this pull request Oct 17, 2026
main took in the first slice of this PR (#79) and solved several of its
other problems differently since August, so the merge ports the rest of
this branch onto main's design instead of keeping both:

- Values, class names included, come from the parse of each module, and
  const initializers resolve lazily (#79). Re-exports, `export *`,
  default and namespace imports, and `export default` resolve through the
  same evaluator, with ESM precedence and ambiguity.
- Cycles are handled by main's pendingLoads and isWaitingFor (#102), so
  the wait graph, the deferred same-file passes, and the import-last
  ordering are gone, along with the guard against inlining the class
  names of skipped templates, which needed the imported modules'
  transforms.
- Class names are IIFEs (#87, #98), so `moduleSideEffects: true` is gone.
- Vite's dev server keeps main's behavior (#104) and does not transform
  imported modules through transformRequest. There, the code of modules
  that no file holds comes from their `load` hooks.
- UNREADABLE_MODULE and UNPARSEABLE_MODULE are gone: a module whose code
  cannot be read exports nothing, and modules are parsed as the language
  Rolldown assigned them.
- Kept: raw TypeScript bindings, `meta.moduleType` parsing,
  prologue-safe imports, query-variant module identity, skipping
  external modules, and resolving each import once per transform.

Tests: main's suite, plus this branch's tests of the kept features, with
new cases for the paths that no fixture reached. The tests of the removed
behavior, and the cycle test that main already has, are dropped.
basic.input.ts is restored for main's inlineConst tests, and this
branch's barrel.ts is now barrel-index.ts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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