Skip to content

Commit baf9cc5

Browse files
committed
fix: Fix some packages not finding an entry point.
1 parent aeda24b commit baf9cc5

File tree

1 file changed

+57
-8
lines changed

1 file changed

+57
-8
lines changed

packages/plugin/src/plugin/data.ts

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import ts from 'typescript';
66
import { normalizeUrl } from '@docusaurus/utils';
77
import type {
88
DocusaurusPluginTypeDocApiOptions,
9+
PackageEntryConfig,
910
PackageReflectionGroup,
1011
ResolvedPackageConfig,
1112
TSDDeclarationReflection,
@@ -221,7 +222,7 @@ function sortReflectionGroups(reflections: TSDDeclarationReflection[]) {
221222
});
222223
}
223224

224-
function matchesEntryPoint(
225+
function sourceFileMatchesEntryPoint(
225226
sourceFile: string,
226227
entryPoint: string,
227228
{ deep, single }: { deep: boolean; single: boolean },
@@ -248,6 +249,43 @@ function matchesEntryPoint(
248249
);
249250
}
250251

252+
function modContainsEntryPoint(
253+
mod: JSONOutput.DeclarationReflection,
254+
entry: PackageEntryConfig,
255+
meta: {
256+
allSourceFiles: Record<string, boolean>;
257+
packagePath: string;
258+
isSinglePackage: boolean;
259+
isUsingDeepImports: boolean;
260+
},
261+
) {
262+
const relModSources = mod.sources ?? [];
263+
const relModSourceFile = relModSources.find((sf) => !!sf.fileName)?.fileName ?? '';
264+
const relEntryPoint = joinUrl(meta.packagePath, entry.path);
265+
266+
// Monorepos of 1 package don't have sources, so use the child sources.
267+
// They also don't use full paths like "package/src/index.ts" and simply use "index.ts",
268+
// so account for those entry points also.
269+
if (!relModSourceFile) {
270+
const relEntryPointName = path.basename(relEntryPoint);
271+
const relEntryPointInSourceFiles =
272+
!!meta.allSourceFiles[relEntryPoint] ||
273+
(relEntryPointName.startsWith('index.') && !!meta.allSourceFiles[relEntryPointName]);
274+
275+
if (relEntryPointInSourceFiles) {
276+
return sourceFileMatchesEntryPoint(relEntryPoint, relEntryPoint, {
277+
deep: meta.isUsingDeepImports,
278+
single: meta.isSinglePackage,
279+
});
280+
}
281+
}
282+
283+
return sourceFileMatchesEntryPoint(relModSourceFile, relEntryPoint, {
284+
deep: meta.isUsingDeepImports,
285+
single: meta.isSinglePackage,
286+
});
287+
}
288+
251289
function extractReflectionModules(
252290
project: JSONOutput.ProjectReflection,
253291
isSinglePackage: boolean,
@@ -289,6 +327,18 @@ function extractReflectionModules(
289327
return modules;
290328
}
291329

330+
function buildSourceFileNameMap(modChildren: JSONOutput.DeclarationReflection[]) {
331+
const map: Record<string, boolean> = {};
332+
333+
modChildren.forEach((child) => {
334+
child.sources?.forEach((sf) => {
335+
map[sf.fileName] = true;
336+
});
337+
});
338+
339+
return map;
340+
}
341+
292342
export function flattenAndGroupPackages(
293343
packageConfigs: ResolvedPackageConfig[],
294344
project: JSONOutput.ProjectReflection,
@@ -304,19 +354,18 @@ export function flattenAndGroupPackages(
304354
const packagesWithDeepImports: TSDDeclarationReflection[] = [];
305355

306356
modules.forEach((mod) => {
307-
// Monorepos of 1 package don't have sources, so use the child sources
308-
const relSources = mod.sources ?? mod.children?.[0].sources ?? [];
309-
const relSourceFile = relSources.find((sf) => !!sf.fileName)?.fileName ?? '';
357+
const allSourceFiles = buildSourceFileNameMap(mod.children ?? []);
310358

311359
packageConfigs.some((cfg) =>
312360
Object.entries(cfg.entryPoints).some(([importPath, entry]) => {
313-
const relEntryPoint = joinUrl(cfg.packagePath, entry.path);
314361
const isUsingDeepImports = !entry.path.match(/\.tsx?$/);
315362

316363
if (
317-
!matchesEntryPoint(relSourceFile, relEntryPoint, {
318-
deep: isUsingDeepImports,
319-
single: isSinglePackage,
364+
!modContainsEntryPoint(mod, entry, {
365+
allSourceFiles,
366+
isSinglePackage,
367+
isUsingDeepImports,
368+
packagePath: cfg.packagePath,
320369
})
321370
) {
322371
return false;

0 commit comments

Comments
 (0)