Skip to content

Commit 21685c8

Browse files
ocavuematthewnitschke-wknexiumbiz-debug
authored
fix: generate declaration map from referenced tsconfig projects (#291)
Co-authored-by: Matthew Nitschke <matthew.nitschke@workiva.com> Co-authored-by: nexiumbiz-debug <265948173+nexiumbiz-debug@users.noreply.github.com>
1 parent b58c7ea commit 21685c8

7 files changed

Lines changed: 71 additions & 17 deletions

File tree

‎src/tsc/emit-build.ts‎

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,11 @@ function buildProjects(
174174

175175
const host = ts.createSolutionBuilderHost(
176176
fsSystem,
177-
createProgramWithPatchedCompilerOptions,
177+
createProgramWithPatchedCompilerOptions({
178+
force,
179+
sourcemap,
180+
tsconfigPath: tsconfig,
181+
}),
178182
)
179183
const builder = ts.createSolutionBuilder(host, [tsconfig], {
180184
force,
@@ -229,7 +233,11 @@ function collectProjectGraph(
229233
parsedConfig.options = patchCompilerOptions(parsedConfig.options, {
230234
tsconfigPath,
231235
force,
232-
sourcemap,
236+
// For a project that has no source files (e.g., a tsconfig.json file
237+
// that only contains references), we don't need to generate a sourcemap
238+
// and we don't need to print the warning about the missing
239+
// `declaration`, `declarationMap` in the tsconfig.json file.
240+
sourcemap: sourcemap && parsedConfig.fileNames.length > 0,
233241
})
234242

235243
projects.push({ tsconfigPath, parsedConfig })
@@ -267,18 +275,20 @@ function parseTsconfig(
267275
return parsedConfig
268276
}
269277

278+
interface CompilerPatchOptions {
279+
tsconfigPath: string
280+
force: boolean
281+
sourcemap: boolean
282+
}
283+
270284
// To ensure we can get `.d.ts` and `.d.ts.map` files from `tsc --build` mode,
271285
// we need to enforce certain compiler options. Notice that changing compiler
272286
// options will invalidate the cache, so it's better to set the correct value in
273287
// tsconfig files in the first place. Therefore, we print some warnings if the
274288
// values are not set correctly.
275289
function patchCompilerOptions(
276290
options: CompilerOptions,
277-
extraOptions: {
278-
tsconfigPath: string
279-
force: boolean
280-
sourcemap: boolean
281-
} | null,
291+
extraOptions: CompilerPatchOptions,
282292
): CompilerOptions {
283293
const noEmit: boolean = options.noEmit ?? false
284294
const declaration: boolean =
@@ -315,12 +325,14 @@ function patchCompilerOptions(
315325
return options
316326
}
317327

318-
const createProgramWithPatchedCompilerOptions: CreateProgram<
319-
EmitAndSemanticDiagnosticsBuilderProgram
320-
> = (rootNames, options, ...args) => {
321-
return ts.createEmitAndSemanticDiagnosticsBuilderProgram(
322-
rootNames,
323-
patchCompilerOptions(options ?? {}, null),
324-
...args,
325-
)
328+
function createProgramWithPatchedCompilerOptions(
329+
patchOptions: CompilerPatchOptions,
330+
): CreateProgram<EmitAndSemanticDiagnosticsBuilderProgram> {
331+
return (rootNames, options, ...args) => {
332+
return ts.createEmitAndSemanticDiagnosticsBuilderProgram(
333+
rootNames,
334+
patchCompilerOptions(options ?? {}, patchOptions),
335+
...args,
336+
)
337+
}
326338
}

‎tests/__snapshots__/tsc.test.ts.snap‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export declare const a = 1;
4444
//#endregion
4545
//# sourceMappingURL=index.d.ts.map
4646
// index.d.ts.map
47-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.d.ts"],"mappings":";qBAAqB"}
47+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.ts"],"mappings":";qBAAa"}
4848
// index.js
4949
//#region tests/fixtures/deep-source-map/src/index.ts
5050
const a = 1;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { add } from './math.js'
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function add(x: number, y: number): number {
2+
return x + y
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"files": [],
3+
"references": [{ "path": "./tsconfig.src.json" }]
4+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ESNext",
4+
"module": "ESNext",
5+
"moduleResolution": "bundler",
6+
"declaration": true,
7+
"strict": true,
8+
"outDir": "dist",
9+
"composite": true
10+
},
11+
"include": ["src/**/*"]
12+
}

‎tests/tsc.test.ts‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ describe('tsc', () => {
110110
expect(sourcemap.sourceRoot).toBeFalsy()
111111
expect(sourcemap.sources).toMatchInlineSnapshot(`
112112
[
113-
"../src/index.d.ts",
113+
"../src/index.ts",
114114
]
115115
`)
116116
expect(snapshot).toMatchSnapshot()
@@ -140,6 +140,28 @@ describe('tsc', () => {
140140
expect(sourcemap.sourcesContent).toBeOneOf([undefined, []])
141141
})
142142

143+
test('build sourcemap sources point to .ts not .d.ts #255', async () => {
144+
const root = path.resolve(dirname, 'fixtures/build-sourcemap-refs')
145+
146+
const { chunks } = await rolldownBuild(
147+
[path.resolve(root, 'src/index.ts')],
148+
[
149+
dts({
150+
tsconfig: path.resolve(root, 'tsconfig.json'),
151+
build: true,
152+
sourcemap: true,
153+
emitDtsOnly: true,
154+
}),
155+
],
156+
{},
157+
{ dir: path.resolve(root, 'dist') },
158+
)
159+
160+
const sourcemap = findSourceMapChunk(chunks, 'index.d.ts.map')
161+
const sources = (sourcemap.sources || []).toSorted()
162+
expect(sources).toEqual(['../src/math.ts'])
163+
})
164+
143165
test('composite references no incremental', async () => {
144166
const root = path.resolve(dirname, 'fixtures/composite-refs-no-incremental')
145167

0 commit comments

Comments
 (0)