fix re-init short-circuit

now it takes config dependencies into account
master
Brad Cornes 2021-05-05 17:56:45 +01:00
parent 137a6c1b61
commit e08ac09959
4 changed files with 22 additions and 21 deletions

View File

@ -64,7 +64,7 @@ export interface FeatureFlags {
export interface State { export interface State {
enabled: boolean enabled: boolean
configPath?: string configPath?: string
configModified?: number configId?: string
config?: any config?: any
version?: string version?: string
separator?: string separator?: string

View File

@ -6,7 +6,6 @@ import Module from 'module'
export default class Hook { export default class Hook {
cache = {} cache = {}
deps: string[] = [] deps: string[] = []
private _watching: boolean = false
private _unhooked: boolean = false private _unhooked: boolean = false
private _origRequire = Module.prototype.require private _origRequire = Module.prototype.require
private _require: (req: string) => any private _require: (req: string) => any
@ -50,9 +49,6 @@ export default class Hook {
let exports = self._origRequire.apply(this, arguments) let exports = self._origRequire.apply(this, arguments)
if (filename !== find) { if (filename !== find) {
if (self._watching) {
self.deps.push(filename)
}
return exports return exports
} }
@ -81,12 +77,4 @@ export default class Hook {
Module.prototype.require = this._origRequire Module.prototype.require = this._origRequire
} }
} }
watch() {
this._watching = true
}
unwatch() {
this._watching = false
}
} }

View File

@ -68,6 +68,7 @@ import { doCodeActions } from 'tailwindcss-language-service/src/codeActions/code
import { getDocumentColors } from 'tailwindcss-language-service/src/documentColorProvider' import { getDocumentColors } from 'tailwindcss-language-service/src/documentColorProvider'
import { fromRatio, names as namedColors } from '@ctrl/tinycolor' import { fromRatio, names as namedColors } from '@ctrl/tinycolor'
import { debounce } from 'debounce' import { debounce } from 'debounce'
import { getModuleDependencies } from './util/getModuleDependencies'
// import postcssLoadConfig from 'postcss-load-config' // import postcssLoadConfig from 'postcss-load-config'
const CONFIG_FILE_GLOB = 'tailwind.config.{js,cjs}' const CONFIG_FILE_GLOB = 'tailwind.config.{js,cjs}'
@ -123,6 +124,12 @@ function deletePropertyPath(obj: any, path: string | string[]): void {
delete obj[path.pop()] 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 { interface ProjectService {
state: State state: State
tryInit: () => Promise<void> tryInit: () => Promise<void>
@ -335,7 +342,8 @@ async function createProjectService(
setPnpApi(pnpApi) setPnpApi(pnpApi)
} }
const configModified = fs.statSync(configPath).mtimeMs const configDependencies = getModuleDependencies(configPath)
const configId = getConfigId(configPath, configDependencies)
const configDir = path.dirname(configPath) const configDir = path.dirname(configPath)
let tailwindcss: any let tailwindcss: any
let postcss: any let postcss: any
@ -372,7 +380,7 @@ async function createProjectService(
postcssVersion === state.modules.postcss.version && postcssVersion === state.modules.postcss.version &&
tailwindcssVersion === state.modules.tailwindcss.version && tailwindcssVersion === state.modules.tailwindcss.version &&
configPath === state.configPath && configPath === state.configPath &&
configModified === state.configModified configId === state.configId
) { ) {
return return
} }
@ -452,7 +460,6 @@ async function createProjectService(
} }
state.configPath = configPath state.configPath = configPath
state.configModified = configModified
state.modules = { state.modules = {
tailwindcss: { version: tailwindcssVersion, module: tailwindcss }, tailwindcss: { version: tailwindcssVersion, module: tailwindcss },
postcss: { version: postcssVersion, module: postcss }, postcss: { version: postcssVersion, module: postcss },
@ -593,18 +600,14 @@ async function createProjectService(
return exports return exports
}) })
hook.watch()
let config let config
try { try {
config = __non_webpack_require__(state.configPath) config = __non_webpack_require__(state.configPath)
} catch (error) { } catch (error) {
hook.unwatch()
hook.unhook() hook.unhook()
throw error throw error
} }
hook.unwatch()
let postcssResult: Result let postcssResult: Result
try { try {
postcssResult = await postcss postcssResult = await postcss
@ -656,9 +659,12 @@ async function createProjectService(
if (state.dependencies) { if (state.dependencies) {
watcher.unwatch(state.dependencies) watcher.unwatch(state.dependencies)
} }
state.dependencies = hook.deps state.dependencies = getModuleDependencies(state.configPath)
console.log({ deps: state.dependencies })
watcher.add(state.dependencies) watcher.add(state.dependencies)
state.configId = getConfigId(state.configPath, state.dependencies)
state.config = resolveConfig.module(config) state.config = resolveConfig.module(config)
state.separator = typeof userSeperator === 'string' ? userSeperator : ':' state.separator = typeof userSeperator === 'string' ? userSeperator : ':'
state.plugins = await getPlugins(config) state.plugins = await getPlugins(config)

View File

@ -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)
}