-
Notifications
You must be signed in to change notification settings - Fork 218
Don't create v4 projects for CSS files that don't look like v4 configs #1164
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
+199
−21
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
568c100
Refactor
thecrypticace 220ec9e
Skip “root” CSS files that don’t look like they use v4
thecrypticace 043d2bb
Update changelog
thecrypticace ce0251c
Merge branch 'main' into fix/skip-v3-css-files
thecrypticace ae80ad2
Check for `@reference`
thecrypticace fb39249
Check for v4’s custom functions
thecrypticace a218caa
Remove unnvessary check
thecrypticace 7f55e84
Add tests
thecrypticace File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
packages/tailwindcss-language-server/src/version-guesser.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
export type TailwindVersion = '3' | '4' | ||
|
||
/** | ||
* Determine the likely Tailwind version used by the given file | ||
* | ||
* This returns an array of possible versions, as a file could contain | ||
* features that make determining the version ambiguous. | ||
* | ||
* The order *does* matter, as the first item is the most likely version. | ||
*/ | ||
export function guessTailwindVersion(content: string): TailwindVersion[] { | ||
// It's likely this is a v4 file if it has a v4 import: | ||
// - `@import "tailwindcss"` | ||
// - `@import "tailwindcss/theme" | ||
// - etc… | ||
let HAS_V4_IMPORT = /@import\s*['"]tailwindcss(?:\/[^'"]+)?['"]/ | ||
if (HAS_V4_IMPORT.test(content)) return ['4'] | ||
|
||
// It's likely this is a v4 file if it has a v4-specific feature: | ||
// - @theme | ||
// - @plugin | ||
// - @utility | ||
// - @variant | ||
// - @custom-variant | ||
let HAS_V4_DIRECTIVE = /@(theme|plugin|utility|custom-variant|variant|reference)\s*[^;{]+[;{]/ | ||
if (HAS_V4_DIRECTIVE.test(content)) return ['4'] | ||
|
||
// It's likely this is a v4 file if it's using v4's custom functions: | ||
// - --alpha(…) | ||
// - --spacing(…) | ||
// - --theme(…) | ||
let HAS_V4_FN = /--(alpha|spacing|theme)\(/ | ||
if (HAS_V4_FN.test(content)) return ['4'] | ||
|
||
// If the file contains older `@tailwind` directives, it's likely a v3 file | ||
let HAS_LEGACY_TAILWIND = /@tailwind\s*(base|preflight|components|variants|screens)+;/ | ||
if (HAS_LEGACY_TAILWIND.test(content)) return ['3'] | ||
|
||
// If the file contains other `@tailwind` directives it might be either | ||
let HAS_TAILWIND = /@tailwind\s*[^;]+;/ | ||
if (HAS_TAILWIND.test(content)) return ['4', '3'] | ||
|
||
// If the file contains other `@apply` or `@config` it might be either | ||
let HAS_COMMON_DIRECTIVE = /@(config|apply)\s*[^;{]+[;{]/ | ||
if (HAS_COMMON_DIRECTIVE.test(content)) return ['4', '3'] | ||
|
||
// If it's got imports at all it could be either | ||
let HAS_IMPORT = /@import\s*['"]/ | ||
if (HAS_IMPORT.test(content)) return ['4', '3'] | ||
|
||
// There's chance this file isn't tailwind-related | ||
return [] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.