Skip to content

Commit

Permalink
Add support for JS entry points in packages mode
Browse files Browse the repository at this point in the history
Resolves #2037
  • Loading branch information
Gerrit0 committed Aug 26, 2022
1 parent 25582a7 commit 0904477
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
9 changes: 3 additions & 6 deletions src/lib/utils/entry-point.ts
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
30 changes: 10 additions & 20 deletions src/lib/utils/package-manifest.ts
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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;
Expand Down
28 changes: 28 additions & 0 deletions src/test/packages.test.ts
Expand Up @@ -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"));
});
});

0 comments on commit 0904477

Please sign in to comment.