Skip to content

Dependencies added during a CSS transform opt-out of HMR #19511

Closed as duplicate

Description

@philipp-spiess

Describe the bug

When using addWatchFile(…) inside a transform hook on CSS files, the watched files will successfully trigger a rebuild of the CSS file whenever they change.

However, a rebuild triggered from the watched files will always cause a hard-refresh instead of a HMR refresh of the Stylesheet that happens when the CSS file changes on it's own.

This can be reproduced by a plugin like this:

function plugin(): Plugin {
  return {
    name: "my-plugin",
    async transform(content: string | undefined, id: string) {
      if (id.endsWith("main.css")) {
        this.addWatchFile(
          fileURLToPath(new URL("./articles/test.txt", import.meta.url))
        );
        return `/* ${Math.random()} */\n${content}`;
      }
    },
  };
}

In Tailwind CSS, we use addWatchFile(…) extensively to avoid setting up our own watchers.

Reproduction

https://github.com/philipp-spiess/vite-css-dependency-hard-refresh

Steps to reproduce

pnpm build
pnpm vite dev
touch src/index.css # This triggers a HMR
touch artilces/test.txt # This triggers a hard-refresh

System Info

System:
  OS: macOS 15.3.1
  CPU: (16) arm64 Apple M4 Max
  Memory: 168.77 MB / 48.00 GB
  Shell: 5.9 - /bin/zsh
Binaries:
  Node: 22.11.0 - ~/.local/share/mise/installs/node/22.11.0/bin/node
  Yarn: 1.22.22 - ~/.local/share/mise/installs/node/22.11.0/bin/yarn
  npm: 10.9.0 - ~/.local/share/mise/installs/node/22.11.0/bin/npm
  pnpm: 9.14.4 - ~/.local/share/mise/installs/pnpm/9.14.4/bin/pnpm
  bun: 1.1.34 - ~/.local/share/mise/installs/bun/1.1.34/bin/bun
Browsers:
  Brave Browser: 133.1.75.180
  Chrome: 133.0.6943.127
  Safari: 18.3
  Safari Technology Preview: 18.2
npmPackages:
  vite: ^6.2.0 => 6.2.0

Used Package Manager

pnpm

Logs

No response

Validations

Activity

  1. added
    duplicate
    and removed on Feb 25, 2025
  2. locked and limited conversation to collaborators on Mar 12, 2025
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

    duplicate

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions