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 {
enabled: boolean
configPath?: string
configModified?: number
configId?: string
config?: any
version?: string
separator?: string

View File

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

View File

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

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