Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/sxzz/rolldown-plugin-dts. A Rolldown plugin to generate and bundle dts files.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187import { spawn } from 'node:child_process'import { existsSync } from 'node:fs'import { mkdtemp, readFile, rm } from 'node:fs/promises'import { tmpdir } from 'node:os'import path from 'node:path'import { styleText } from 'node:util'import { createDebug } from 'obug'import { filename_to_dts } from '../filename.ts'import { require, tryResolve } from '../utils/require.ts'import type { LanguageContext } from '../custom-language.ts'import type { Logger } from '../options.ts'import type { Generator, GeneratorResult } from './index.ts'import type { SourceMapInput } from 'rolldown'
// Deprecated backend for TypeScript 7.0, which only ships the `tsgo` CLI.// TypeScript 7.1+ uses the API generator. To be removed in the next minor.const debug = createDebug('rolldown-plugin-dts:tsgo')
// Output is piped rather than inherited so diagnostics reach the user through// the thrown error, i.e. through Rolldown, instead of being written straight// to the terminal.const spawnAsync = (...args: Parameters<typeof spawn>) => new Promise<void>((resolve, reject) => { const child = spawn(...args) let output = '' child.stdout?.on('data', (chunk) => (output += chunk)) child.stderr?.on('data', (chunk) => (output += chunk)) child.on('close', (code, signal) => { if (code === 0) return resolve() const message = signal ? `tsgo process terminated by signal ${signal}` : `tsgo process exited with code ${code}` output = output.trim() reject(new Error(output ? `${message}\n${output}` : message)) }) child.on('error', (error) => reject(error)) })
let tsgoPathCache: string | undefined
export function resolveTsgoPath(logger: Logger): string { if (tsgoPathCache) return tsgoPathCache
const pkgJsonPath = tryResolve('typescript/package.json') if (!pkgJsonPath) { throw new Error( 'TypeScript Go is not installed. Please install the `typescript@7.0` package to use the `tsgo` generator.', ) }
const { version } = require(pkgJsonPath) as { version: string } logger.info( `Emit types with ${styleText('underline', `typescript@${version}`)}`, )
const getExePath: any = require( path.join(path.dirname(pkgJsonPath), 'lib/getExePath.js'), ) const tsgoPath = ( typeof getExePath === 'function' ? getExePath : getExePath.default )() if (import.meta.TEST) { return tsgoPath } return (tsgoPathCache = tsgoPath)}
interface TsgoContext { path: string dispose: () => Promise<void>}
export interface TsgoCliGeneratorOptions { tsgoPath: string tsconfig: string sourcemap?: boolean typecheck?: boolean languageContext: LanguageContext}
export class TsgoCliGenerator implements Generator { private options: TsgoCliGeneratorOptions private context?: TsgoContext private rootDir: string
constructor(options: TsgoCliGeneratorOptions) { this.options = options this.rootDir = path.parse(path.resolve(options.tsconfig)).root }
async init(): Promise<void> { const { tsgoPath, tsconfig, sourcemap, typecheck } = this.options this.context = await runTsgo( tsgoPath, this.rootDir, tsconfig, sourcemap, typecheck, ) }
async emit(_code: string, fileName: string): Promise<GeneratorResult> { const { languageContext } = this.options if (languageContext.isCustomLanguageFile(fileName)) { return { error: `tsgo does not support ${path.extname(fileName)} files.`, } } if (!this.context) { throw new Error('TsgoCliGenerator has not been initialized.') }
const dtsPath = path.resolve( this.context.path, path.relative(this.rootDir, filename_to_dts(fileName, languageContext)), ) if (!existsSync(dtsPath)) { debug('[tsgo]', dtsPath, 'is missing') return { error: `tsgo did not generate dts file for ${fileName}, please check your tsconfig.`, } }
const code = await readFile(dtsPath, 'utf8') let map: SourceMapInput | undefined const mapPath = `${dtsPath}.map` if (existsSync(mapPath)) { const mapRaw = await readFile(mapPath, 'utf8') map = { ...JSON.parse(mapRaw), sources: [fileName], } }
return { code, map } }
async dispose(): Promise<void> { await this.context?.dispose() this.context = undefined }}
async function runTsgo( tsgoPath: string, rootDir: string, tsconfig: string, sourcemap?: boolean, typecheck?: boolean,): Promise<TsgoContext> { debug('[tsgo] rootDir', rootDir) debug('[tsgo] using tsgo binary', tsgoPath)
const tsgoDist = await mkdtemp(path.join(tmpdir(), 'rolldown-plugin-dts-')) debug('[tsgo] tsgoDist', tsgoDist)
const args = [ '--noEmit', 'false', '--declaration', '--emitDeclarationOnly', '-p', tsconfig, '--outDir', tsgoDist, '--rootDir', rootDir, ...(typecheck ? [] : ['--noCheck']), ...(sourcemap ? ['--declarationMap'] : []), ] debug('[tsgo] args %o', args)
await spawnAsync(tsgoPath, args, { stdio: ['ignore', 'pipe', 'pipe'] })
return { path: tsgoDist, async dispose() { if (debug.enabled) { debug('[tsgo] skip cleanup of tsgoDist', tsgoDist) } else { debug('[tsgo] disposing tsgoDist', tsgoDist) await rm(tsgoDist, { recursive: true, force: true }).catch(() => {}) } }, }}