@@ -250,12 +250,12 @@ export class Twind {
250
250
)
251
251
252
252
if ( configFile ) {
253
- this . logger . log ( `Loaded twind config from ${ configFile } ` )
253
+ this . logger . log ( `loaded twind config from ${ configFile } ` )
254
254
255
255
// Reset all state on config file changes
256
256
this . _watchers . push ( watch ( configFile , ( ) => this . _reset ( ) , { once : true } ) )
257
257
} else {
258
- this . logger . log ( `No twind config found` )
258
+ this . logger . log ( `no local twind config found` )
259
259
}
260
260
261
261
const sheet = virtualSheet ( )
@@ -269,7 +269,9 @@ export class Twind {
269
269
. resolveModuleNames ( [ 'twind' ] , program . getRootFileNames ( ) [ 0 ] )
270
270
. map ( ( moduleName ) => moduleName ?. resolvedFileName ) [ 0 ]
271
271
272
- this . logger . log ( 'local twind dts: ' + twindDTSFile )
272
+ if ( twindDTSFile ) {
273
+ this . logger . log ( `found local twind dts at ${ twindDTSFile } ` )
274
+ }
273
275
274
276
let twindDTSSourceFile =
275
277
( twindDTSFile &&
@@ -280,32 +282,36 @@ export class Twind {
280
282
281
283
// No local twind but a twind.config -> use our twind
282
284
if (
283
- ! twindDTSFile &&
284
285
! twindDTSSourceFile &&
286
+ ! twindDTSFile &&
285
287
configFile &&
286
288
/ t w i n d \. c o n f i g \. \w + $ / . test ( configFile )
287
289
) {
288
290
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
289
291
// @ts -ignore
290
- const from : string | URL = import . meta. url || pathToFileURL ( __filename )
292
+ const from = fileURLToPath ( import . meta. url )
291
293
292
- const { resolve } =
293
- Module . createRequire ?.( from ) || Module . createRequireFromPath ( fileURLToPath ( from ) )
294
+ const { resolve } = Module . createRequire ?.( from ) || Module . createRequireFromPath ( from )
294
295
295
296
try {
296
297
twindDTSFile = resolve ( 'twind' ) . replace ( / \. \w + $ / , '.d.ts' )
297
- this . logger . log ( 'builtin twind dts: ' + twindDTSFile )
298
+ if ( twindDTSFile ) {
299
+ this . logger . log ( `found builtin twind dts at ${ twindDTSFile } ` )
300
+ }
298
301
} catch {
299
302
// ignore
300
303
}
301
304
}
302
305
303
- if ( twindDTSFile ) {
306
+ if ( ! twindDTSSourceFile && twindDTSFile ) {
307
+ const options = program . getCompilerOptions ( )
308
+
304
309
program = this . typescript . createProgram ( {
305
- rootNames : [ ...program . getRootFileNames ( ) , twindDTSFile ] ,
306
- options : program . getCompilerOptions ( ) ,
307
- // projectReferences?: readonly ProjectReference[];
308
- // host?: program.getCom
310
+ rootNames : [ ...program . getRootFileNames ( ) , twindDTSFile . replace ( / \. d \. t s $ / , '.js' ) ] ,
311
+ options : {
312
+ ...options ,
313
+ typeRoots : [ ...( options . typeRoots || [ ] ) , path . dirname ( twindDTSFile ) ] ,
314
+ } ,
309
315
oldProgram : program ,
310
316
} )
311
317
@@ -314,19 +320,39 @@ export class Twind {
314
320
. find ( ( sourceFile ) => sourceFile . fileName . endsWith ( 'twind/twind.d.ts' ) )
315
321
}
316
322
317
- this . logger . log ( 'twindDTSSourceFile: ' + twindDTSSourceFile ?. fileName )
318
-
319
323
if ( twindDTSSourceFile ) {
324
+ this . logger . log ( `using twind completions from ${ twindDTSSourceFile . fileName } ` )
320
325
this . _watchers . push ( watch ( twindDTSSourceFile . fileName , ( ) => this . _reset ( ) , { once : true } ) )
326
+ } else {
327
+ this . logger . log ( `no twind completions found` )
321
328
}
322
329
323
330
const twindFile = twindDTSSourceFile ?. fileName . replace ( / \. d \. t s / , '.js' )
331
+ let version : string | undefined = 'undefined'
332
+
324
333
if ( twindFile ) {
325
334
this . _watchers . push ( watch ( twindFile , ( ) => this . _reset ( ) , { once : true } ) )
335
+
336
+ const packageJSON = this . info . project . readFile (
337
+ path . join ( path . dirname ( twindFile ) , 'package.json' ) ,
338
+ )
339
+
340
+ if ( packageJSON ) {
341
+ try {
342
+ version = ( JSON . parse ( packageJSON ) || { } ) . version
343
+ } catch {
344
+ // ignore
345
+ }
346
+ }
326
347
}
327
348
328
- this . logger . log ( 'twindFile: ' + twindFile )
349
+ if ( twindFile ) {
350
+ this . logger . log ( `loading twind${ version ? '@' + version : '' } from ${ twindFile } ` )
351
+ } else {
352
+ this . logger . log ( `using builtin twind` )
353
+ }
329
354
355
+ // Prefer local twind
330
356
const { tw } = (
331
357
( twindFile &&
332
358
( loadFile ( twindFile , program . getCurrentDirectory ( ) ) as typeof import ( 'twind' ) ) ?. create ) ||
0 commit comments