Skip to content

styles is not removed when css import is removed from non hmr module #20769

@hi-ogawa

Description

@hi-ogawa

Describe the bug

For HMR, Vite removes styles using prune event, but this is currently triggered only when an import is removed from a module which has import.meta.hot.

// check import.meta usage
if (rawUrl === 'import.meta') {
const prop = source.slice(end, end + 4)
if (prop === '.hot') {
hasHMR = true

const prunedImports = await moduleGraph.updateModuleInfo(
importerModule,
importedUrls,
importedBindings,
normalizedAcceptedUrls,
isPartiallySelfAccepting ? acceptedExports : null,
isSelfAccepting,
staticImportedUrls,
)
if (hasHMR && prunedImports) {
handlePrunedModules(prunedImports, environment)
}

This can causes an issue where style removal doesn't happen when modifying non-self-accepting module, for example:

app.jsx ->  dep2.js -> test.css

app.jsx : self accepting
dep2.js : not self accepting <- modifying this file to remove test.css import doesn't prune style

Reproduction

https://stackblitz.com/github/hi-ogawa/reproductions/tree/main/vite-prune-css?file=src%2Fdep2.js

Steps to reproduce

  • open stackblitz
    • observe orange text
  • comment out import "./test.css" from src/dep2.js
    • still text is orange
  • reload window
    • now text is black

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 20.19.1 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.8.2 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    @vitejs/plugin-react: ^5.0.0 => 5.0.2 
    vite: ^7.1.2 => 7.1.5

Used Package Manager

pnpm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions