Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When I modify a source file, Vite always reloads the page instead of using HMR. #16764

Open
wcshds opened this issue Feb 23, 2025 · 6 comments
Open

Comments

@wcshds
Copy link

wcshds commented Feb 23, 2025

QQ2025222-15517.mp4

What version of Tailwind CSS are you using?

v4.0.8

What build tool (or framework if it abstracts the build tool) are you using?

Vite 6.1.1

What version of Node.js are you using?

v22.14.0

What browser are you using?

Chrome and Firefox

What operating system are you using?

WSL

Reproduction URL

https://github.com/wcshds/nuxt-tailwind-source-reload-everytime

Describe your issue

I tried to use tailwind v4 in my Nuxt project. I use @source to explicitly register source path, but when I modify a source file, Vite always reloads the page instead of only using HMR.

@philipp-spiess
Copy link
Member

Hey! I think we need to file this as an upstream bug report for Nuxt.js. I am able to reproduce this as soon as you add a transform hook to a CSS file that adds a watch directory. So this currently reproduces the same issue:

function debug() {
  return {
    name: "debug",
    transform(content: string, id: string) {
      if (id.endsWith("main.css")) {
        this.addWatchFile(
          "/Users/philipp/dev/nuxt-tailwind-source-reload-everytime/articles/test.txt"
        );
        return { content: `/* ${Math.random()} */\n${content}` };
      }
    },
  };
}

I'm going to clean this up an make an upstream bug report!

@philipp-spiess
Copy link
Member

Actually can reproduce this in a plain Vite setup, so this appears to be a Vite thing.

@philipp-spiess
Copy link
Member

Filed vitejs/vite#19511

@philipp-spiess
Copy link
Member

So, apparently this is expected behavior for now: vitejs/vite#9512 (comment)

We'll do some digging on our side (the Vite extension) to see if we can work around this somehow but I can't make any guarantees :/

@wcshds
Copy link
Author

wcshds commented Feb 26, 2025

We'll do some digging on our side (the Vite extension) to see if we can work around this somehow but I can't make any guarantees :/

@philipp-spiess Thank you. I found that for my simple use case, adding an extra simple Vite plugin is sufficient.

import type { Plugin } from 'vite'
import path from "node:path"

export default function preventTailwindReload(
  directories: string[]
): Plugin {
  return {
    name: 'vite-prevent-tailwind-reload',
    handleHotUpdate({ file, modules }) {
      const currentDir = process.cwd();
      const relativeDir = path.dirname(path.relative(currentDir, file));

      if (directories.includes(relativeDir)) {
        return [...modules[0]?.importers ?? [], ...modules.slice(1)]
      }
    }
  }
}

Of course, it would be even better if the official Tailwind Vite plugin could completely resolve this issue.

@rozsazoltan
Copy link

rozsazoltan commented Mar 4, 2025

Yes. Currently, only the contents of .gitignore are not taken into account in v4. However, I would love to see a solution similar to the @source directive for excluding certain sources.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants