Skip to content

Commit 6354205

Browse files
committed
fix: prefer twind from project directory
1 parent 727d3e1 commit 6354205

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/twind.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { Logger } from 'typescript-template-language-service-decorator'
44
import type * as TS from 'typescript/lib/tsserverlibrary'
55

66
import resolveFrom from 'resolve-from'
7+
import importFrom from 'import-from'
78
import cssbeautify from 'cssbeautify'
89

910
import type {
@@ -257,7 +258,12 @@ export class Twind {
257258
reports.length = 0
258259
})
259260

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+
)({
261267
...config,
262268
sheet,
263269
mode: {
@@ -323,10 +329,18 @@ export class Twind {
323329

324330
let tokens: string[] = []
325331

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

331345
if (twindDTSSourceFile) {
332346
const { typescript: ts } = this

0 commit comments

Comments
 (0)