Skip to content

Commit 87f645b

Browse files
✨ feat(vite): add official consumer vite extension entry
Support docs.config vite module path and docs.vite.* convention files so consumers can prepend Vite plugins without pnpm-patching docs-kit. Public helpers ship from @lobehub/docs-kit/vite-extension.
1 parent a996bc9 commit 87f645b

11 files changed

Lines changed: 754 additions & 68 deletions

File tree

‎.agents/skills/using-docs-kit/SKILL.md‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ description: >
1313

1414
The consumer repo holds **only content + `docs.config.ts`**. The kit owns Vite and React Router
1515
config; do not add `vite.config.ts` or `react-router.config.ts` unless overriding (see
16-
[reference/config.md](reference/config.md)).
16+
[reference/config.md](reference/config.md)). Extend the kit Vite pipeline with `docs.vite.ts`
17+
(or `docs.config.ts` → `vite`) instead of patching the package.
1718

1819
lobe-ui itself is the reference consumer: `docs.config.ts`, `docs/`, `src/*/index.mdx`,
1920
`src/*/demos/`.
@@ -23,6 +24,7 @@ lobe-ui itself is the reference consumer: `docs.config.ts`, `docs/`, `src/*/inde
2324
```
2425
<repo>/
2526
├── docs.config.ts # defineDocsConfig({...}) — required
27+
├── docs.vite.ts # optional consumer Vite plugins (or set config.vite)
2628
├── package.json # lobedocs scripts
2729
├── docs/
2830
│ ├── index.mdx # "/" — required (frontmatter feeds SEO even with a custom homePage)
@@ -90,9 +92,9 @@ export default {
9092
};
9193
```
9294

93-
> The package's only stable JS entry is `@lobehub/docs-kit/react-router-config`; there is no
94-
> root export for `defineDocsConfig`. It is an identity function, so export a plain object.
95-
> (lobe-ui imports it from `./packages/docs-kit/src/config` only because it is the workspace.)
95+
> Stable JS entries: `@lobehub/docs-kit/react-router-config` and `@lobehub/docs-kit/vite-extension`.
96+
> There is no root export for `defineDocsConfig` (identity helper — a plain default export is fine).
97+
> (lobe-ui may import `./packages/docs-kit/src/config` only because it is the workspace.)
9698
9799
**Restart `lobedocs dev` after editing `docs.config.ts`** — config is cached per process.
98100

‎.agents/skills/using-docs-kit/reference/config.md‎

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,29 @@ Source of truth: `packages/docs-kit/src/config.ts` (`DocsConfig`). Loaded once p
66

77
## Top-level fields
88

9-
| Field | Type | Notes |
10-
| ----------------- | ---------------------------------------------------- | ------------------------------------------------------------------------------------------------------ |
11-
| `atomDirs` | `{ dir: string; type?: string; subType?: string }[]` | Required. Roots scanned recursively for `index.mdx` |
12-
| `title` | `string` | Required. Site name in `<title>`, OG, llms.txt |
13-
| `description` | `string` | Required. Site summary |
14-
| `siteUrl` | `string` | Required. Canonical origin (no trailing slash) for sitemap/OG/canonical |
15-
| `alias` | `Record<string, string>` | Vite aliases. Map your package name (and `pkg/es`) to `src` so demos import the package but run source |
16-
| `homePage` | `string` | Path to a module whose default export is the landing page. Missing file = build error |
17-
| `publicDocs` | `string[]` | Extra `docs/**/*.mdx` guide pages to publish (route = path without `docs/` and extension) |
18-
| `navSections` | `Record<string, string>` | `{ 'src/foo/Bar/index.mdx': 'Section' }` — only for a page sitting outside its section's folder |
19-
| `favicons` | `{ icon?, icon16?, icon32?, appleTouchIcon? }` | Hrefs into `public/`; unset keys fall back to lobe-ui's paths, so set all four |
20-
| `legacyRedirects` | `DocumentationInventory` | Frozen old-URL inventory (lobe-ui's `compatibility.json` from dumi). New sites: omit |
9+
| Field | Type | Notes |
10+
| ----------------- | ---------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
11+
| `atomDirs` | `{ dir: string; type?: string; subType?: string }[]` | Required. Roots scanned recursively for `index.mdx` |
12+
| `title` | `string` | Required. Site name in `<title>`, OG, llms.txt |
13+
| `description` | `string` | Required. Site summary |
14+
| `siteUrl` | `string` | Required. Canonical origin (no trailing slash) for sitemap/OG/canonical |
15+
| `alias` | `Record<string, string>` | Vite aliases. Map your package name (and `pkg/es`) to `src` so demos import the package but run source |
16+
| `homePage` | `string` | Path to a module whose default export is the landing page. Missing file = build error |
17+
| `publicDocs` | `string[]` | Extra `docs/**/*.mdx` guide pages to publish (route = path without `docs/` and extension) |
18+
| `navSections` | `Record<string, string>` | `{ 'src/foo/Bar/index.mdx': 'Section' }` — only for a page sitting outside its section's folder |
19+
| `favicons` | `{ icon?, icon16?, icon32?, appleTouchIcon? }` | Hrefs into `public/`; unset keys fall back to lobe-ui's paths, so set all four |
20+
| `legacyRedirects` | `DocumentationInventory` | Frozen old-URL inventory (lobe-ui's `compatibility.json` from dumi). New sites: omit |
21+
| `vite` | `string` | Module path for consumer Vite plugins / `{ plugins, optimizeDeps, ssr, apply }`. In-process load (hooks kept). Convention: `docs.vite.*` or `docs.kit-plugins.cjs`. See below. |
22+
23+
## Consumer Vite plugins (`vite` / `docs.vite.*`)
24+
25+
`docs.config.ts` cannot carry plugin hooks (JSON subprocess). Set `vite: './docs.vite.ts'` or add a root convention file (`docs.vite.ts|mts|mjs|js|cjs`, then legacy `docs.kit-plugins.cjs`). The kit prepends returned plugins before its own.
26+
27+
Public helper/types: `@lobehub/docs-kit/vite-extension` (`defineDocsVite`, etc.) — same publish style as `react-router-config`. Do not import `@lobehub/docs-kit/src/…` as an API.
28+
29+
Export a plugin, plugin array, `(env) => …` factory, or `{ plugins, optimizeDeps?, ssr?, apply? }`. Guard client-only transforms with `options?.ssr` (or `environment.consumer === 'server'`). Use `apply: 'serve'` only for dev-only hooks; demo transforms needed in the static client build must not set `apply: 'serve'`.
30+
31+
docs-kit does not ship or E2E-verify Ink/xterm plugins; consumers own those.
2132

2233
## `themeConfig`
2334

‎AGENTS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ The user-facing docs are the playground site (`pnpm dev`):
4747

4848
## Adding Things
4949

50-
- **Config field**: add it to `DocsConfig` / `DocsThemeConfig` in `src/config.ts`; if the browser needs it, it reaches the app through `virtual:lobedocs/site-config` (`ClientSiteConfig`). Values must survive JSON (the config is serialized out of a subprocess); pass functions as a module path, like `stylex`. Add a test, document it in `playground/docs/file-conventions/docs-config.mdx`, and update the `using-docs-kit` reference.
50+
- **Config field**: add it to `DocsConfig` / `DocsThemeConfig` in `src/config.ts`; if the browser needs it, it reaches the app through `virtual:lobedocs/site-config` (`ClientSiteConfig`). Values must survive JSON (the config is serialized out of a subprocess); pass functions as a module path, like `stylex` or `vite` (consumer Vite plugins). Add a test, document it in `playground/docs/file-conventions/docs-config.mdx`, and update the `using-docs-kit` reference.
5151
- **CLI command**: add a handler to `commands` in `src/cli/commands.ts` (usage text is derived from its keys), cover it in `src/cli/commands.test.ts`, and document it in `playground/docs/getting-started/cli.mdx` and the README table.
5252
- **Docs UI component**: `site/components/<Name>/<Name>.tsx` with a StyleX `style.ts` (named exports only; StyleX longhands such as `backgroundColor`, enforced by ESLint) and a colocated `<Name>.test.tsx`. To expose it in MDX, register it in `site/app/mdx-components.tsx` and update the "only `Demo` and `Api` are global" statements in docs and skills.
5353
- **Theme or appearance**: providers and the theme bootstrap live in `site/app/providers/`; global styles in `site/styles/`. Colors come from `@lobehub/ui` tokens, not hard-coded values.

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"files": [
2828
"bin",
2929
"react-router-config.ts",
30+
"vite-extension.ts",
3031
"site",
3132
"src",
3233
"vite.config.ts"

‎playground/docs/advanced/react-router.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ A config you write yourself always wins: the CLI only generates its temporary `r
3636

