Skip to content

Commit

Permalink
Add detection for multiple loads of the package
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit0 committed Nov 14, 2021
1 parent d6910ff commit 3b5174e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Unreleased

### Features

- 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.
It will only work if both loaded TypeDocs are v0.22.9 or later.

### Bug Fixes

- Corrected HTML generation for projects using Google Analytics, #1786.
Expand Down
7 changes: 7 additions & 0 deletions src/lib/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
getWatchEntryPoints,
} from "./utils/entry-point";
import { nicePath } from "./utils/paths";
import { hasBeenLoadedMultipleTimes } from "./utils/general";

// eslint-disable-next-line @typescript-eslint/no-var-requires
const packageInfo = require("../../package.json") as {
Expand Down Expand Up @@ -149,6 +150,12 @@ export class Application extends ChildableComponent<
}
}
this.options.read(this.logger);

if (hasBeenLoadedMultipleTimes()) {
this.logger.warn(
`TypeDoc has been loaded multiple times. This is commonly caused by plugins which have their own installation of TypeDoc. This will likely break things.`
);
}
}

/**
Expand Down
15 changes: 15 additions & 0 deletions src/lib/utils/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,18 @@ export type IfInternal<T, F> = InternalOnly extends true ? T : F;
* See {@link IfInternal} for the rationale.
*/
export type NeverIfInternal<T> = IfInternal<never, T>;

/**
* This is a hack to make it possible to detect and warn about installation setups
* which result in TypeDoc being installed multiple times. If TypeDoc has been loaded
* multiple times, then parts of it will not work as expected.
*/
const loadSymbol = Symbol.for("typedoc_loads");
const getLoads = () => globalThis[loadSymbol as never] || 0;

// @ts-expect-error
globalThis[loadSymbol] = getLoads() + 1;

export function hasBeenLoadedMultipleTimes() {
return getLoads() !== 1;
}

0 comments on commit 3b5174e

Please sign in to comment.