@@ -4,6 +4,7 @@ import type { Logger } from 'typescript-template-language-service-decorator'
4
4
import type * as TS from 'typescript/lib/tsserverlibrary'
5
5
6
6
import resolveFrom from 'resolve-from'
7
+ import importFrom from 'import-from'
7
8
import cssbeautify from 'cssbeautify'
8
9
9
10
import type {
@@ -257,7 +258,12 @@ export class Twind {
257
258
reports . length = 0
258
259
} )
259
260
260
- const { tw } = create ( {
261
+ // Load twind from project
262
+ // TODO Use esbuild and watch twindPackageFile
263
+ const { tw } = (
264
+ ( importFrom . silent ( program . getCurrentDirectory ( ) , 'twind' ) as typeof import ( 'twind' ) )
265
+ ?. create || create
266
+ ) ( {
261
267
...config ,
262
268
sheet,
263
269
mode : {
@@ -323,10 +329,18 @@ export class Twind {
323
329
324
330
let tokens : string [ ] = [ ]
325
331
326
- // TODO use resolveFrom.silent and warn
327
- const twindPackageFile = resolveFrom ( program . getCurrentDirectory ( ) , 'twind/package.json' )
328
- const twindDTSFileName = path . resolve ( path . dirname ( twindPackageFile ) , 'twind.d.ts' )
329
- const twindDTSSourceFile = program . getSourceFile ( twindDTSFileName )
332
+ // Prefer project twind and fallback to bundled twind
333
+ const twindPackageFile = resolveFrom . silent ( program . getCurrentDirectory ( ) , 'twind/package.json' )
334
+ this . logger . log ( 'twindPackageFile: ' + twindPackageFile )
335
+
336
+ const twindDTSSourceFile =
337
+ ( twindPackageFile &&
338
+ program . getSourceFile ( path . resolve ( path . dirname ( twindPackageFile ) , 'twind.d.ts' ) ) ) ||
339
+ program
340
+ . getSourceFiles ( )
341
+ . find ( ( sourceFile ) => sourceFile . fileName . endsWith ( 'twind/twind.d.ts' ) )
342
+
343
+ this . logger . log ( 'twindPackageFile: ' + twindDTSSourceFile ?. fileName )
330
344
331
345
if ( twindDTSSourceFile ) {
332
346
const { typescript : ts } = this
0 commit comments