From e08ac0995939cf186d7441ef4a76e2af5630ba3a Mon Sep 17 00:00:00 2001 From: Brad Cornes Date: Wed, 5 May 2021 17:56:45 +0100 Subject: [PATCH] fix re-init short-circuit now it takes config dependencies into account --- .../src/util/state.ts | 2 +- src/lib/hook.ts | 12 ---------- src/server.ts | 22 ++++++++++++------- src/util/getModuleDependencies.ts | 7 ++++++ 4 files changed, 22 insertions(+), 21 deletions(-) create mode 100644 src/util/getModuleDependencies.ts diff --git a/packages/tailwindcss-language-service/src/util/state.ts b/packages/tailwindcss-language-service/src/util/state.ts index 72dc427..c1e58e4 100644 --- a/packages/tailwindcss-language-service/src/util/state.ts +++ b/packages/tailwindcss-language-service/src/util/state.ts @@ -64,7 +64,7 @@ export interface FeatureFlags { export interface State { enabled: boolean configPath?: string - configModified?: number + configId?: string config?: any version?: string separator?: string diff --git a/src/lib/hook.ts b/src/lib/hook.ts index f7e11ea..f616272 100644 --- a/src/lib/hook.ts +++ b/src/lib/hook.ts @@ -6,7 +6,6 @@ import Module from 'module' export default class Hook { cache = {} deps: string[] = [] - private _watching: boolean = false private _unhooked: boolean = false private _origRequire = Module.prototype.require private _require: (req: string) => any @@ -50,9 +49,6 @@ export default class Hook { let exports = self._origRequire.apply(this, arguments) if (filename !== find) { - if (self._watching) { - self.deps.push(filename) - } return exports } @@ -81,12 +77,4 @@ export default class Hook { Module.prototype.require = this._origRequire } } - - watch() { - this._watching = true - } - - unwatch() { - this._watching = false - } } diff --git a/src/server.ts b/src/server.ts index 1651cbe..88f9bf0 100644 --- a/src/server.ts +++ b/src/server.ts @@ -68,6 +68,7 @@ import { doCodeActions } from 'tailwindcss-language-service/src/codeActions/code import { getDocumentColors } from 'tailwindcss-language-service/src/documentColorProvider' import { fromRatio, names as namedColors } from '@ctrl/tinycolor' import { debounce } from 'debounce' +import { getModuleDependencies } from './util/getModuleDependencies' // import postcssLoadConfig from 'postcss-load-config' const CONFIG_FILE_GLOB = 'tailwind.config.{js,cjs}' @@ -123,6 +124,12 @@ function deletePropertyPath(obj: any, path: string | string[]): void { delete obj[path.pop()] } +function getConfigId(configPath: string, configDependencies: string[]): string { + return JSON.stringify( + [configPath, ...configDependencies].map((file) => [file, fs.statSync(file).mtimeMs]) + ) +} + interface ProjectService { state: State tryInit: () => Promise @@ -335,7 +342,8 @@ async function createProjectService( setPnpApi(pnpApi) } - const configModified = fs.statSync(configPath).mtimeMs + const configDependencies = getModuleDependencies(configPath) + const configId = getConfigId(configPath, configDependencies) const configDir = path.dirname(configPath) let tailwindcss: any let postcss: any @@ -372,7 +380,7 @@ async function createProjectService( postcssVersion === state.modules.postcss.version && tailwindcssVersion === state.modules.tailwindcss.version && configPath === state.configPath && - configModified === state.configModified + configId === state.configId ) { return } @@ -452,7 +460,6 @@ async function createProjectService( } state.configPath = configPath - state.configModified = configModified state.modules = { tailwindcss: { version: tailwindcssVersion, module: tailwindcss }, postcss: { version: postcssVersion, module: postcss }, @@ -593,18 +600,14 @@ async function createProjectService( return exports }) - hook.watch() let config try { config = __non_webpack_require__(state.configPath) } catch (error) { - hook.unwatch() hook.unhook() throw error } - hook.unwatch() - let postcssResult: Result try { postcssResult = await postcss @@ -656,9 +659,12 @@ async function createProjectService( if (state.dependencies) { watcher.unwatch(state.dependencies) } - state.dependencies = hook.deps + state.dependencies = getModuleDependencies(state.configPath) + console.log({ deps: state.dependencies }) watcher.add(state.dependencies) + state.configId = getConfigId(state.configPath, state.dependencies) + state.config = resolveConfig.module(config) state.separator = typeof userSeperator === 'string' ? userSeperator : ':' state.plugins = await getPlugins(config) diff --git a/src/util/getModuleDependencies.ts b/src/util/getModuleDependencies.ts new file mode 100644 index 0000000..93f2752 --- /dev/null +++ b/src/util/getModuleDependencies.ts @@ -0,0 +1,7 @@ +import _getModuleDependencies from 'tailwindcss/lib/lib/getModuleDependencies' + +export function getModuleDependencies(modulePath: string): string[] { + return _getModuleDependencies(modulePath) + .map(({ file }) => file) + .filter((file) => file !== modulePath) +}