Skip to content

Commit ee58d34

Browse files
committed
Add declaration paths to remap cache in packages mode
Resolves #2416
1 parent 7e0b959 commit ee58d34

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### Features
44

55
- Added `--sourceLinkExternal` option to render source code links as external, #2415.
6+
- TypeDoc no longer requires the `declarationMap` option to be set to true to handle cross-package links in packages mode, #2416.
67
- Added `external-last` option for the `--sort` option, #2418.
78

89
### Bug Fixes

Diff for: src/lib/application.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import { ApplicationEvents } from "./application-events";
4141
import { findTsConfigFile } from "./utils/tsconfig";
4242
import { deriveRootDir, glob, readFile } from "./utils/fs";
4343
import { resetReflectionID } from "./models/reflections/abstract";
44+
import { addInferredDeclarationMapPaths } from "./models/reflections/ReflectionSymbolId";
4445

4546
// eslint-disable-next-line @typescript-eslint/no-var-requires
4647
const packageInfo = require("../../package.json") as {
@@ -590,9 +591,10 @@ export class Application extends ChildableComponent<
590591
const origOptions = this.options;
591592
const projects: JSONOutput.ProjectReflection[] = [];
592593

594+
const projectsToConvert: { dir: string; options: Options }[] = [];
593595
// Generate a json file for each package
594596
for (const dir of packageDirs) {
595-
this.logger.info(`Converting project at ${nicePath(dir)}`);
597+
this.logger.verbose(`Reading project at ${nicePath(dir)}`);
596598
const opts = origOptions.copyForPackage(dir);
597599
await opts.read(this.logger, dir);
598600
// Invalid links should only be reported after everything has been merged.
@@ -609,7 +611,17 @@ export class Application extends ChildableComponent<
609611
continue;
610612
}
611613

612-
this.options = opts;
614+
addInferredDeclarationMapPaths(
615+
opts.getCompilerOptions(),
616+
opts.getFileNames(),
617+
);
618+
619+
projectsToConvert.push({ dir, options: opts });
620+
}
621+
622+
for (const { dir, options } of projectsToConvert) {
623+
this.logger.info(`Converting project at ${nicePath(dir)}`);
624+
this.options = options;
613625
const project = await this.convert();
614626
if (project) {
615627
this.validate(project);

Diff for: src/lib/models/reflections/ReflectionSymbolId.ts

+28-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { existsSync } from "fs";
22
import { isAbsolute, join, relative, resolve } from "path";
33
import ts from "typescript";
44
import type { JSONOutput, Serializer } from "../../serialization/index";
5-
import { readFile } from "../../utils/fs";
5+
import { getCommonDirectory, readFile } from "../../utils/fs";
66
import { getQualifiedName } from "../../utils/tsutils";
77
import { optional, validate } from "../../utils/validation";
88
import { normalizePath } from "../../utils/paths";
@@ -61,27 +61,26 @@ export class ReflectionSymbolId {
6161
}
6262

6363
toObject(serializer: Serializer) {
64+
const sourceFileName = isAbsolute(this.fileName)
65+
? normalizePath(
66+
relative(
67+
serializer.projectRoot,
68+
resolveDeclarationMaps(this.fileName),
69+
),
70+
)
71+
: this.fileName;
72+
6473
return {
65-
sourceFileName: isAbsolute(this.fileName)
66-
? normalizePath(
67-
relative(
68-
serializer.projectRoot,
69-
resolveDeclarationMaps(this.fileName),
70-
),
71-
)
72-
: this.fileName,
74+
sourceFileName,
7375
qualifiedName: this.qualifiedName,
7476
};
7577
}
7678
}
7779

7880
const declarationMapCache = new Map<string, string>();
7981

80-
/**
81-
* See also getTsSourceFromJsSource in package-manifest.ts.
82-
*/
8382
function resolveDeclarationMaps(file: string): string {
84-
if (!file.endsWith(".d.ts")) return file;
83+
if (!/\.d\.[cm]?ts$/.test(file)) return file;
8584
if (declarationMapCache.has(file)) return declarationMapCache.get(file)!;
8685

8786
const mapFile = file + ".map";
@@ -123,3 +122,19 @@ function resolveDeclarationMaps(file: string): string {
123122

124123
return file;
125124
}
125+
126+
export function addInferredDeclarationMapPaths(
127+
opts: ts.CompilerOptions,
128+
files: readonly string[],
129+
) {
130+
const rootDir = opts.rootDir || getCommonDirectory(files);
131+
const declDir = opts.declarationDir || opts.outDir || rootDir;
132+
133+
for (const file of files) {
134+
const mapFile = resolve(declDir, relative(rootDir, file)).replace(
135+
/\.([cm]?[tj]s)x?$/,
136+
".d.$1",
137+
);
138+
declarationMapCache.set(mapFile, file);
139+
}
140+
}

0 commit comments

Comments
 (0)