Skip to content

Commit bf7ecb7

Browse files
Improve TypeDoc output for dev package (#11981)
1 parent 6ccf92a commit bf7ecb7

File tree

10 files changed

+113
-39
lines changed

10 files changed

+113
-39
lines changed

packages/react-router-dev/config/routes.ts

+50-9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ export function setAppDirectory(directory: string) {
88
appDirectory = directory;
99
}
1010

11+
/**
12+
* Provides the absolute path to the app directory, for use within `routes.ts`.
13+
* This is designed to support resolving file system routes.
14+
*/
1115
export function getAppDirectory() {
1216
invariant(appDirectory);
1317
return appDirectory;
@@ -52,10 +56,15 @@ export interface RouteManifest {
5256
[routeId: string]: RouteManifestEntry;
5357
}
5458

59+
/**
60+
* Route config to be exported via the `routes` export within `routes.ts`.
61+
*/
5562
export type RouteConfig = RouteConfigEntry[] | Promise<RouteConfigEntry[]>;
5663

5764
/**
58-
* A route exported from the routes config file
65+
* Configuration for an individual route, for use within `routes.ts`. As a
66+
* convenience, route config entries can be created with the {@link route},
67+
* {@link index} and {@link layout} helper functions.
5968
*/
6069
export interface RouteConfigEntry {
6170
/**
@@ -90,8 +99,6 @@ export interface RouteConfigEntry {
9099
children?: RouteConfigEntry[];
91100
}
92101

93-
type CreateRoutePath = string | null | undefined;
94-
95102
const createConfigRouteOptionKeys = [
96103
"id",
97104
"index",
@@ -101,19 +108,23 @@ type CreateRouteOptions = Pick<
101108
RouteConfigEntry,
102109
(typeof createConfigRouteOptionKeys)[number]
103110
>;
111+
/**
112+
* Helper function for creating a route config entry, for use within
113+
* `routes.ts`.
114+
*/
104115
function createRoute(
105-
path: CreateRoutePath,
116+
path: string | null | undefined,
106117
file: string,
107118
children?: RouteConfigEntry[]
108119
): RouteConfigEntry;
109120
function createRoute(
110-
path: CreateRoutePath,
121+
path: string | null | undefined,
111122
file: string,
112123
options: CreateRouteOptions,
113124
children?: RouteConfigEntry[]
114125
): RouteConfigEntry;
115126
function createRoute(
116-
path: CreateRoutePath,
127+
path: string | null | undefined,
117128
file: string,
118129
optionsOrChildren: CreateRouteOptions | RouteConfigEntry[] | undefined,
119130
children?: RouteConfigEntry[]
@@ -141,6 +152,10 @@ type CreateIndexOptions = Pick<
141152
RouteConfigEntry,
142153
(typeof createIndexOptionKeys)[number]
143154
>;
155+
/**
156+
* Helper function for creating a route config entry for an index route, for use
157+
* within `routes.ts`.
158+
*/
144159
function createIndex(
145160
file: string,
146161
options?: CreateIndexOptions
@@ -159,6 +174,10 @@ type CreateLayoutOptions = Pick<
159174
RouteConfigEntry,
160175
(typeof createLayoutOptionKeys)[number]
161176
>;
177+
/**
178+
* Helper function for creating a route config entry for a layout route, for use
179+
* within `routes.ts`.
180+
*/
162181
function createLayout(
163182
file: string,
164183
children?: RouteConfigEntry[]
@@ -191,19 +210,41 @@ function createLayout(
191210
export const route = createRoute;
192211
export const index = createIndex;
193212
export const layout = createLayout;
194-
type RouteHelpers = {
213+
/**
214+
* Creates a set of route config helpers that resolve file paths relative to the
215+
* given directory, for use within `routes.ts`. This is designed to support
216+
* splitting route config into multiple files within different directories.
217+
*/
218+
export function relative(directory: string): {
195219
route: typeof route;
196220
index: typeof index;
197221
layout: typeof layout;
198-
};
199-
export function relative(directory: string): RouteHelpers {
222+
} {
200223
return {
224+
/**
225+
* Helper function for creating a route config entry, for use within
226+
* `routes.ts`. Note that this helper has been scoped, meaning that file
227+
* path will be resolved relative to the directory provided to the
228+
* `relative` call that created this helper.
229+
*/
201230
route: (path, file, ...rest) => {
202231
return route(path, resolve(directory, file), ...(rest as any));
203232
},
233+
/**
234+
* Helper function for creating a route config entry for an index route, for
235+
* use within `routes.ts`. Note that this helper has been scoped, meaning
236+
* that file path will be resolved relative to the directory provided to the
237+
* `relative` call that created this helper.
238+
*/
204239
index: (file, ...rest) => {
205240
return index(resolve(directory, file), ...(rest as any));
206241
},
242+
/**
243+
* Helper function for creating a route config entry for a layout route, for
244+
* use within `routes.ts`. Note that this helper has been scoped, meaning
245+
* that file path will be resolved relative to the directory provided to the
246+
* `relative` call that created this helper.
247+
*/
207248
layout: (file, ...rest) => {
208249
return layout(resolve(directory, file), ...(rest as any));
209250
},

packages/react-router-dev/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
"default": "./dist/vite.js"
2929
},
3030
"./vite/cloudflare": {
31-
"types": "./dist/vite-cloudflare.d.ts",
32-
"default": "./dist/vite-cloudflare.js"
31+
"types": "./dist/vite/cloudflare.d.ts",
32+
"default": "./dist/vite/cloudflare.js"
3333
},
3434
"./package.json": "./package.json"
3535
},

packages/react-router-dev/rollup.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module.exports = function rollup() {
3030
`${SOURCE_DIR}/index.ts`,
3131
`${SOURCE_DIR}/routes.ts`,
3232
`${SOURCE_DIR}/vite.ts`,
33-
`${SOURCE_DIR}/vite-cloudflare.ts`,
33+
`${SOURCE_DIR}/vite/cloudflare.ts`,
3434
],
3535
output: {
3636
banner: createBanner("@react-router/dev", version),
+6-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
{
2-
"entryPoints": ["./index.ts"]
2+
"entryPoints": [
3+
"./index.ts",
4+
"./routes.ts",
5+
"./vite.ts",
6+
"./vite/cloudflare.ts"
7+
]
38
}

packages/react-router-dev/vite/cloudflare-dev-proxy.ts

+16-7
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,19 @@ function importWrangler() {
2828

2929
const PLUGIN_NAME = "react-router-cloudflare-vite-dev-proxy";
3030

31-
export const cloudflareDevProxyVitePlugin = <Env, Cf extends CfProperties>({
32-
getLoadContext,
33-
...options
34-
}: {
35-
getLoadContext?: GetLoadContext<Env, Cf>;
36-
} & GetPlatformProxyOptions = {}): Plugin => {
31+
/**
32+
* Vite plugin that provides [Node proxies to local workerd
33+
* bindings](https://developers.cloudflare.com/workers/wrangler/api/#getplatformproxy)
34+
* to `context.cloudflare` in your server loaders and server actions during
35+
* development.
36+
*/
37+
export const cloudflareDevProxyVitePlugin = <Env, Cf extends CfProperties>(
38+
options: {
39+
getLoadContext?: GetLoadContext<Env, Cf>;
40+
} & GetPlatformProxyOptions = {}
41+
): Plugin => {
42+
let { getLoadContext, ...restOptions } = options;
43+
3744
return {
3845
name: PLUGIN_NAME,
3946
config: () => ({
@@ -59,7 +66,9 @@ export const cloudflareDevProxyVitePlugin = <Env, Cf extends CfProperties>({
5966
configureServer: async (viteDevServer) => {
6067
let { getPlatformProxy } = await importWrangler();
6168
// Do not include `dispose` in Cloudflare context
62-
let { dispose, ...cloudflare } = await getPlatformProxy<Env, Cf>(options);
69+
let { dispose, ...cloudflare } = await getPlatformProxy<Env, Cf>(
70+
restOptions
71+
);
6372
let context = { cloudflare };
6473
return () => {
6574
if (!viteDevServer.config.server.middlewareMode) {
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { cloudflareDevProxyVitePlugin as cloudflareDevProxy } from "./vite/cloudflare-dev-proxy";
1+
export { cloudflareDevProxyVitePlugin as cloudflareDevProxy } from "./cloudflare-dev-proxy";

packages/react-router-dev/vite/plugin.ts

+3
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,9 @@ let deepFreeze = (o: any) => {
415415
};
416416

417417
type ReactRouterVitePlugin = (config?: ReactRouterConfig) => Vite.Plugin[];
418+
/**
419+
* React Router [Vite plugin.](https://vitejs.dev/guide/using-plugins.html)
420+
*/
418421
export const reactRouterVitePlugin: ReactRouterVitePlugin = (_config) => {
419422
let reactRouterUserConfig = _config ?? {};
420423

packages/react-router-fs-routes/index.ts

+22-11
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,29 @@ import { routeManifestToRouteConfig } from "./manifest";
99
import { flatRoutes as flatRoutesImpl } from "./flatRoutes";
1010
import { normalizeSlashes } from "./normalizeSlashes";
1111

12-
export async function flatRoutes({
13-
ignoredRouteFiles,
14-
rootDirectory: userRootDirectory = "./routes",
15-
}: {
16-
ignoredRouteFiles?: string[];
12+
/**
13+
* Creates route config from the file system using a convention that matches
14+
* [Remix v2's route file
15+
* naming](https://remix.run/docs/en/v2/file-conventions/routes-files), for use
16+
* within `routes.ts`.
17+
*/
18+
export async function flatRoutes(
19+
options: {
20+
/**
21+
* An array of [minimatch](https://www.npmjs.com/package/minimatch) globs that match files to ignore.
22+
* Defaults to `[]`.
23+
*/
24+
ignoredRouteFiles?: string[];
1725

18-
/**
19-
* The directory containing file system routes, relative to the app directory.
20-
* Defaults to `./routes`.
21-
*/
22-
rootDirectory?: string;
23-
} = {}): Promise<RouteConfigEntry[]> {
26+
/**
27+
* The directory containing file system routes, relative to the app directory.
28+
* Defaults to `"./routes"`.
29+
*/
30+
rootDirectory?: string;
31+
} = {}
32+
): Promise<RouteConfigEntry[]> {
33+
let { ignoredRouteFiles = [], rootDirectory: userRootDirectory = "routes" } =
34+
options;
2435
let appDirectory = getAppDirectory();
2536
let rootDirectory = path.resolve(appDirectory, userRootDirectory);
2637
let relativeRootDirectory = path.relative(appDirectory, rootDirectory);

packages/react-router-remix-config-routes-adapter/defineRoutes.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import type { RouteManifest, RouteManifestEntry } from "./manifest";
22
import { normalizeSlashes } from "./normalizeSlashes";
33

4-
export type DefineRoutesFunction = typeof defineRoutes;
4+
export type DefineRoutesFunction = (
5+
callback: (defineRoute: DefineRouteFunction) => void
6+
) => RouteManifest;
57

68
interface DefineRouteOptions {
79
/**
@@ -55,9 +57,7 @@ interface DefineRouteFunction {
5557
* A function for defining routes programmatically, instead of using the
5658
* filesystem convention.
5759
*/
58-
export function defineRoutes(
59-
callback: (defineRoute: DefineRouteFunction) => void
60-
): RouteManifest {
60+
export const defineRoutes: DefineRoutesFunction = (callback) => {
6161
let routes: RouteManifest = Object.create(null);
6262
let parentRoutes: RouteManifestEntry[] = [];
6363
let alreadyReturned = false;
@@ -119,7 +119,7 @@ export function defineRoutes(
119119
alreadyReturned = true;
120120

121121
return routes;
122-
}
122+
};
123123

124124
function createRouteId(file: string) {
125125
return normalizeSlashes(stripFileExtension(file));

packages/react-router-remix-config-routes-adapter/index.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@ import { defineRoutes, type DefineRoutesFunction } from "./defineRoutes";
55

66
export type { DefineRoutesFunction };
77

8+
/**
9+
* Adapts routes defined using [Remix's `routes` config
10+
* option](https://remix.run/docs/en/v2/file-conventions/vite-config#routes) to
11+
* React Router's config format, for use within `routes.ts`.
12+
*/
813
export async function remixConfigRoutes(
9-
customRoutes: (
14+
routes: (
1015
defineRoutes: DefineRoutesFunction
1116
) =>
1217
| ReturnType<DefineRoutesFunction>
1318
| Promise<ReturnType<DefineRoutesFunction>>
1419
): Promise<RouteConfigEntry[]> {
15-
let routeManifest = await customRoutes(defineRoutes);
20+
let routeManifest = await routes(defineRoutes);
1621
return routeManifestToRouteConfig(routeManifest);
1722
}

0 commit comments

Comments
 (0)