Skip to content

Commit 6bbcb6b

Browse files
committed
Reload when tsconfig files change
1 parent f0be47e commit 6bbcb6b

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

packages/tailwindcss-language-server/src/lib/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export const CONFIG_GLOB =
22
'{tailwind,tailwind.config,tailwind.*.config,tailwind.config.*}.{js,cjs,ts,mjs,mts,cts}'
33
export const PACKAGE_LOCK_GLOB = '{package-lock.json,yarn.lock,pnpm-lock.yaml}'
44
export const CSS_GLOB = '*.{css,scss,sass,less,pcss}'
5+
export const TSCONFIG_GLOB = '{tsconfig,tsconfig.*,jsconfig,jsconfig.*}.json'

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import picomatch from 'picomatch'
3939
import { resolveFrom } from './util/resolveFrom'
4040
import * as parcel from './watcher/index.js'
4141
import { equal } from '@tailwindcss/language-service/src/util/array'
42-
import { CONFIG_GLOB, CSS_GLOB, PACKAGE_LOCK_GLOB } from './lib/constants'
42+
import { CONFIG_GLOB, CSS_GLOB, PACKAGE_LOCK_GLOB, TSCONFIG_GLOB } from './lib/constants'
4343
import { clearRequireCache, isObject, changeAffectsFile, normalizeDriveLetter } from './utils'
4444
import { DocumentService } from './documents'
4545
import { createProjectService, type ProjectService } from './projects'
@@ -48,6 +48,7 @@ import { readCssFile } from './util/css'
4848
import { ProjectLocator, type ProjectConfig } from './project-locator'
4949
import type { TailwindCssSettings } from '@tailwindcss/language-service/src/util/state'
5050
import { createResolver, Resolver } from './resolver'
51+
import { retry } from './util/retry'
5152

5253
const TRIGGER_CHARACTERS = [
5354
// class attributes
@@ -296,6 +297,7 @@ export class TW {
296297
let isPackageMatcher = picomatch(`**/${PACKAGE_LOCK_GLOB}`, { dot: true })
297298
let isCssMatcher = picomatch(`**/${CSS_GLOB}`, { dot: true })
298299
let isConfigMatcher = picomatch(`**/${CONFIG_GLOB}`, { dot: true })
300+
let isTSConfigMatcher = picomatch(`**/${TSCONFIG_GLOB}`, { dot: true })
299301

300302
changeLoop: for (let change of changes) {
301303
let normalizedFilename = normalizePath(change.file)
@@ -335,6 +337,25 @@ export class TW {
335337
}
336338
}
337339

340+
let isTsconfig = isTSConfigMatcher(normalizedFilename)
341+
if (isTsconfig) {
342+
// TODO: Use a refresh() instead of a full server restart
343+
// let refreshPromise = retry({
344+
// tries: 4,
345+
// delay: 250,
346+
// callback: () => resolver.refresh(),
347+
// })
348+
349+
// try {
350+
// await refreshPromise
351+
// } catch (err) {
352+
// console.error('Unable to reload resolver', err)
353+
// }
354+
355+
needsRestart = true
356+
break changeLoop
357+
}
358+
338359
for (let [, project] of this.projects) {
339360
if (!project.state.v4) continue
340361

@@ -424,6 +445,7 @@ export class TW {
424445
{ globPattern: `**/${CONFIG_GLOB}` },
425446
{ globPattern: `**/${PACKAGE_LOCK_GLOB}` },
426447
{ globPattern: `**/${CSS_GLOB}` },
448+
{ globPattern: `**/${TSCONFIG_GLOB}` },
427449
],
428450
},
429451
)
@@ -472,7 +494,7 @@ export class TW {
472494
} else {
473495
let watch: typeof chokidar.watch = require('chokidar').watch
474496
let chokidarWatcher = watch(
475-
[`**/${CONFIG_GLOB}`, `**/${PACKAGE_LOCK_GLOB}`, `**/${CSS_GLOB}`],
497+
[`**/${CONFIG_GLOB}`, `**/${PACKAGE_LOCK_GLOB}`, `**/${CSS_GLOB}`, `**/${TSCONFIG_GLOB}`],
476498
{
477499
cwd: base,
478500
ignorePermissionErrors: true,
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export interface RetryOptions {
2+
tries: number
3+
delay: number
4+
callback: () => Promise<any>
5+
}
6+
7+
export async function retry<T>({ tries, delay, callback }) {
8+
retry: try {
9+
return await callback()
10+
} catch (err) {
11+
if (tries-- === 0) throw err
12+
13+
// Wait a bit before trying again _ this exists for projects like
14+
// Nuxt that create a several tsconfig files at once
15+
await new Promise((resolve) => setTimeout(resolve, delay))
16+
17+
break retry
18+
}
19+
}

0 commit comments

Comments
 (0)