Skip to content

Commit 3a463d9

Browse files
committed
fix: optimize loading of twind dts (prefer local, fallback bundled)
1 parent 6734a77 commit 3a463d9

File tree

1 file changed

+42
-16
lines changed

1 file changed

+42
-16
lines changed

src/twind.ts

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,12 @@ export class Twind {
250250
)
251251

252252
if (configFile) {
253-
this.logger.log(`Loaded twind config from ${configFile}`)
253+
this.logger.log(`loaded twind config from ${configFile}`)
254254

255255
// Reset all state on config file changes
256256
this._watchers.push(watch(configFile, () => this._reset(), { once: true }))
257257
} else {
258-
this.logger.log(`No twind config found`)
258+
this.logger.log(`no local twind config found`)
259259
}
260260

261261
const sheet = virtualSheet()
@@ -269,7 +269,9 @@ export class Twind {
269269
.resolveModuleNames(['twind'], program.getRootFileNames()[0])
270270
.map((moduleName) => moduleName?.resolvedFileName)[0]
271271

272-
this.logger.log('local twind dts: ' + twindDTSFile)
272+
if (twindDTSFile) {
273+
this.logger.log(`found local twind dts at ${twindDTSFile}`)
274+
}
273275

274276
let twindDTSSourceFile =
275277
(twindDTSFile &&
@@ -280,32 +282,36 @@ export class Twind {
280282

281283
// No local twind but a twind.config -> use our twind
282284
if (
283-
!twindDTSFile &&
284285
!twindDTSSourceFile &&
286+
!twindDTSFile &&
285287
configFile &&
286288
/twind\.config\.\w+$/.test(configFile)
287289
) {
288290
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
289291
// @ts-ignore
290-
const from: string | URL = import.meta.url || pathToFileURL(__filename)
292+
const from = fileURLToPath(import.meta.url)
291293

292-
const { resolve } =
293-
Module.createRequire?.(from) || Module.createRequireFromPath(fileURLToPath(from))
294+
const { resolve } = Module.createRequire?.(from) || Module.createRequireFromPath(from)
294295

295296
try {
296297
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+
}
298301
} catch {
299302
// ignore
300303
}
301304
}
302305

303-
if (twindDTSFile) {
306+
if (!twindDTSSourceFile && twindDTSFile) {
307+
const options = program.getCompilerOptions()
308+
304309
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\.ts$/, '.js')],
311+
options: {
312+
...options,
313+
typeRoots: [...(options.typeRoots || []), path.dirname(twindDTSFile)],
314+
},
309315
oldProgram: program,
310316
})
311317

@@ -314,19 +320,39 @@ export class Twind {
314320
.find((sourceFile) => sourceFile.fileName.endsWith('twind/twind.d.ts'))
315321
}
316322

317-
this.logger.log('twindDTSSourceFile: ' + twindDTSSourceFile?.fileName)
318-
319323
if (twindDTSSourceFile) {
324+
this.logger.log(`using twind completions from ${twindDTSSourceFile.fileName}`)
320325
this._watchers.push(watch(twindDTSSourceFile.fileName, () => this._reset(), { once: true }))
326+
} else {
327+
this.logger.log(`no twind completions found`)
321328
}
322329

323330
const twindFile = twindDTSSourceFile?.fileName.replace(/\.d\.ts/, '.js')
331+
let version: string | undefined = 'undefined'
332+
324333
if (twindFile) {
325334
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+
}
326347
}
327348

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+
}
329354

355+
// Prefer local twind
330356
const { tw } = (
331357
(twindFile &&
332358
(loadFile(twindFile, program.getCurrentDirectory()) as typeof import('twind'))?.create) ||

0 commit comments

Comments
 (0)