Skip to content

Commit c3bbd2f

Browse files
committed
Fix loading of ESM and TypeScript configs
1 parent ff6ad9b commit c3bbd2f

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ testFixture('multi-config-content', [
5757
{ config: 'tailwind.config.two.js' },
5858
])
5959

60+
61+
testFixture('v3/esm-config', [
62+
//
63+
{ config: 'tailwind.config.mjs' },
64+
])
65+
66+
testFixture('v3/ts-config', [
67+
//
68+
{ config: 'tailwind.config.ts' },
69+
])
70+
71+
6072
testFixture('v4/basic', [
6173
//
6274
{ config: 'app.css' },

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,19 @@ async function* contentSelectorsFromJsConfig(
347347
features: Feature[],
348348
actualConfig?: any,
349349
): AsyncIterable<DocumentSelector> {
350-
let config = actualConfig ?? require(entry.path)
350+
let config: any
351+
352+
// This is wrapped in a try catch because a user might be using an ESM- or TypeScript-based config
353+
// and we don't want to stop the project from loading just because of that. We'll recover the list
354+
// of document selectors later in the loading process when initializing the project by using
355+
// Tailwind's `loadConfig` API. Ideally the configuration loading is either NOT done here or
356+
// all of it is done here. But that's a much larger refactor.
357+
try {
358+
config = actualConfig ?? require(entry.path)
359+
} catch {
360+
return
361+
}
362+
351363
let files: unknown = config.content?.files ?? config.content
352364
let content: (string | {})[] = Array.isArray(files) ? files : []
353365

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default {
2+
theme: {
3+
colors: { cool: 'blue' },
4+
},
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default {
2+
theme: {
3+
colors: { cool: 'blue' }
4+
}
5+
} satisfies {
6+
theme: Record<string, any>
7+
}

0 commit comments

Comments
 (0)