|
1 | | -import type { Options as VuePluginOptions } from '@vitejs/plugin-vue' |
2 | 1 | import _debug from 'debug' |
3 | | -import fg from 'fast-glob' |
4 | 2 | import fs from 'fs-extra' |
5 | 3 | import path from 'path' |
6 | 4 | import c from 'picocolors' |
7 | 5 | import { |
8 | 6 | createLogger, |
9 | 7 | loadConfigFromFile, |
10 | 8 | mergeConfig as mergeViteConfig, |
11 | | - normalizePath, |
12 | | - type Logger, |
13 | | - type UserConfig as ViteConfig |
| 9 | + normalizePath |
14 | 10 | } from 'vite' |
15 | 11 | import { DEFAULT_THEME_PATH } from './alias' |
16 | | -import type { MarkdownOptions } from './markdown/markdown' |
17 | | -import { |
18 | | - dynamicRouteRE, |
19 | | - resolveDynamicRoutes, |
20 | | - type ResolvedRouteConfig |
21 | | -} from './plugins/dynamicRoutesPlugin' |
22 | | -import { resolveRewrites } from './plugins/rewritesPlugin' |
| 12 | +import { resolvePages } from './plugins/dynamicRoutesPlugin' |
23 | 13 | import { |
24 | 14 | APPEARANCE_KEY, |
25 | | - type Awaitable, |
26 | 15 | type DefaultTheme, |
27 | 16 | type HeadConfig, |
28 | | - type LocaleConfig, |
29 | | - type LocaleSpecificConfig, |
30 | | - type PageData, |
31 | | - type SiteData, |
32 | | - type SSGContext |
| 17 | + type SiteData |
33 | 18 | } from './shared' |
| 19 | +import { |
| 20 | + type UserConfig, |
| 21 | + type RawConfigExports, |
| 22 | + type SiteConfig |
| 23 | +} from './siteConfig' |
34 | 24 |
|
35 | | -const debug = _debug('vitepress:config') |
36 | | - |
37 | | -export interface UserConfig<ThemeConfig = any> |
38 | | - extends LocaleSpecificConfig<ThemeConfig> { |
39 | | - extends?: RawConfigExports<ThemeConfig> |
40 | | - |
41 | | - base?: string |
42 | | - srcDir?: string |
43 | | - srcExclude?: string[] |
44 | | - outDir?: string |
45 | | - cacheDir?: string |
46 | | - |
47 | | - shouldPreload?: (link: string, page: string) => boolean |
48 | | - |
49 | | - locales?: LocaleConfig<ThemeConfig> |
50 | | - |
51 | | - appearance?: boolean | 'dark' |
52 | | - lastUpdated?: boolean |
53 | | - |
54 | | - /** |
55 | | - * MarkdownIt options |
56 | | - */ |
57 | | - markdown?: MarkdownOptions |
58 | | - /** |
59 | | - * Options to pass on to `@vitejs/plugin-vue` |
60 | | - */ |
61 | | - vue?: VuePluginOptions |
62 | | - /** |
63 | | - * Vite config |
64 | | - */ |
65 | | - vite?: ViteConfig |
66 | | - |
67 | | - /** |
68 | | - * Configure the scroll offset when the theme has a sticky header. |
69 | | - * Can be a number or a selector element to get the offset from. |
70 | | - * Can also be an array of selectors in case some elements will be |
71 | | - * invisible due to responsive layout. VitePress will fallback to the next |
72 | | - * selector if a selector fails to match, or the matched element is not |
73 | | - * currently visible in viewport. |
74 | | - */ |
75 | | - scrollOffset?: number | string | string[] |
76 | | - |
77 | | - /** |
78 | | - * Enable MPA / zero-JS mode. |
79 | | - * @experimental |
80 | | - */ |
81 | | - mpa?: boolean |
82 | | - |
83 | | - /** |
84 | | - * Don't fail builds due to dead links. |
85 | | - * |
86 | | - * @default false |
87 | | - */ |
88 | | - ignoreDeadLinks?: |
89 | | - | boolean |
90 | | - | 'localhostLinks' |
91 | | - | (string | RegExp | ((link: string) => boolean))[] |
92 | | - |
93 | | - /** |
94 | | - * Don't force `.html` on URLs. |
95 | | - * |
96 | | - * @default false |
97 | | - */ |
98 | | - cleanUrls?: boolean |
99 | | - |
100 | | - /** |
101 | | - * Use web fonts instead of emitting font files to dist. |
102 | | - * The used theme should import a file named `fonts.(s)css` for this to work. |
103 | | - * If you are a theme author, to support this, place your web font import |
104 | | - * between `webfont-marker-begin` and `webfont-marker-end` comments. |
105 | | - * |
106 | | - * @default true in webcontainers, else false |
107 | | - */ |
108 | | - useWebFonts?: boolean |
109 | | - |
110 | | - /** |
111 | | - * @experimental |
112 | | - * |
113 | | - * source -> destination |
114 | | - */ |
115 | | - rewrites?: Record<string, string> |
116 | | - |
117 | | - /** |
118 | | - * Build end hook: called when SSG finish. |
119 | | - * @param siteConfig The resolved configuration. |
120 | | - */ |
121 | | - buildEnd?: (siteConfig: SiteConfig) => Awaitable<void> |
122 | | - |
123 | | - /** |
124 | | - * Render end hook: called when SSR rendering is done. |
125 | | - */ |
126 | | - postRender?: (context: SSGContext) => Awaitable<SSGContext | void> |
127 | | - |
128 | | - /** |
129 | | - * Head transform hook: runs before writing HTML to dist. |
130 | | - * |
131 | | - * This build hook will allow you to modify the head adding new entries that cannot be statically added. |
132 | | - */ |
133 | | - transformHead?: (context: TransformContext) => Awaitable<HeadConfig[] | void> |
134 | | - |
135 | | - /** |
136 | | - * HTML transform hook: runs before writing HTML to dist. |
137 | | - */ |
138 | | - transformHtml?: ( |
139 | | - code: string, |
140 | | - id: string, |
141 | | - ctx: TransformContext |
142 | | - ) => Awaitable<string | void> |
143 | | - |
144 | | - /** |
145 | | - * PageData transform hook: runs when rendering markdown to vue |
146 | | - */ |
147 | | - transformPageData?: ( |
148 | | - pageData: PageData, |
149 | | - ctx: TransformPageContext |
150 | | - ) => Awaitable<Partial<PageData> | { [key: string]: any } | void> |
151 | | -} |
152 | | - |
153 | | -export interface TransformPageContext { |
154 | | - siteConfig: SiteConfig |
155 | | -} |
156 | | - |
157 | | -export interface TransformContext { |
158 | | - page: string |
159 | | - siteConfig: SiteConfig |
160 | | - siteData: SiteData |
161 | | - pageData: PageData |
162 | | - title: string |
163 | | - description: string |
164 | | - head: HeadConfig[] |
165 | | - content: string |
166 | | - assets: string[] |
167 | | -} |
168 | | - |
169 | | -export type RawConfigExports<ThemeConfig = any> = |
170 | | - | Awaitable<UserConfig<ThemeConfig>> |
171 | | - | (() => Awaitable<UserConfig<ThemeConfig>>) |
| 25 | +export * from './siteConfig' |
| 26 | +export { resolvePages } from './plugins/dynamicRoutesPlugin' |
172 | 27 |
|
173 | | -export interface SiteConfig<ThemeConfig = any> |
174 | | - extends Pick< |
175 | | - UserConfig, |
176 | | - | 'markdown' |
177 | | - | 'vue' |
178 | | - | 'vite' |
179 | | - | 'shouldPreload' |
180 | | - | 'mpa' |
181 | | - | 'lastUpdated' |
182 | | - | 'ignoreDeadLinks' |
183 | | - | 'cleanUrls' |
184 | | - | 'useWebFonts' |
185 | | - | 'postRender' |
186 | | - | 'buildEnd' |
187 | | - | 'transformHead' |
188 | | - | 'transformHtml' |
189 | | - | 'transformPageData' |
190 | | - > { |
191 | | - root: string |
192 | | - srcDir: string |
193 | | - site: SiteData<ThemeConfig> |
194 | | - configPath: string | undefined |
195 | | - configDeps: string[] |
196 | | - themeDir: string |
197 | | - outDir: string |
198 | | - cacheDir: string |
199 | | - tempDir: string |
200 | | - pages: string[] |
201 | | - dynamicRoutes: { |
202 | | - routes: ResolvedRouteConfig[] |
203 | | - fileToModulesMap: Record<string, Set<string>> |
204 | | - } |
205 | | - rewrites: { |
206 | | - map: Record<string, string | undefined> |
207 | | - inv: Record<string, string | undefined> |
208 | | - } |
209 | | - logger: Logger |
210 | | - userConfig: UserConfig |
211 | | -} |
| 28 | +const debug = _debug('vitepress:config') |
212 | 29 |
|
213 | 30 | const resolve = (root: string, file: string) => |
214 | 31 | normalizePath(path.resolve(root, `.vitepress`, file)) |
@@ -438,33 +255,3 @@ function resolveSiteDataHead(userConfig?: UserConfig): HeadConfig[] { |
438 | 255 |
|
439 | 256 | return head |
440 | 257 | } |
441 | | - |
442 | | -export async function resolvePages(srcDir: string, userConfig: UserConfig) { |
443 | | - // Important: fast-glob doesn't guarantee order of the returned files. |
444 | | - // We must sort the pages so the input list to rollup is stable across |
445 | | - // builds - otherwise different input order could result in different exports |
446 | | - // order in shared chunks which in turns invalidates the hash of every chunk! |
447 | | - // JavaScript built-in sort() is mandated to be stable as of ES2019 and |
448 | | - // supported in Node 12+, which is required by Vite. |
449 | | - const allMarkdownFiles = ( |
450 | | - await fg(['**.md'], { |
451 | | - cwd: srcDir, |
452 | | - ignore: ['**/node_modules', ...(userConfig.srcExclude || [])] |
453 | | - }) |
454 | | - ).sort() |
455 | | - |
456 | | - const pages = allMarkdownFiles.filter((p) => !dynamicRouteRE.test(p)) |
457 | | - const dynamicRouteFiles = allMarkdownFiles.filter((p) => |
458 | | - dynamicRouteRE.test(p) |
459 | | - ) |
460 | | - const dynamicRoutes = await resolveDynamicRoutes(srcDir, dynamicRouteFiles) |
461 | | - pages.push(...dynamicRoutes.routes.map((r) => r.path)) |
462 | | - |
463 | | - const rewrites = resolveRewrites(pages, userConfig.rewrites) |
464 | | - |
465 | | - return { |
466 | | - pages, |
467 | | - dynamicRoutes, |
468 | | - rewrites |
469 | | - } |
470 | | -} |
0 commit comments