Skip to content

Commit

Permalink
Vite: Don't crash with virtual module dependencies (#16780)
Browse files Browse the repository at this point in the history
Fixes #16732

If we can not get the mtime from a file, chances are that the resource
is a virtual module. This is perfectly legit and we can fall back to
what we did before the changes in `4.0.8` (which is to rebuild the root
every time a change contains a dependency like that).

## Test plan

Added a test to mimic the setup from the repor in #16732. Also ensured
the repro now passes:

<img width="1278" alt="Screenshot 2025-02-24 at 17 29 38"
src="https://github.com/user-attachments/assets/d111273d-579f-44c2-82f5-aa32d6a1879a"
/>

Note that importing virtual modules directly in CSS does not work as the
resolver we use does not resolve against the Vite runtime it seems. This
is unrelated to the regression added in `4.0.8` though and something to
look into in the future.
  • Loading branch information
philipp-spiess authored Feb 25, 2025
1 parent 37ea0c3 commit 59e003e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- _Experimental_: Add `user-valid` and `user-invalid` variants ([#12370](https://github.com/tailwindlabs/tailwindcss/pull/12370))
- _Experimental_: Add `wrap-anywhere`, `wrap-break-word`, and `wrap-normal` utilities ([#12128](https://github.com/tailwindlabs/tailwindcss/pull/12128))

### Fixed

- Vite: Don't crash when importing a virtual module in JavaScript that ends in `.css` ([#16780](https://github.com/tailwindlabs/tailwindcss/pull/16780))

## [4.0.8] - 2025-02-21

### Added
Expand Down
Binary file added integrations/vite/virtual-modules.test.ts
Binary file not shown.
21 changes: 15 additions & 6 deletions packages/@tailwindcss-vite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,9 @@ class Root {
private candidates: Set<string> = new Set<string>()

// List of all build dependencies (e.g. imported stylesheets or plugins) and
// their last modification timestamp
private buildDependencies = new Map<string, number>()
// their last modification timestamp. If no mtime can be found, we need to
// assume the file has always changed.
private buildDependencies = new Map<string, number | null>()

constructor(
private id: string,
Expand Down Expand Up @@ -334,14 +335,22 @@ class Root {
}

private async addBuildDependency(path: string) {
let stat = await fs.stat(path)
this.buildDependencies.set(path, stat.mtimeMs)
let mtime: number | null = null
try {
mtime = (await fs.stat(path)).mtimeMs
} catch {}
this.buildDependencies.set(path, mtime)
}

private async requiresBuild(): Promise<boolean> {
for (let [path, mtime] of this.buildDependencies) {
let stat = await fs.stat(path)
if (stat.mtimeMs > mtime) {
if (mtime === null) return true
try {
let stat = await fs.stat(path)
if (stat.mtimeMs > mtime) {
return true
}
} catch {
return true
}
}
Expand Down

0 comments on commit 59e003e

Please sign in to comment.