Skip to content

css: lightningcss visitor runs twice — minifyCSS re-applies css.lightningcss.visitor #23146

Closed

Description

@mattpilott

Describe the bug

When css.transformer: 'lightningcss', a configured css.lightningcss.visitor runs twice over production CSS: once in the transform/bundle pass, and again in the minify pass.

compileLightningCSS spreads the user's options into bundleAsync, applying the visitor:

await (await importLightningCSS()).bundleAsync({
  ...config.css.lightningcss,
  filename, projectRoot: config.root, resolver: { ... }
})

Then minifyCSS spreads the same object into transform, so visitor (and customAtRules) are applied a second time to the already-transformed output:

const { code, warnings } = (await importLightningCSS()).transform({
  ...config.css.lightningcss,
  targets: convertTargets(config.build.cssTarget),
  cssModules: void 0,
  filename, code: Buffer.from(css), minify: true
})

Since build.cssMinify defaults to lightningcss in Vite 8, this is the default path.

Reproduction

Minimal project — a visitor that counts its invocations, and two media queries:

// vite.config.js
let calls = 0
export default {
  css: {
    transformer: 'lightningcss',
    lightningcss: {
      visitor: { MediaQuery(q) { calls++; return q } }
    }
  },
  plugins: [{ name: 'report', closeBundle() { console.log('MediaQuery visitor calls:', calls) } }]
}
/* src/style.css */
@media (min-width: 100px) { a { color: red } }
@media (min-width: 200px) { b { color: blue } }
$ vite build
MediaQuery visitor calls: 4      # two queries, two passes

$ vite build   # with build.cssMinify: false
MediaQuery visitor calls: 2      # expected

Verified on vite 8.2.0 / lightningcss 1.33.0 / node 26.

Impact

  • Correctness — a visitor that is not idempotent produces wrong output, and it is not obvious from the API that idempotency is required. The second pass also sees post-transform CSS, which is not what a visitor author is writing against.
  • Performance — visitors with broad handlers (MediaQuery, Rule, Length) cross the JS/native boundary once per matching node, twice per build.
  • Confusing errors — a visitor that throws on unrecognised input can throw during minification rather than transformation, pointing at the wrong phase.

Related: this was raised in passing in #13835 (Stabilizing Lightning CSS) as visitor duplication in the Vite 8 beta, but I could not find a dedicated issue.

Suggested fix

Don't forward visitor / customAtRules from css.lightningcss into minifyCSS when config.css.transformer === 'lightningcss' — the transform pass has already applied them. Something like:

const { visitor, customAtRules, ...minifyOptions } =
  config.css.transformer === 'lightningcss' ? config.css.lightningcss : { ...config.css.lightningcss }

Note the same call already overrides targets with convertTargets(config.build.cssTarget), discarding any css.lightningcss.targets the user set — so the two passes can silently use different browser floors unless build.cssTarget is kept in sync by hand. That may be worth addressing at the same time.

Activity

  1. added theissue type on Aug 6, 2026
  2. added
    p2-edge-case
    on Aug 6, 2026
  3. added a commit that references this issue on Aug 6, 2026
    de041a7
  4. locked and limited conversation to collaborators on Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feat: cssp2-edge-case

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions