Skip to content

Commit cb0f305

Browse files
Support loading TypeScript configs and plugins in v4 projects (#1076)
Fixes #1072 We already have tests to cover this case but vitest hooks into module loading which lets us load TypeScript during tests. I tested it by running the tests against the server running as a separate node process rather than importing the sources and running the server in-process. The test harness is already set up for this so the only change needed to test this manually was this: <img width="416" alt="Screenshot 2024-10-25 at 15 43 28" src="https://github.com/user-attachments/assets/6dfc4d1e-e60e-4450-a34b-c231515922ed"> --------- Co-authored-by: Robin Malfait <malfait.robin@gmail.com>
1 parent 5798007 commit cb0f305

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

packages/tailwindcss-language-server/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"esbuild": "^0.20.2",
6767
"fast-glob": "3.2.4",
6868
"find-up": "5.0.0",
69+
"jiti": "^2.3.3",
6970
"klona": "2.0.4",
7071
"license-checker": "25.0.1",
7172
"minimist": "^1.2.8",

packages/tailwindcss-language-server/src/util/v4/design-system.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import type { DesignSystem } from '@tailwindcss/language-service/src/util/v4'
22

33
import postcss from 'postcss'
4+
import { createJiti } from 'jiti'
45
import * as fs from 'node:fs/promises'
56
import * as path from 'node:path'
67
import { resolveCssFrom, resolveCssImports } from '../../css'
78
import { resolveFrom } from '../resolveFrom'
89
import { pathToFileURL } from 'tailwindcss-language-server/src/utils'
10+
import type { Jiti } from 'jiti/lib/types'
911

1012
const HAS_V4_IMPORT = /@import\s*(?:'tailwindcss'|"tailwindcss")/
1113
const HAS_V4_THEME = /@theme\s*\{/
@@ -22,6 +24,20 @@ export async function isMaybeV4(css: string): Promise<boolean> {
2224
return HAS_V4_THEME.test(css) || HAS_V4_IMPORT.test(css)
2325
}
2426

27+
let jiti: Jiti | undefined
28+
29+
async function importFile(id: string) {
30+
try {
31+
// Load ESM/CJS files through Node/Bun/whatever runtime is being used
32+
return await import(id)
33+
} catch {
34+
jiti ??= createJiti(__filename, { moduleCache: false, fsCache: false })
35+
36+
// Transpile using Jiti if we can't load the file directly
37+
return await jiti.import(id)
38+
}
39+
}
40+
2541
/**
2642
* Create a loader function that can load plugins and config files relative to
2743
* the CSS file that uses them. However, we don't want missing files to prevent
@@ -45,7 +61,7 @@ function createLoader<T>({
4561
let url = pathToFileURL(resolved)
4662
url.searchParams.append('t', cacheKey)
4763

48-
return await import(url.href).then((m) => m.default ?? m)
64+
return await importFile(url.href).then((m) => m.default ?? m)
4965
} catch (err) {
5066
return onError(id, err, resourceType)
5167
}

packages/vscode-tailwindcss/CHANGELOG.md

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

55
- Fix display of color swatches using new v4 oklch color palette ([#1073](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1073))
66
- Properly validate `theme(…)` function paths in v4 ([#1074](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1074))
7+
- Support loading TypeScript configs and plugins in v4 projects ([#1076](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1076))
78

89
## 0.12.12
910

pnpm-lock.yaml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)