|
1 | 1 | import * as path from 'path'
|
| 2 | +import Module from 'module' |
| 3 | +import { fileURLToPath, pathToFileURL } from 'url' |
2 | 4 |
|
3 | 5 | import type { Logger } from 'typescript-template-language-service-decorator'
|
4 | 6 | import type * as TS from 'typescript/lib/tsserverlibrary'
|
@@ -235,7 +237,7 @@ export class Twind {
|
235 | 237 | return this._state
|
236 | 238 | }
|
237 | 239 |
|
238 |
| - const program = this.info.languageService.getProgram() |
| 240 | + let program = this.info.languageService.getProgram() |
239 | 241 |
|
240 | 242 | if (!program) {
|
241 | 243 | return undefined
|
@@ -263,17 +265,55 @@ export class Twind {
|
263 | 265 | })
|
264 | 266 |
|
265 | 267 | // Prefer project twind and fallback to bundled twind
|
266 |
| - const twindDTSFile = this.info.project |
| 268 | + let twindDTSFile = this.info.project |
267 | 269 | .resolveModuleNames(['twind'], program.getRootFileNames()[0])
|
268 | 270 | .map((moduleName) => moduleName?.resolvedFileName)[0]
|
269 | 271 |
|
270 |
| - const twindDTSSourceFile = |
| 272 | + this.logger.log('local twind dts: ' + twindDTSFile) |
| 273 | + |
| 274 | + let twindDTSSourceFile = |
271 | 275 | (twindDTSFile &&
|
272 | 276 | program.getSourceFiles().find((sourceFile) => sourceFile.fileName == twindDTSFile)) ||
|
273 | 277 | program
|
274 | 278 | .getSourceFiles()
|
275 | 279 | .find((sourceFile) => sourceFile.fileName.endsWith('twind/twind.d.ts'))
|
276 | 280 |
|
| 281 | + // No local twind but a twind.config -> use our twind |
| 282 | + if ( |
| 283 | + !twindDTSFile && |
| 284 | + !twindDTSSourceFile && |
| 285 | + configFile && |
| 286 | + /twind\.config\.\w+$/.test(configFile) |
| 287 | + ) { |
| 288 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 289 | + // @ts-ignore |
| 290 | + const from: string | URL = import.meta.url || pathToFileURL(__filename) |
| 291 | + |
| 292 | + const { resolve } = |
| 293 | + Module.createRequire?.(from) || Module.createRequireFromPath(fileURLToPath(from)) |
| 294 | + |
| 295 | + try { |
| 296 | + twindDTSFile = resolve('twind').replace(/\.\w+$/, '.d.ts') |
| 297 | + this.logger.log('builtin twind dts: ' + twindDTSFile) |
| 298 | + } catch { |
| 299 | + // ignore |
| 300 | + } |
| 301 | + } |
| 302 | + |
| 303 | + if (twindDTSFile) { |
| 304 | + program = this.typescript.createProgram({ |
| 305 | + rootNames: [...program.getRootFileNames(), twindDTSFile], |
| 306 | + options: program.getCompilerOptions(), |
| 307 | + // projectReferences?: readonly ProjectReference[]; |
| 308 | + // host?: program.getCom |
| 309 | + oldProgram: program, |
| 310 | + }) |
| 311 | + |
| 312 | + twindDTSSourceFile = program |
| 313 | + .getSourceFiles() |
| 314 | + .find((sourceFile) => sourceFile.fileName.endsWith('twind/twind.d.ts')) |
| 315 | + } |
| 316 | + |
277 | 317 | this.logger.log('twindDTSSourceFile: ' + twindDTSSourceFile?.fileName)
|
278 | 318 |
|
279 | 319 | if (twindDTSSourceFile) {
|
|
0 commit comments