3737
## Stable entry points
3838

39-
`@lobehub/docs-kit/react-router-config` and the `lobedocs` CLI are the supported entry points. Other deep imports into the package are not stable yet.
39+
`@lobehub/docs-kit/react-router-config`, `@lobehub/docs-kit/vite-extension`, and the `lobedocs` CLI are the supported entry points. Other deep imports into the package are not stable yet.

‎playground/docs/file-conventions/docs-config.mdx‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export default {
5252
| `navSections` | `Record<string, string>` | Section override keyed by MDX source, for a page outside its section's folder |
5353
| `favicons` | `Record<string, string>` | Favicon hrefs keyed `icon`, `icon16`, `icon32` and `appleTouchIcon`. Unset keys fall back to `/favicon.ico`, `/favicon-16x16.png`, `/favicon-32x32.png` and `/apple-touch-icon.png`, so ship those in `public/` or set all four |
5454
| `stylex` | `string \| object` | StyleX options, or a path to a module that default-exports them. See [Theme and Appearance](/guide/theming) |
55+
| `vite` | `string` | Path to a module that default-exports consumer Vite plugins or a `{ plugins, optimizeDeps, ssr, apply }` object. Loaded in-process (hooks preserved). See below. |
5556
| `legacyRedirects` | `DocumentationInventory` | Frozen inventory of an old dumi site. See [Migrating from dumi](/advanced/migrating-from-dumi) |
5657

5758
### atomDirs
@@ -70,6 +71,66 @@ themeConfig: {
7071
},
7172
```
7273

74+
### vite
75+
76+
`docs.config.ts` is JSON-serialized, so Vite plugin hooks cannot live in the config object. Point `vite` at a module instead (same idea as `stylex`), or drop a convention file at the repository root:
77+
78+
- `docs.vite.ts` / `.mts` / `.mjs` / `.js` / `.cjs` (first match wins)
79+
- `docs.kit-plugins.cjs` (legacy filename from an unofficial consumer patch; still loaded for drop-in upgrades)
80+
81+
The kit loads that module **in-process** from its own `vite.config.ts` and **prepends** the returned plugins ahead of StyleX / MDX / React Router, so `enforce: 'pre'` transforms run first. Use this to swap browser demo modules, add `configureServer` hooks in `lobedocs dev`, or extend `optimizeDeps` / `ssr`.
82+
83+
The public helper and types ship from `@lobehub/docs-kit/vite-extension` (same publish pattern as `@lobehub/docs-kit/react-router-config`). Deep imports under `src/` are not a stable API.
84+
85+
```ts
86+
// docs.config.ts — optional when the convention file name is used
87+
export default {
88+
// …
89+
vite: './docs.vite.ts',
90+
};
91+
```
92+
93+
```ts
94+
// docs.vite.ts
95+
import type { Plugin } from 'vite';
96+
97+
import { defineDocsVite } from '@lobehub/docs-kit/vite-extension';
98+
99+
const demoBrowserPlugin = (): Plugin => ({
100+
name: 'demo-browser-modules',
101+
enforce: 'pre',
102+
transform(code, id, options) {
103+
// Client-only: skip SSR / prerender so browser-only deps never enter the server bundle.
104+
if (options?.ssr) return null;
105+
if (!id.includes('/demos/') || !id.endsWith('.tsx')) return null;
106+
// Replace the browser module; docs-kit still reads the on-disk source for code display.
107+
return null;
108+
},
109+
configureServer() {
110+
// Dev-only middleware / WebSocket — Vite does not run this during `lobedocs build`.
111+
},
112+
});
113+
114+
export default defineDocsVite({
115+
// Omit `apply` so transforms also run in the client build. Use `apply: 'serve'` only for
116+
// hooks that must not exist in build/prerender at all (for example live WebSockets).
117+
// Do not use `apply: 'serve'` for demo transforms that the static site still needs.
118+
optimizeDeps: {
119+
// List browser-only packages your plugin introduces, when needed.
120+
},
121+
ssr: {
122+
// Keep those same packages external to the SSR bundle when needed.
123+
},
124+
plugins: [demoBrowserPlugin()],
125+
});
126+
```
127+
128+
A default export may also be a single plugin, a plugin array, or a factory `(env) => …` that receives Vite's `ConfigEnv` (`command`, `mode`, `isSsrBuild`, …). Zero-arg factories (common in `.cjs` hooks) still work.
129+
130+
docs-kit only provides this extension entry and the load/order/`apply` semantics above. It does **not** ship or end-to-end verify any particular Ink / xterm consumer plugin — that remains in the consuming repo.
131+
132+
Restart `lobedocs dev` after changing the module path in `docs.config.ts`; Vite still hot-reloads the plugin module once loaded.
133+
73134
## themeConfig
74135

75136
| Field | Purpose |

‎src/config.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,15 @@ export interface DocsConfig {
118118
stylex?: Record<string, unknown> | string;
119119
themeConfig?: DocsThemeConfig;
120120
title: string;
121+
/**
122+
* Module path (relative to the repository root) that default-exports consumer
123+
* Vite plugins or a `{ plugins, optimizeDeps, ssr, apply }` extension object.
124+
* Loaded in-process by the kit `vite.config.ts` (not via the JSON config
125+
* subprocess), so plugin hooks stay intact. When omitted, the kit also looks
126+
* for convention files `docs.vite.{ts,mts,mjs,js,cjs}` then `docs.kit-plugins.cjs`.
127+
* See `src/viteExtension.ts` and the docs.config reference.
128+
*/
129+
vite?: string;
121130
}
122131

123132
export type ClientSiteConfig = Pick<

0 commit comments

Comments
 (0)