1- import { fork , spawn , type ChildProcess } from 'node:child_process'
1+ import { fork , type ChildProcess } from 'node:child_process'
22import { existsSync } from 'node:fs'
3- import { mkdtemp , readFile , rm } from 'node:fs/promises'
4- import { tmpdir } from 'node:os'
3+ import { readFile , rm } from 'node:fs/promises'
54import path from 'node:path'
65import { parse } from '@babel/parser'
76import { createDebug } from 'obug'
@@ -24,6 +23,7 @@ import {
2423 invalidateContextFile ,
2524 type TscContext ,
2625} from './tsc/context.ts'
26+ import { runTsgo } from './tsgo.ts'
2727import type { OptionsResolved } from './options.ts'
2828import type { TscOptions , TscResult } from './tsc/index.ts'
2929import type { TscFunctions } from './tsc/worker.ts'
@@ -35,13 +35,6 @@ const debug = createDebug('rolldown-plugin-dts:generate')
3535
3636const WORKER_URL = import . meta. WORKER_URL || './tsc/worker.ts'
3737
38- const spawnAsync = ( ...args : Parameters < typeof spawn > ) =>
39- new Promise < void > ( ( resolve , reject ) => {
40- const child = spawn ( ...args )
41- child . on ( 'close' , ( ) => resolve ( ) )
42- child . on ( 'error' , ( error ) => reject ( error ) )
43- } )
44-
4538export interface TsModule {
4639 /** `.ts` source code */
4740 code : string
@@ -114,7 +107,7 @@ export function createGeneratePlugin({
114107 // isWatch = this.meta.watchMode
115108
116109 if ( tsgo ) {
117- tsgoDist = await runTsgo ( rootDir , tsconfig )
110+ tsgoDist = await runTsgo ( rootDir , tsconfig , sourcemap )
118111 } else if ( ! oxc ) {
119112 // tsc
120113 if ( parallel ) {
@@ -238,14 +231,23 @@ export function createGeneratePlugin({
238231 tsgoDist ! ,
239232 path . relative ( path . resolve ( rootDir ) , filename_to_dts ( id ) ) ,
240233 )
241- if ( existsSync ( dtsPath ) ) {
242- dtsCode = await readFile ( dtsPath , 'utf8' )
243- } else {
234+ if ( ! existsSync ( dtsPath ) ) {
244235 debug ( '[tsgo]' , dtsPath , 'is missing' )
245236 throw new Error (
246237 `tsgo did not generate dts file for ${ id } , please check your tsconfig.` ,
247238 )
248239 }
240+
241+ dtsCode = await readFile ( dtsPath , 'utf8' )
242+
243+ const mapPath = `${ dtsPath } .map`
244+ if ( existsSync ( mapPath ) ) {
245+ const mapRaw = await readFile ( mapPath , 'utf8' )
246+ map = {
247+ ...JSON . parse ( mapRaw ) ,
248+ sources : [ id ] ,
249+ }
250+ }
249251 } else if ( oxc && ! RE_VUE . test ( id ) ) {
250252 const result = isolatedDeclarationSync ( id , code , oxc )
251253 if ( result . errors . length ) {
@@ -362,36 +364,6 @@ export { __json_default_export as default }`
362364 }
363365}
364366
365- async function runTsgo ( rootDir : string , tsconfig ?: string ) {
366- const tsgoPkg = import . meta. resolve ( '@typescript/native-preview/package.json' )
367- const { default : getExePath } = await import (
368- new URL ( 'lib/getExePath.js' , tsgoPkg ) . href
369- )
370- const tsgo = getExePath ( )
371- const tsgoDist = await mkdtemp ( path . join ( tmpdir ( ) , 'rolldown-plugin-dts-' ) )
372- debug ( '[tsgo] tsgoDist' , tsgoDist )
373- debug ( '[tsgo] rootDir' , rootDir )
374-
375- await spawnAsync (
376- tsgo ,
377- [
378- '--noEmit' ,
379- 'false' ,
380- '--declaration' ,
381- '--emitDeclarationOnly' ,
382- ...( tsconfig ? [ '-p' , tsconfig ] : [ ] ) ,
383- '--outDir' ,
384- tsgoDist ,
385- '--rootDir' ,
386- rootDir ,
387- '--noCheck' ,
388- ] ,
389- { stdio : 'inherit' } ,
390- )
391-
392- return tsgoDist
393- }
394-
395367function collectJsonExportMap ( code : string ) : Map < string , string > {
396368 const exportMap = new Map < string , string > ( )
397369 const { program } = parse ( code , {
0 commit comments