diff --git a/CHANGELOG.md b/CHANGELOG.md index 97b691653..5bffc3eff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Introduced a `skipErrorChecking` option which instructs TypeDoc to not ask TypeScript for compiler errors before attempting to generate documentation. Turning this on may improve generation speed, but could also cause a crash if your code contains compiler errors. +- Added support for JS entry points when using packages mode, #2037. ### Bug Fixes diff --git a/src/lib/utils/entry-point.ts b/src/lib/utils/entry-point.ts index b2efcd215..b571bcb77 100644 --- a/src/lib/utils/entry-point.ts +++ b/src/lib/utils/entry-point.ts @@ -13,6 +13,7 @@ import { createMinimatch, matchesAny, nicePath } from "./paths"; import type { Logger } from "./loggers"; import type { Options } from "./options"; import { getCommonDirectory, glob, normalizePath } from "./fs"; +import { validate } from "./validation"; /** * Defines how entry points are interpreted. @@ -386,16 +387,12 @@ function getEntryPointsForPackages( const sourceFile = program.getSourceFile(packageEntryPoint); if (sourceFile === undefined) { logger.error( - `Entry point "${packageEntryPoint}" does not appear to be built by the tsconfig found at "${tsconfigFile}"` + `Entry point "${packageEntryPoint}" does not appear to be built by/included in the tsconfig found at "${tsconfigFile}"` ); return; } - if ( - includeVersion && - (!packageJson["version"] || - typeof packageJson["version"] !== "string") - ) { + if (includeVersion && !validate({ version: String }, packageJson)) { logger.warn( `--includeVersion was specified, but "${nicePath( packageJsonPath diff --git a/src/lib/utils/package-manifest.ts b/src/lib/utils/package-manifest.ts index 56fc65a88..a47f55e4b 100644 --- a/src/lib/utils/package-manifest.ts +++ b/src/lib/utils/package-manifest.ts @@ -166,8 +166,10 @@ function getTsSourceFromJsSource( const sourceMapPrefix = "\n//# sourceMappingURL="; const indexOfSourceMapPrefix = contents.indexOf(sourceMapPrefix); if (indexOfSourceMapPrefix === -1) { - logger.error(`The file ${jsPath} does not contain a sourceMappingURL`); - return; + logger.verbose( + `The file ${jsPath} does not contain a sourceMappingURL` + ); + return jsPath; } const endOfSourceMapPrefix = indexOfSourceMapPrefix + sourceMapPrefix.length; @@ -253,30 +255,18 @@ export function getTsEntryPointForPackage( ); if (typedocPackageConfig?.entryPoint) { packageMain = typedocPackageConfig.entryPoint; - } else if ( - hasOwnProperty(packageJson, "typedocMain") && - typeof packageJson.typedocMain == "string" - ) { + } else if (validate({ typedocMain: String }, packageJson)) { logger.warn( `Legacy typedoc entry point config (using "typedocMain" field) found for "${nicePath( packageJsonPath )}". Please update to use "typedoc": { "entryPoint": "..." } instead. In future upgrade, "typedocMain" field will be ignored.` ); packageMain = packageJson.typedocMain; - } else if ( - hasOwnProperty(packageJson, "main") && - typeof packageJson.main == "string" - ) { + } else if (validate({ main: String }, packageJson)) { packageMain = packageJson.main; - } else if ( - hasOwnProperty(packageJson, "types") && - typeof packageJson.types == "string" - ) { + } else if (validate({ types: String }, packageJson)) { packageTypes = packageJson.types; - } else if ( - hasOwnProperty(packageJson, "typings") && - typeof packageJson.typings == "string" - ) { + } else if (validate({ typings: String }, packageJson)) { packageTypes = packageJson.typings; } let entryPointPath = resolve(packageJsonPath, "..", packageMain); @@ -287,7 +277,7 @@ export function getTsEntryPointForPackage( try { entryPointPath = require.resolve(entryPointPath, { paths: [] }); if ( - /\.([cm]ts|tsx?)$/.test(entryPointPath) && + /\.([cm]?ts|tsx?)$/.test(entryPointPath) && existsSync(entryPointPath) ) { return entryPointPath; @@ -302,7 +292,7 @@ export function getTsEntryPointForPackage( packageTypes ?? packageMain ); if ( - /\.([cm][tj]s|tsx?)$/.test(entryPointPath) && + /\.([cm]?[tj]s|tsx?)$/.test(entryPointPath) && existsSync(entryPointPath) ) { return entryPointPath; diff --git a/src/test/packages.test.ts b/src/test/packages.test.ts index 6ec429bc4..55f70e86d 100644 --- a/src/test/packages.test.ts +++ b/src/test/packages.test.ts @@ -253,4 +253,32 @@ describe("Packages support", () => { logger.expectNoOtherMessages(); equal(packages, [normalizePath(project.cwd)]); }); + + it("Handles js entry points (#2037)", () => { + project.addJsonFile("tsconfig.json", { + compilerOptions: { + strict: true, + checkJs: true, + }, + include: ["src"], + }); + const packageJson = { + name: "typedoc-js-package", + main: "src/index.js", + }; + project.addJsonFile("package.json", packageJson); + project.addFile("src/index.js", `exports.foo = 123;`); + project.write(); + + const logger = new TestLogger(); + const entry = getTsEntryPointForPackage( + logger, + join(project.cwd, "package.json"), + packageJson + ); + + logger.discardDebugMessages(); + logger.expectNoOtherMessages(); + equal(entry, join(project.cwd, "src/index.js")); + }); });