Skip to content

Commit 0904477

Browse files
committed
Add support for JS entry points in packages mode
Resolves #2037
1 parent 25582a7 commit 0904477

File tree

4 files changed

+42
-26
lines changed

4 files changed

+42
-26
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- Introduced a `skipErrorChecking` option which instructs TypeDoc to not ask TypeScript for compiler errors
77
before attempting to generate documentation. Turning this on may improve generation speed, but could also
88
cause a crash if your code contains compiler errors.
9+
- Added support for JS entry points when using packages mode, #2037.
910

1011
### Bug Fixes
1112

src/lib/utils/entry-point.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { createMinimatch, matchesAny, nicePath } from "./paths";
1313
import type { Logger } from "./loggers";
1414
import type { Options } from "./options";
1515
import { getCommonDirectory, glob, normalizePath } from "./fs";
16+
import { validate } from "./validation";
1617

1718
/**
1819
* Defines how entry points are interpreted.
@@ -386,16 +387,12 @@ function getEntryPointsForPackages(
386387
const sourceFile = program.getSourceFile(packageEntryPoint);
387388
if (sourceFile === undefined) {
388389
logger.error(
389-
`Entry point "${packageEntryPoint}" does not appear to be built by the tsconfig found at "${tsconfigFile}"`
390+
`Entry point "${packageEntryPoint}" does not appear to be built by/included in the tsconfig found at "${tsconfigFile}"`
390391
);
391392
return;
392393
}
393394

394-
if (
395-
includeVersion &&
396-
(!packageJson["version"] ||
397-
typeof packageJson["version"] !== "string")
398-
) {
395+
if (includeVersion && !validate({ version: String }, packageJson)) {
399396
logger.warn(
400397
`--includeVersion was specified, but "${nicePath(
401398
packageJsonPath

src/lib/utils/package-manifest.ts

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,10 @@ function getTsSourceFromJsSource(
166166
const sourceMapPrefix = "\n//# sourceMappingURL=";
167167
const indexOfSourceMapPrefix = contents.indexOf(sourceMapPrefix);
168168
if (indexOfSourceMapPrefix === -1) {
169-
logger.error(`The file ${jsPath} does not contain a sourceMappingURL`);
170-
return;
169+
logger.verbose(
170+
`The file ${jsPath} does not contain a sourceMappingURL`
171+
);
172+
return jsPath;
171173
}
172174
const endOfSourceMapPrefix =
173175
indexOfSourceMapPrefix + sourceMapPrefix.length;
@@ -253,30 +255,18 @@ export function getTsEntryPointForPackage(
253255
);
254256
if (typedocPackageConfig?.entryPoint) {
255257
packageMain = typedocPackageConfig.entryPoint;
256-
} else if (
257-
hasOwnProperty(packageJson, "typedocMain") &&
258-
typeof packageJson.typedocMain == "string"
259-
) {
258+
} else if (validate({ typedocMain: String }, packageJson)) {
260259
logger.warn(
261260
`Legacy typedoc entry point config (using "typedocMain" field) found for "${nicePath(
262261
packageJsonPath
263262
)}". Please update to use "typedoc": { "entryPoint": "..." } instead. In future upgrade, "typedocMain" field will be ignored.`
264263
);
265264
packageMain = packageJson.typedocMain;
266-
} else if (
267-
hasOwnProperty(packageJson, "main") &&
268-
typeof packageJson.main == "string"
269-
) {
265+
} else if (validate({ main: String }, packageJson)) {
270266
packageMain = packageJson.main;
271-
} else if (
272-
hasOwnProperty(packageJson, "types") &&
273-
typeof packageJson.types == "string"
274-
) {
267+
} else if (validate({ types: String }, packageJson)) {
275268
packageTypes = packageJson.types;
276-
} else if (
277-
hasOwnProperty(packageJson, "typings") &&
278-
typeof packageJson.typings == "string"
279-
) {
269+
} else if (validate({ typings: String }, packageJson)) {
280270
packageTypes = packageJson.typings;
281271
}
282272
let entryPointPath = resolve(packageJsonPath, "..", packageMain);
@@ -287,7 +277,7 @@ export function getTsEntryPointForPackage(
287277
try {
288278
entryPointPath = require.resolve(entryPointPath, { paths: [] });
289279
if (
290-
/\.([cm]ts|tsx?)$/.test(entryPointPath) &&
280+
/\.([cm]?ts|tsx?)$/.test(entryPointPath) &&
291281
existsSync(entryPointPath)
292282
) {
293283
return entryPointPath;
@@ -302,7 +292,7 @@ export function getTsEntryPointForPackage(
302292
packageTypes ?? packageMain
303293
);
304294
if (
305-
/\.([cm][tj]s|tsx?)$/.test(entryPointPath) &&
295+
/\.([cm]?[tj]s|tsx?)$/.test(entryPointPath) &&
306296
existsSync(entryPointPath)
307297
) {
308298
return entryPointPath;

src/test/packages.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,4 +253,32 @@ describe("Packages support", () => {
253253
logger.expectNoOtherMessages();
254254
equal(packages, [normalizePath(project.cwd)]);
255255
});
256+
257+
it("Handles js entry points (#2037)", () => {
258+
project.addJsonFile("tsconfig.json", {
259+
compilerOptions: {
260+
strict: true,
261+
checkJs: true,
262+
},
263+
include: ["src"],
264+
});
265+
const packageJson = {
266+
name: "typedoc-js-package",
267+
main: "src/index.js",
268+
};
269+
project.addJsonFile("package.json", packageJson);
270+
project.addFile("src/index.js", `exports.foo = 123;`);
271+
project.write();
272+
273+
const logger = new TestLogger();
274+
const entry = getTsEntryPointForPackage(
275+
logger,
276+
join(project.cwd, "package.json"),
277+
packageJson
278+
);
279+
280+
logger.discardDebugMessages();
281+
logger.expectNoOtherMessages();
282+
equal(entry, join(project.cwd, "src/index.js"));
283+
});
256284
});

0 commit comments

Comments
 (0)