Skip to content

Vite: Don't crash with virtual module dependencies #16780

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

Merged
merged 4 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member Author

@philipp-spiess philipp-spiess Feb 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a catch here as well, pretty sure deleting a dependency right now is no bueno 😅

return true
}
}
Expand Down