Skip to content

Commit e73b17b

Browse files
committed
Reload when tsconfig files change
1 parent 11adef9 commit e73b17b

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-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: 23 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,12 @@ export class TW {
335337
}
336338
}
337339

340+
let isTsconfig = isTSConfigMatcher(normalizedFilename)
341+
if (isTsconfig) {
342+
needsSoftRestart = true
343+
break changeLoop
344+
}
345+
338346
for (let [, project] of this.projects) {
339347
if (!project.state.v4) continue
340348

@@ -391,6 +399,18 @@ export class TW {
391399
}
392400

393401
if (needsSoftRestart) {
402+
let refreshPromise = retry({
403+
tries: 4,
404+
delay: 250,
405+
callback: () => resolver.refresh(),
406+
})
407+
408+
try {
409+
await refreshPromise
410+
} catch (err) {
411+
console.error('Unable to reload resolver', err)
412+
}
413+
394414
try {
395415
await this.softRestart()
396416
} catch {
@@ -429,6 +449,7 @@ export class TW {
429449
{ globPattern: `**/${CONFIG_GLOB}` },
430450
{ globPattern: `**/${PACKAGE_LOCK_GLOB}` },
431451
{ globPattern: `**/${CSS_GLOB}` },
452+
{ globPattern: `**/${TSCONFIG_GLOB}` },
432453
],
433454
},
434455
)
@@ -477,7 +498,7 @@ export class TW {
477498
} else {
478499
let watch: typeof chokidar.watch = require('chokidar').watch
479500
let chokidarWatcher = watch(
480-
[`**/${CONFIG_GLOB}`, `**/${PACKAGE_LOCK_GLOB}`, `**/${CSS_GLOB}`],
501+
[`**/${CONFIG_GLOB}`, `**/${PACKAGE_LOCK_GLOB}`, `**/${CSS_GLOB}`, `**/${TSCONFIG_GLOB}`],
481502
{
482503
cwd: base,
483504
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)