Skip to content

Improve insiders version detection #1123

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 5 commits into from
Jan 29, 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
20 changes: 17 additions & 3 deletions packages/tailwindcss-language-server/src/project-locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,12 +425,25 @@ export class ProjectLocator {

private async detectTailwindVersion(config: ConfigEntry) {
try {
let metadataPath = await this.resolver.resolveJsId(
let metadataPath = await this.resolver.resolveCjsId(
'tailwindcss/package.json',
path.dirname(config.path),
)

let { version } = require(metadataPath)
let features = supportedFeatures(version)

let mod: unknown = undefined

if (this.resolver.hasPnP()) {
let modPath = await this.resolver.resolveCjsId('tailwindcss', path.dirname(config.path))
mod = require(modPath)
} else {
let modPath = await this.resolver.resolveJsId('tailwindcss', path.dirname(config.path))
let modURL = pathToFileURL(modPath).href
mod = await import(modURL)
}

let features = supportedFeatures(version, mod)

if (typeof version === 'string') {
return {
Expand All @@ -442,7 +455,8 @@ export class ProjectLocator {
} catch {}

let { version } = require('tailwindcss/package.json')
let features = supportedFeatures(version)
let mod = require('tailwindcss')
let features = supportedFeatures(version, mod)

return {
version,
Expand Down
8 changes: 5 additions & 3 deletions packages/tailwindcss-language-server/src/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,6 @@ export async function createProjectService(
let tailwindDir = path.dirname(tailwindcssPkgPath)
tailwindcssVersion = require(tailwindcssPkgPath).version

let features = supportedFeatures(tailwindcssVersion)
log(`supported features: ${JSON.stringify(features)}`)

// Loading via `await import(…)` with the Yarn PnP API is not possible
if (await resolver.hasPnP()) {
let tailwindcssPath = await resolver.resolveCjsId('tailwindcss', configDir)
Expand All @@ -461,6 +458,11 @@ export async function createProjectService(
tailwindcss = await import(tailwindcssURL)
}

// TODO: The module should be loaded in the project locator
// and this should be determined there and passed in instead
let features = supportedFeatures(tailwindcssVersion, tailwindcss)
log(`supported features: ${JSON.stringify(features)}`)

if (!features.includes('css-at-theme')) {
tailwindcss = tailwindcss.default ?? tailwindcss
}
Expand Down
22 changes: 20 additions & 2 deletions packages/tailwindcss-language-service/src/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,28 @@ export type Feature =
/**
* Determine a list of features that are supported by the given Tailwind CSS version
*/
export function supportedFeatures(version: string): Feature[] {
export function supportedFeatures(version: string, mod?: unknown): Feature[] {
let features: Feature[] = []

let isInsidersV3 = version.startsWith('0.0.0-insiders')
let isInsiders = version.startsWith('0.0.0-insiders')
let isInsidersV3 = false
let isInsidersV4 = false

if (isInsiders) {
if (mod && typeof mod === 'object' && 'compile' in mod) {
// ESM version for normal installations
isInsidersV4 = true
} else if (mod && typeof mod === 'function' && 'compile' in mod) {
// Common JS version for Yarn PnP support
isInsidersV4 = true
} else {
isInsidersV3 = true
}
}

if (isInsidersV4) {
return ['css-at-theme', 'layer:base', 'content-list']
}

if (!isInsidersV3 && semver.gte(version, '4.0.0-alpha.1')) {
return ['css-at-theme', 'layer:base', 'content-list']
Expand Down
8 changes: 8 additions & 0 deletions packages/tailwindcss-language-service/src/util/docsUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ export function docsUrl(version: string, paths: string | string[]): string {
}
if (semver.gte(version, '1.99.0')) {
major = 2
url = 'https://v2.tailwindcss.com/docs/'
}
if (semver.gte(version, '2.99.0')) {
major = 3
url = 'https://v3.tailwindcss.com/docs/'
}
if (semver.gte(version, '3.99.0')) {
major = 4
url = 'https://tailwindcss.com/docs/'
}
const path = Array.isArray(paths) ? paths[major] || paths[paths.length - 1] : paths
Expand Down
1 change: 1 addition & 0 deletions packages/vscode-tailwindcss/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Don't suggest `--font-size-*` theme keys in v4.0 ([#1150](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1150))
- Fix detection of Tailwind CSS version when using Yarn PnP ([#1151](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1151))
- Add support for v4.x insiders builds ([#1123](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1123))

## 0.14.1

Expand Down
3 changes: 2 additions & 1 deletion packages/vscode-tailwindcss/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ async function activeTextEditorSupportsClassSorting(): Promise<boolean> {
if (!project) {
return false
}
// TODO: Use feature detection instead of version checking
return semver.gte(project.version, '3.0.0')
}

Expand Down Expand Up @@ -573,7 +574,7 @@ export async function activate(context: ExtensionContext) {
return
}

if (!await anyFolderNeedsLanguageServer(Workspace.workspaceFolders ?? [])) {
if (!(await anyFolderNeedsLanguageServer(Workspace.workspaceFolders ?? []))) {
return
}

Expand Down