Skip to content

Commit 26168c8

Browse files
Improve insiders version detection (#1123)
This is to allow IntelliSense to detect v4 insiders builds while continuing to work with v3 insiders builds.
1 parent 1ea192e commit 26168c8

File tree

6 files changed

+53
-9
lines changed

6 files changed

+53
-9
lines changed

packages/tailwindcss-language-server/src/project-locator.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -425,12 +425,25 @@ export class ProjectLocator {
425425

426426
private async detectTailwindVersion(config: ConfigEntry) {
427427
try {
428-
let metadataPath = await this.resolver.resolveJsId(
428+
let metadataPath = await this.resolver.resolveCjsId(
429429
'tailwindcss/package.json',
430430
path.dirname(config.path),
431431
)
432+
432433
let { version } = require(metadataPath)
433-
let features = supportedFeatures(version)
434+
435+
let mod: unknown = undefined
436+
437+
if (this.resolver.hasPnP()) {
438+
let modPath = await this.resolver.resolveCjsId('tailwindcss', path.dirname(config.path))
439+
mod = require(modPath)
440+
} else {
441+
let modPath = await this.resolver.resolveJsId('tailwindcss', path.dirname(config.path))
442+
let modURL = pathToFileURL(modPath).href
443+
mod = await import(modURL)
444+
}
445+
446+
let features = supportedFeatures(version, mod)
434447

435448
if (typeof version === 'string') {
436449
return {
@@ -442,7 +455,8 @@ export class ProjectLocator {
442455
} catch {}
443456

444457
let { version } = require('tailwindcss/package.json')
445-
let features = supportedFeatures(version)
458+
let mod = require('tailwindcss')
459+
let features = supportedFeatures(version, mod)
446460

447461
return {
448462
version,

packages/tailwindcss-language-server/src/projects.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -446,9 +446,6 @@ export async function createProjectService(
446446
let tailwindDir = path.dirname(tailwindcssPkgPath)
447447
tailwindcssVersion = require(tailwindcssPkgPath).version
448448

449-
let features = supportedFeatures(tailwindcssVersion)
450-
log(`supported features: ${JSON.stringify(features)}`)
451-
452449
// Loading via `await import(…)` with the Yarn PnP API is not possible
453450
if (await resolver.hasPnP()) {
454451
let tailwindcssPath = await resolver.resolveCjsId('tailwindcss', configDir)
@@ -461,6 +458,11 @@ export async function createProjectService(
461458
tailwindcss = await import(tailwindcssURL)
462459
}
463460

461+
// TODO: The module should be loaded in the project locator
462+
// and this should be determined there and passed in instead
463+
let features = supportedFeatures(tailwindcssVersion, tailwindcss)
464+
log(`supported features: ${JSON.stringify(features)}`)
465+
464466
if (!features.includes('css-at-theme')) {
465467
tailwindcss = tailwindcss.default ?? tailwindcss
466468
}

packages/tailwindcss-language-service/src/features.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,28 @@ export type Feature =
1919
/**
2020
* Determine a list of features that are supported by the given Tailwind CSS version
2121
*/
22-
export function supportedFeatures(version: string): Feature[] {
22+
export function supportedFeatures(version: string, mod?: unknown): Feature[] {
2323
let features: Feature[] = []
2424

25-
let isInsidersV3 = version.startsWith('0.0.0-insiders')
25+
let isInsiders = version.startsWith('0.0.0-insiders')
26+
let isInsidersV3 = false
27+
let isInsidersV4 = false
28+
29+
if (isInsiders) {
30+
if (mod && typeof mod === 'object' && 'compile' in mod) {
31+
// ESM version for normal installations
32+
isInsidersV4 = true
33+
} else if (mod && typeof mod === 'function' && 'compile' in mod) {
34+
// Common JS version for Yarn PnP support
35+
isInsidersV4 = true
36+
} else {
37+
isInsidersV3 = true
38+
}
39+
}
40+
41+
if (isInsidersV4) {
42+
return ['css-at-theme', 'layer:base', 'content-list']
43+
}
2644

2745
if (!isInsidersV3 && semver.gte(version, '4.0.0-alpha.1')) {
2846
return ['css-at-theme', 'layer:base', 'content-list']

packages/tailwindcss-language-service/src/util/docsUrl.ts

+8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ export function docsUrl(version: string, paths: string | string[]): string {
99
}
1010
if (semver.gte(version, '1.99.0')) {
1111
major = 2
12+
url = 'https://v2.tailwindcss.com/docs/'
13+
}
14+
if (semver.gte(version, '2.99.0')) {
15+
major = 3
16+
url = 'https://v3.tailwindcss.com/docs/'
17+
}
18+
if (semver.gte(version, '3.99.0')) {
19+
major = 4
1220
url = 'https://tailwindcss.com/docs/'
1321
}
1422
const path = Array.isArray(paths) ? paths[major] || paths[paths.length - 1] : paths

packages/vscode-tailwindcss/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

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

89
## 0.14.1
910

packages/vscode-tailwindcss/src/extension.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ async function activeTextEditorSupportsClassSorting(): Promise<boolean> {
157157
if (!project) {
158158
return false
159159
}
160+
// TODO: Use feature detection instead of version checking
160161
return semver.gte(project.version, '3.0.0')
161162
}
162163

@@ -573,7 +574,7 @@ export async function activate(context: ExtensionContext) {
573574
return
574575
}
575576

576-
if (!await anyFolderNeedsLanguageServer(Workspace.workspaceFolders ?? [])) {
577+
if (!(await anyFolderNeedsLanguageServer(Workspace.workspaceFolders ?? []))) {
577578
return
578579
}
579580

0 commit comments

Comments
 (0)