Skip to content

<style>@import "./style.css";</style> fails to HMR #7042

@leoj3n

Description

@leoj3n

Describe the bug

Inside <body> or <head> of index.html, the following code:

    <style>
      @import "./style.css";
    </style>

Does not cause index.html to reload or for the css to be replaced when ./style.css is edited and saved.

The @import is considered an isDirectCSSRequest here:

if (isDirectCSSRequest(id)) {
return css
} else {

The ModuleNode will look like that:

ModuleNode {
  id: '/home/projects/vitejs-vite-flhjoj/style.css?direct',
  file: '/home/projects/vitejs-vite-flhjoj/style.css',
  importers: Set(0) {},
  importedModules: Set(0) {},
  acceptedHmrDeps: Set(0) {},
  isSelfAccepting: false,
  transformResult: null,
  ssrTransformResult: null,
  ssrModule: null,
  lastHMRTimestamp: 0,
  url: '/style.css?direct',
  type: 'css'
}

Editing and saving ./style.css logs out:

12:09:54 PM [vite] hmr update /style.css?direct

However, no actual update takes place, unlike when doing import './style.css'; in main.js which has ModuleNode like:

ModuleNode {
  id: '/home/projects/vitejs-vite-flhjoj/style.css',
  file: '/home/projects/vitejs-vite-flhjoj/style.css',
  importers: Set(1) {
    ModuleNode {
      id: '/home/projects/vitejs-vite-flhjoj/main.js',
      file: '/home/projects/vitejs-vite-flhjoj/main.js',
      importers: Set(0) {},
      importedModules: [Set],
      acceptedHmrDeps: Set(0) {},
      isSelfAccepting: false,
      transformResult: [Object],
      ssrTransformResult: null,
      ssrModule: null,
      lastHMRTimestamp: 0,
      url: '/main.js',
      type: 'js'
    }
  },
  importedModules: Set(0) {},
  acceptedHmrDeps: Set(0) {},
  isSelfAccepting: false,
  transformResult: null,
  ssrTransformResult: null,
  ssrModule: null,
  lastHMRTimestamp: 0,
  url: '/style.css',
  type: 'js'
}

And hits this code instead of isDirectCSSRequest:

return [
`import { updateStyle as __vite__updateStyle, removeStyle as __vite__removeStyle } from ${JSON.stringify(
path.posix.join(config.base, CLIENT_PUBLIC_PATH)
)}`,
`const __vite__id = ${JSON.stringify(id)}`,
`const __vite__css = ${JSON.stringify(css)}`,
`__vite__updateStyle(__vite__id, __vite__css)`,
// css modules exports change on edit so it can't self accept
`${
modulesCode ||
`import.meta.hot.accept()\nexport default __vite__css`
}`,
`import.meta.hot.prune(() => __vite__removeStyle(__vite__id))`
].join('\n')

Which results in that being logged to console upon editing ./style.css:

hmr update /style.css

If you were to edit index.html when only using @import, by adding a space somewhere and saving the file, that is logged to console:

12:17:20 PM [vite] page reload index.html

And the CSS changes from the @import-ed ./style.css are updated.

Therefore, it seems one solution may be to trigger page reload index.html when @import-ed css files are edited.

Reproduction

https://stackblitz.com/edit/vitejs-vite-flhjoj?file=style.css

System Info

Stackblitz.

Used Package Manager

npm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions