Skip to content

Commit 3b5174e

Browse files
committed
Add detection for multiple loads of the package
1 parent d6910ff commit 3b5174e

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Unreleased
22

3+
### Features
4+
5+
- TypeDoc will now detect and warn if multiple instances of the package are loaded. This usually means that a plugin has its own version of TypeDoc installed, which will lead to things breaking in unexpected ways.
6+
It will only work if both loaded TypeDocs are v0.22.9 or later.
7+
38
### Bug Fixes
49

510
- Corrected HTML generation for projects using Google Analytics, #1786.

src/lib/application.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
getWatchEntryPoints,
3434
} from "./utils/entry-point";
3535
import { nicePath } from "./utils/paths";
36+
import { hasBeenLoadedMultipleTimes } from "./utils/general";
3637

3738
// eslint-disable-next-line @typescript-eslint/no-var-requires
3839
const packageInfo = require("../../package.json") as {
@@ -149,6 +150,12 @@ export class Application extends ChildableComponent<
149150
}
150151
}
151152
this.options.read(this.logger);
153+
154+
if (hasBeenLoadedMultipleTimes()) {
155+
this.logger.warn(
156+
`TypeDoc has been loaded multiple times. This is commonly caused by plugins which have their own installation of TypeDoc. This will likely break things.`
157+
);
158+
}
152159
}
153160

154161
/**

src/lib/utils/general.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,18 @@ export type IfInternal<T, F> = InternalOnly extends true ? T : F;
3232
* See {@link IfInternal} for the rationale.
3333
*/
3434
export type NeverIfInternal<T> = IfInternal<never, T>;
35+
36+
/**
37+
* This is a hack to make it possible to detect and warn about installation setups
38+
* which result in TypeDoc being installed multiple times. If TypeDoc has been loaded
39+
* multiple times, then parts of it will not work as expected.
40+
*/
41+
const loadSymbol = Symbol.for("typedoc_loads");
42+
const getLoads = () => globalThis[loadSymbol as never] || 0;
43+
44+
// @ts-expect-error
45+
globalThis[loadSymbol] = getLoads() + 1;
46+
47+
export function hasBeenLoadedMultipleTimes() {
48+
return getLoads() !== 1;
49+
}

0 commit comments

Comments
 (0)