Skip to content

Commit e08ac09

Browse files
committed
fix re-init short-circuit
now it takes config dependencies into account
1 parent 137a6c1 commit e08ac09

File tree

4 files changed

+22
-21
lines changed

4 files changed

+22
-21
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export interface FeatureFlags {
6464
export interface State {
6565
enabled: boolean
6666
configPath?: string
67-
configModified?: number
67+
configId?: string
6868
config?: any
6969
version?: string
7070
separator?: string

src/lib/hook.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import Module from 'module'
66
export default class Hook {
77
cache = {}
88
deps: string[] = []
9-
private _watching: boolean = false
109
private _unhooked: boolean = false
1110
private _origRequire = Module.prototype.require
1211
private _require: (req: string) => any
@@ -50,9 +49,6 @@ export default class Hook {
5049
let exports = self._origRequire.apply(this, arguments)
5150

5251
if (filename !== find) {
53-
if (self._watching) {
54-
self.deps.push(filename)
55-
}
5652
return exports
5753
}
5854

@@ -81,12 +77,4 @@ export default class Hook {
8177
Module.prototype.require = this._origRequire
8278
}
8379
}
84-
85-
watch() {
86-
this._watching = true
87-
}
88-
89-
unwatch() {
90-
this._watching = false
91-
}
9280
}

src/server.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ import { doCodeActions } from 'tailwindcss-language-service/src/codeActions/code
6868
import { getDocumentColors } from 'tailwindcss-language-service/src/documentColorProvider'
6969
import { fromRatio, names as namedColors } from '@ctrl/tinycolor'
7070
import { debounce } from 'debounce'
71+
import { getModuleDependencies } from './util/getModuleDependencies'
7172
// import postcssLoadConfig from 'postcss-load-config'
7273

7374
const CONFIG_FILE_GLOB = 'tailwind.config.{js,cjs}'
@@ -123,6 +124,12 @@ function deletePropertyPath(obj: any, path: string | string[]): void {
123124
delete obj[path.pop()]
124125
}
125126

127+
function getConfigId(configPath: string, configDependencies: string[]): string {
128+
return JSON.stringify(
129+
[configPath, ...configDependencies].map((file) => [file, fs.statSync(file).mtimeMs])
130+
)
131+
}
132+
126133
interface ProjectService {
127134
state: State
128135
tryInit: () => Promise<void>
@@ -335,7 +342,8 @@ async function createProjectService(
335342
setPnpApi(pnpApi)
336343
}
337344

338-
const configModified = fs.statSync(configPath).mtimeMs
345+
const configDependencies = getModuleDependencies(configPath)
346+
const configId = getConfigId(configPath, configDependencies)
339347
const configDir = path.dirname(configPath)
340348
let tailwindcss: any
341349
let postcss: any
@@ -372,7 +380,7 @@ async function createProjectService(
372380
postcssVersion === state.modules.postcss.version &&
373381
tailwindcssVersion === state.modules.tailwindcss.version &&
374382
configPath === state.configPath &&
375-
configModified === state.configModified
383+
configId === state.configId
376384
) {
377385
return
378386
}
@@ -452,7 +460,6 @@ async function createProjectService(
452460
}
453461

454462
state.configPath = configPath
455-
state.configModified = configModified
456463
state.modules = {
457464
tailwindcss: { version: tailwindcssVersion, module: tailwindcss },
458465
postcss: { version: postcssVersion, module: postcss },
@@ -593,18 +600,14 @@ async function createProjectService(
593600
return exports
594601
})
595602

596-
hook.watch()
597603
let config
598604
try {
599605
config = __non_webpack_require__(state.configPath)
600606
} catch (error) {
601-
hook.unwatch()
602607
hook.unhook()
603608
throw error
604609
}
605610

606-
hook.unwatch()
607-
608611
let postcssResult: Result
609612
try {
610613
postcssResult = await postcss
@@ -656,9 +659,12 @@ async function createProjectService(
656659
if (state.dependencies) {
657660
watcher.unwatch(state.dependencies)
658661
}
659-
state.dependencies = hook.deps
662+
state.dependencies = getModuleDependencies(state.configPath)
663+
console.log({ deps: state.dependencies })
660664
watcher.add(state.dependencies)
661665

666+
state.configId = getConfigId(state.configPath, state.dependencies)
667+
662668
state.config = resolveConfig.module(config)
663669
state.separator = typeof userSeperator === 'string' ? userSeperator : ':'
664670
state.plugins = await getPlugins(config)

src/util/getModuleDependencies.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import _getModuleDependencies from 'tailwindcss/lib/lib/getModuleDependencies'
2+
3+
export function getModuleDependencies(modulePath: string): string[] {
4+
return _getModuleDependencies(modulePath)
5+
.map(({ file }) => file)
6+
.filter((file) => file !== modulePath)
7+
}

0 commit comments

Comments
 (0)