Skip to content

Commit

Permalink
Handle reexports. (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Jan 24, 2024
1 parent ff45d2b commit 609608e
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 5 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ jobs:
repo-type:
- monorepo
- monorepo-1
- monorepo-reexports
- polyrepo
- polyrepo-deep
- polyrepo-multi
Expand Down
7 changes: 7 additions & 0 deletions fixtures/monorepo-1-package/reexports/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# STANDARD

```ts
function foo(num: number) {
return 123;
}
```
4 changes: 4 additions & 0 deletions fixtures/monorepo-1-package/reexports/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "reexports",
"version": "1.0.0"
}
1 change: 1 addition & 0 deletions fixtures/monorepo-1-package/reexports/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './other';
112 changes: 112 additions & 0 deletions fixtures/monorepo-1-package/reexports/src/other.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/**
* a type
* @beta
*/
export type Type = 'standard';

/**
* short description
*
* long description with a link to {@link bizz}. did it work?
* what about our own tokens: {@apilink Foo} and {@doclink intro}!
* and some inline `code`???
*
* ```
* let dontForget = 'block code';
* ```
*
* @param {String} msg description
* @param other without type
* @returns returns the param
*/
export function comments(msg: string, other: boolean) {}

/**
* newy new guy
* @param a a thing
* @param b b thing
* @alpha
*/
export function bizz(a: string, b: string): string;

/**
* newy new guy
* @param a a thing
* @param b b thing
* @param c c thing
* @beta
*/
export function bizz(a: string, b: string, c: string): string;

/**
* a thing for a thing
* @param a id
* @returns returns the param
* @beta
*/
export function bizz(...args: string[]): string {
return args[0];
}

/**
* @param {string} [a] thing
* @param {string} [b="b"] thing
* @param {string} [c="c override"] thing
*/
export function defs(a?: string, b?: string, c: string = 'c') {}

/**
* thing for a thing
* @beta
*/
export interface Foo {
/**
* very experimental
* @alpha
* @default "foo"
*/
foo: string;

/**
* very experimental
* @experimental
*/
a: string;

/**
* @default 123
*/
int?: number;
}

/**
* :::
* standard
* :::
*
* :::note
* with type
* :::
*
* ::: title
* title only
* :::
*
* :::info title
* with type
*
* and title
* :::
*
* :::success
*
* extra new lines
*
* :::
*/
export function admonitions() {}

/**
* @throws something
*/
export function errors() {}
17 changes: 17 additions & 0 deletions fixtures/monorepo-1-package/reexports/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"declarationDir": "dts",
"outDir": "dts",
"rootDir": "src",
"emitDeclarationOnly": true
},
"exclude": [
"dts",
"tests"
],
"extends": "../../../tsconfig.options.json",
"include": [
"src/**/*"
],
"references": []
}
3 changes: 3 additions & 0 deletions fixtures/monorepo-1-package/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
{
"path": "other"
},
{
"path": "reexports"
},
{
"path": "standard"
}
Expand Down
1 change: 1 addition & 0 deletions packages/plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export default function typedocApiPlugin(

return {
entryPoints: entries,
packageRoot: path.normalize(path.join(projectRoot, pkgConfig.path || '.')),
packagePath: pkgConfig.path || '.',
packageSlug: pkgConfig.slug ?? path.basename(pkgConfig.path),
// Load later on
Expand Down
22 changes: 18 additions & 4 deletions packages/plugin/src/plugin/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ function modContainsEntryPoint(
meta: {
allSourceFiles: Record<string, boolean>;
packagePath: string;
packageRoot: string;
isSinglePackage: boolean;
isUsingDeepImports: boolean;
},
Expand All @@ -267,12 +268,14 @@ function modContainsEntryPoint(
// They also don't use full paths like "package/src/index.ts" and simply use "index.ts",
// so account for those entry points also.
if (!relModSourceFile) {
const absEntryPoint = path.normalize(path.join(meta.packageRoot, entry.path));
const relEntryPointName = path.basename(relEntryPoint);
const relEntryPointInSourceFiles =
const entryPointInSourceFiles =
!!meta.allSourceFiles[absEntryPoint] ||
!!meta.allSourceFiles[relEntryPoint] ||
(relEntryPointName.startsWith('index.') && !!meta.allSourceFiles[relEntryPointName]);

if (relEntryPointInSourceFiles) {
if (entryPointInSourceFiles) {
return sourceFileMatchesEntryPoint(relEntryPoint, relEntryPoint, {
deep: meta.isUsingDeepImports,
single: meta.isSinglePackage,
Expand Down Expand Up @@ -327,11 +330,21 @@ function extractReflectionModules(
return modules;
}

function buildSourceFileNameMap(modChildren: JSONOutput.DeclarationReflection[]) {
function buildSourceFileNameMap(
project: JSONOutput.ProjectReflection,
modChildren: JSONOutput.DeclarationReflection[],
) {
const map: Record<string, boolean> = {};
const cwd = process.cwd();

Object.values(project.symbolIdMap).forEach((symbol) => {
// absolute
map[path.normalize(path.join(cwd, symbol.sourceFileName))] = true;
});

modChildren.forEach((child) => {
child.sources?.forEach((sf) => {
// relative
map[sf.fileName] = true;
});
});
Expand All @@ -354,7 +367,7 @@ export function flattenAndGroupPackages(
const packagesWithDeepImports: TSDDeclarationReflection[] = [];

modules.forEach((mod) => {
const allSourceFiles = buildSourceFileNameMap(mod.children ?? []);
const allSourceFiles = buildSourceFileNameMap(project, mod.children ?? []);

packageConfigs.some((cfg) =>
Object.entries(cfg.entryPoints).some(([importPath, entry]) => {
Expand All @@ -366,6 +379,7 @@ export function flattenAndGroupPackages(
isSinglePackage,
isUsingDeepImports,
packagePath: cfg.packagePath,
packageRoot: cfg.packageRoot,
})
) {
return false;
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { JSONOutput, TypeDocOptions } from 'typedoc';
import type { MDXPlugin } from '@docusaurus/mdx-loader'
import type { MDXPlugin } from '@docusaurus/mdx-loader';
import type {
PropSidebarItem,
VersionBanner,
Expand Down Expand Up @@ -54,6 +54,7 @@ export interface PackageConfig {

export interface ResolvedPackageConfig {
entryPoints: Record<string, PackageEntryConfig>;
packageRoot: string;
packagePath: string;
packageSlug: string;
packageName: string;
Expand Down
7 changes: 7 additions & 0 deletions website/docusaurus.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ const monorepoOnePackage = {
packages: ['standard'],
};

const monorepoReexports = {
projectRoot: path.join(__dirname, '../fixtures/monorepo-1-package'),
packages: ['reexports'],
};

// POLYREPO STANDARD
const polyrepo = {
projectRoot: path.join(__dirname, '../fixtures/polyrepo'),
Expand Down Expand Up @@ -122,6 +127,8 @@ function getPluginConfig() {
return monorepo;
case 'monorepo-1':
return monorepoOnePackage;
case 'monorepo-reexports':
return monorepoReexports;
case 'polyrepo':
return polyrepo;
case 'polyrepo-deep':
Expand Down

0 comments on commit 609608e

Please sign in to comment.