Skip to content

Commit c75dcd8

Browse files
committed
feat(tsgo): support source map
closes #158
1 parent 1827d1b commit c75dcd8

File tree

8 files changed

+128
-62
lines changed

8 files changed

+128
-62
lines changed

src/generate.ts

Lines changed: 16 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { fork, spawn, type ChildProcess } from 'node:child_process'
1+
import { fork, type ChildProcess } from 'node:child_process'
22
import { 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'
54
import path from 'node:path'
65
import { parse } from '@babel/parser'
76
import { createDebug } from 'obug'
@@ -24,6 +23,7 @@ import {
2423
invalidateContextFile,
2524
type TscContext,
2625
} from './tsc/context.ts'
26+
import { runTsgo } from './tsgo.ts'
2727
import type { OptionsResolved } from './options.ts'
2828
import type { TscOptions, TscResult } from './tsc/index.ts'
2929
import type { TscFunctions } from './tsc/worker.ts'
@@ -35,13 +35,6 @@ const debug = createDebug('rolldown-plugin-dts:generate')
3535

3636
const 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-
4538
export 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-
395367
function collectJsonExportMap(code: string): Map<string, string> {
396368
const exportMap = new Map<string, string>()
397369
const { program } = parse(code, {

src/tsgo.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { spawn } from 'node:child_process'
2+
import { mkdtemp } from 'node:fs/promises'
3+
import { tmpdir } from 'node:os'
4+
import path from 'node:path'
5+
import { createDebug } from 'obug'
6+
7+
const debug = createDebug('rolldown-plugin-dts:tsgo')
8+
9+
const spawnAsync = (...args: Parameters<typeof spawn>) =>
10+
new Promise<void>((resolve, reject) => {
11+
const child = spawn(...args)
12+
child.on('close', () => resolve())
13+
child.on('error', (error) => reject(error))
14+
})
15+
16+
export async function runTsgo(
17+
rootDir: string,
18+
tsconfig?: string,
19+
sourcemap?: boolean,
20+
) {
21+
debug('[tsgo] rootDir', rootDir)
22+
23+
const tsgoPkg = import.meta.resolve('@typescript/native-preview/package.json')
24+
const { default: getExePath } = await import(
25+
new URL('lib/getExePath.js', tsgoPkg).href
26+
)
27+
const tsgo = getExePath()
28+
const tsgoDist = await mkdtemp(path.join(tmpdir(), 'rolldown-plugin-dts-'))
29+
debug('[tsgo] tsgoDist', tsgoDist)
30+
31+
const args = [
32+
'--noEmit',
33+
'false',
34+
'--declaration',
35+
'--emitDeclarationOnly',
36+
...(tsconfig ? ['-p', tsconfig] : []),
37+
'--outDir',
38+
tsgoDist,
39+
'--rootDir',
40+
rootDir,
41+
'--noCheck',
42+
...(sourcemap ? ['--declarationMap'] : []),
43+
]
44+
debug('[tsgo] args %o', args)
45+
46+
await spawnAsync(tsgo, args, { stdio: 'inherit' })
47+
return tsgoDist
48+
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
## source-map.d.ts
1+
## index.d.ts
22

33
```ts
4-
//#region tests/fixtures/source-map.d.ts
4+
//#region tests/fixtures/source-map/index.d.ts
55
declare const a: string;
66
declare const b: string;
77
type Str = string;
88
declare function fn(param: Str): string;
99
//#endregion
1010
export { a, b, fn };
11-
//# sourceMappingURL=source-map.d.ts.map
11+
//# sourceMappingURL=index.d.ts.map
1212
```
1313

14-
## source-map.d.ts.map
14+
## index.d.ts.map
1515

1616
```map
17-
{"version":3,"file":"source-map.d.ts","names":[],"sources":["../../fixtures/source-map.ts"],"mappings":";cAAa,CAAA;AAAA,cAEA,CAAA;AAAA,KAIR,GAAA;AAAA,iBACW,EAAA,CAAA,KAAA,EAAU,GAAA"}
17+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../fixtures/source-map/index.ts"],"mappings":";cAAa,CAAA;AAAA,cAEA,CAAA;AAAA,KAIR,GAAA;AAAA,iBACW,EAAA,CAAA,KAAA,EAAU,GAAA"}
1818
```
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
## source-map.d.ts
1+
## index.d.ts
22

33
```ts
4-
//#region tests/fixtures/source-map.d.ts
4+
//#region tests/fixtures/source-map/index.d.ts
55
declare const a: string;
66
declare const b: string;
77
type Str = string;
88
declare function fn(param: Str): string;
99
//#endregion
1010
export { a, b, fn };
11-
//# sourceMappingURL=source-map.d.ts.map
11+
//# sourceMappingURL=index.d.ts.map
1212
```
1313

14-
## source-map.d.ts.map
14+
## index.d.ts.map
1515

1616
```map
17-
{"version":3,"file":"source-map.d.ts","names":[],"sources":["../../fixtures/source-map.ts"],"mappings":";cAAa,CAAA;AAAA,cAEA,CAAA;AAAA,KAIR,GAAA;AAAA,iBACW,EAAA,CAAA,KAAA,EAAU,GAAA"}
17+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../fixtures/source-map/index.ts"],"mappings":";cAAa,CAAA;AAAA,cAEA,CAAA;AAAA,KAIR,GAAA;AAAA,iBACW,EAAA,CAAA,KAAA,EAAU,GAAA"}
1818
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## index.d.ts
2+
3+
```ts
4+
//#region tests/fixtures/source-map/index.d.ts
5+
declare const a: string;
6+
declare const b: string;
7+
type Str = string;
8+
declare function fn(param: Str): string;
9+
//#endregion
10+
export { a, b, fn };
11+
//# sourceMappingURL=index.d.ts.map
12+
```
13+
14+
## index.d.ts.map
15+
16+
```map
17+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../fixtures/source-map/index.ts"],"mappings":";cAAa,CAAA;AAAA,cAEA,CAAA;AAAA,KAIR,GAAA;AAAA,iBACW,EAAA,CAAA,KAAA,EAAU,GAAA"}
18+
```
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"compilerOptions": {
3+
"declaration": true,
4+
"emitDeclarationOnly": true,
5+
"declarationMap": true
6+
}
7+
}

tests/source-map.test.ts

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
11
import { rm } from 'node:fs/promises'
22
import path from 'node:path'
3-
import { fileURLToPath } from 'node:url'
43
import { expectFilesSnapshot, rolldownBuild } from '@sxzz/test-utils'
54
import { build } from 'rolldown'
65
import { beforeAll, expect, test } from 'vitest'
76
import { dts } from '../src/index.ts'
87

9-
const dirname = path.dirname(fileURLToPath(import.meta.url))
10-
const tempDir = path.join(dirname, 'temp')
8+
const tempDir = path.join(import.meta.dirname, 'temp')
9+
const input = path.resolve(import.meta.dirname, 'fixtures/source-map/index.ts')
10+
const tsconfig = path.resolve(
11+
import.meta.dirname,
12+
'fixtures/source-map/tsconfig.json',
13+
)
1114

1215
beforeAll(async () => {
1316
await rm(tempDir, { recursive: true, force: true })
1417
})
1518

16-
const input = path.resolve(dirname, 'fixtures/source-map.ts')
17-
1819
test('oxc', async () => {
1920
const dir = path.join(tempDir, 'source-map-oxc')
2021
await build({
2122
input,
2223
plugins: [
2324
dts({
2425
oxc: true,
26+
tsconfig,
2527
sourcemap: true,
2628
emitDtsOnly: true,
2729
}),
@@ -39,6 +41,7 @@ test('tsc', async () => {
3941
plugins: [
4042
dts({
4143
oxc: false,
44+
tsconfig,
4245
sourcemap: true,
4346
emitDtsOnly: true,
4447
}),
@@ -49,6 +52,24 @@ test('tsc', async () => {
4952
await expectFilesSnapshot(dir, '__snapshots__/source-map-tsc.md')
5053
})
5154

55+
test('tsgo', async () => {
56+
const dir = path.join(tempDir, 'source-map-tsgo')
57+
await build({
58+
input,
59+
plugins: [
60+
dts({
61+
tsgo: true,
62+
tsconfig,
63+
sourcemap: true,
64+
emitDtsOnly: true,
65+
}),
66+
],
67+
output: { dir },
68+
write: true,
69+
})
70+
await expectFilesSnapshot(dir, '__snapshots__/source-map-tsgo.md')
71+
})
72+
5273
test('disable dts source map only', async () => {
5374
const { chunks } = await rolldownBuild(
5475
input,
@@ -58,9 +79,9 @@ test('disable dts source map only', async () => {
5879
)
5980
expect(chunks.map((chunk) => chunk.fileName)).toMatchInlineSnapshot(`
6081
[
61-
"source-map.d.ts",
62-
"source-map.js",
63-
"source-map.js.map",
82+
"index.d.ts",
83+
"index.js",
84+
"index.js.map",
6485
]
6586
`)
6687
})

0 commit comments

Comments
 (0)