Skip to content

Commit fed438b

Browse files
committed
fix: watch twind config for changes
1 parent 7857450 commit fed438b

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

src/twind.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { getConfig } from './load-twind-config'
2424
import { getColor } from './colors'
2525

2626
import type { ConfigurationManager } from './configuration'
27+
import { watch } from './watch'
2728

2829
const isCSSProperty = (key: string, value: CSSRuleValue): boolean =>
2930
!'@:&'.includes(key[0]) && ('rg'.includes((typeof value)[5]) || Array.isArray(value))
@@ -194,10 +195,8 @@ export interface Completions {
194195
export class Twind {
195196
private readonly typescript: typeof TS
196197
private readonly info: ts.server.PluginCreateInfo
197-
private readonly configurationManager: ConfigurationManager
198198
private readonly logger: Logger
199199
private _completions: Completions | undefined
200-
private _configFile: string | undefined
201200
private _state:
202201
| {
203202
program: TS.Program
@@ -217,8 +216,14 @@ export class Twind {
217216
) {
218217
this.typescript = typescript
219218
this.info = info
220-
this.configurationManager = configurationManager
221219
this.logger = logger
220+
221+
configurationManager.onUpdatedConfig(() => this._reset())
222+
// TODO watch changes to package.json, package-lock.json, yarn.lock, pnpm-lock.yaml
223+
}
224+
225+
private _reset(): void {
226+
this._state = this._completions = undefined
222227
}
223228

224229
private get state() {
@@ -234,7 +239,14 @@ export class Twind {
234239

235240
const { configFile, ...config } = getConfig(program.getCurrentDirectory())
236241

237-
this._configFile = configFile
242+
if (configFile) {
243+
this.logger.log(`Loaded twind config from ${configFile}`)
244+
245+
// Resez all state on config file changes
246+
watch(configFile, () => this._reset())
247+
} else {
248+
this.logger.log(`No twind config found`)
249+
}
238250

239251
const sheet = virtualSheet()
240252
const reports: ReportInfo[] = []
@@ -251,13 +263,14 @@ export class Twind {
251263
reports.push(info)
252264
},
253265
},
254-
preflight: false,
255-
prefix: false,
256266
plugins: {
257267
...config.plugins,
258268
// Used to generate CSS for variants
259269
TYPESCRIPT_PLUGIN_PLACEHOLDER: { '--typescript_plugin_placeholder': 'none' },
260270
},
271+
preflight: false,
272+
hash: false,
273+
prefix: false,
261274
})
262275

263276
let context: Context

src/watch.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as fs from 'fs'
2+
3+
export function watch(
4+
file: string,
5+
listener: (event: 'rename' | 'change', filename: string) => void,
6+
): void {
7+
try {
8+
const watcher = fs.watch(
9+
file,
10+
{
11+
persistent: false,
12+
},
13+
(event, filename) => {
14+
watcher.close()
15+
listener(event, filename)
16+
},
17+
)
18+
} catch (error) {
19+
if (error.code !== 'ERR_FEATURE_UNAVAILABLE_ON_PLATFORM') {
20+
throw error
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)