版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

427 строки
15 KiB

  1. import { RouteRecordRaw, RouteMeta } from 'vue-router';
  2. interface CustomRouteBlock extends Partial<Omit<RouteRecordRaw, 'components' | 'component' | 'children' | 'beforeEnter' | 'name'>> {
  3. name?: string;
  4. }
  5. declare const enum TreeNodeType {
  6. static = 0,
  7. param = 1
  8. }
  9. interface RouteRecordOverride extends Partial<Pick<RouteRecordRaw, 'meta' | 'props' | 'alias' | 'path'>> {
  10. name?: string;
  11. }
  12. type SubSegment = string | TreeRouteParam;
  13. declare class _TreeNodeValueBase {
  14. /**
  15. * flag based on the type of the segment
  16. */
  17. _type: TreeNodeType;
  18. /**
  19. * segment as defined by the file structure e.g. keeps the `index` name
  20. */
  21. rawSegment: string;
  22. /**
  23. * transformed version of the segment into a vue-router path. e.g. `'index'` becomes `''` and `[param]` becomes
  24. * `:param`
  25. */
  26. pathSegment: string;
  27. /**
  28. * Array of sub segments. This is usually one single elements but can have more for paths like `prefix-[param]-end.vue`
  29. */
  30. subSegments: SubSegment[];
  31. /**
  32. * fullPath of the node based on parent nodes
  33. */
  34. path: string;
  35. /**
  36. * Overrides defined by each file. The map is necessary to handle named views.
  37. */
  38. private _overrides;
  39. /**
  40. * Should we add the loader guard to the route record.
  41. */
  42. includeLoaderGuard: boolean;
  43. /**
  44. * View name (Vue Router feature) mapped to their corresponding file. By default, the view name is `default` unless
  45. * specified with a `@` e.g. `index@aux.vue` will have a view name of `aux`.
  46. */
  47. components: Map<string, string>;
  48. constructor(rawSegment: string, parent: TreeNodeValue | undefined, pathSegment?: string, subSegments?: SubSegment[]);
  49. toString(): string;
  50. isParam(): this is TreeNodeValueParam;
  51. isStatic(): this is TreeNodeValueStatic;
  52. get overrides(): RouteRecordOverride;
  53. setOverride(path: string, routeBlock: CustomRouteBlock | undefined): void;
  54. /**
  55. * Remove all overrides for a given key.
  56. *
  57. * @param key - key to remove from the override
  58. */
  59. removeOverride(key: keyof CustomRouteBlock): void;
  60. mergeOverride(path: string, routeBlock: CustomRouteBlock): void;
  61. addEditOverride(routeBlock: CustomRouteBlock): void;
  62. setEditOverride<K extends keyof RouteRecordOverride>(key: K, value: RouteRecordOverride[K]): void;
  63. }
  64. declare class TreeNodeValueStatic extends _TreeNodeValueBase {
  65. _type: TreeNodeType.static;
  66. constructor(rawSegment: string, parent: TreeNodeValue | undefined, pathSegment?: string);
  67. }
  68. interface TreeRouteParam {
  69. paramName: string;
  70. modifier: string;
  71. optional: boolean;
  72. repeatable: boolean;
  73. isSplat: boolean;
  74. }
  75. declare class TreeNodeValueParam extends _TreeNodeValueBase {
  76. params: TreeRouteParam[];
  77. _type: TreeNodeType.param;
  78. constructor(rawSegment: string, parent: TreeNodeValue | undefined, params: TreeRouteParam[], pathSegment: string, subSegments: SubSegment[]);
  79. }
  80. type TreeNodeValue = TreeNodeValueStatic | TreeNodeValueParam;
  81. declare function createTreeNodeValue(segment: string, parent?: TreeNodeValue): TreeNodeValue;
  82. declare class TreeNode {
  83. /**
  84. * value of the node
  85. */
  86. value: TreeNodeValue;
  87. /**
  88. * children of the node
  89. */
  90. children: Map<string, TreeNode>;
  91. /**
  92. * Parent node.
  93. */
  94. parent?: TreeNode;
  95. /**
  96. * Plugin options taken into account by the tree.
  97. */
  98. options: ResolvedOptions;
  99. /**
  100. * Should this page import the page info
  101. */
  102. hasDefinePage: boolean;
  103. constructor(options: ResolvedOptions, filePath: string, parent?: TreeNode);
  104. /**
  105. * Adds a path to the tree. `path` cannot start with a `/`.
  106. *
  107. * @param path - path segment to insert. **It must contain the file extension** this allows to
  108. * differentiate between folders and files.
  109. * @param filePath - file path, defaults to path for convenience and testing
  110. */
  111. insert(path: string, filePath?: string): TreeNode;
  112. setCustomRouteBlock(path: string, routeBlock: CustomRouteBlock | undefined): void;
  113. getSortedChildren(): TreeNode[];
  114. /**
  115. * Delete and detach itself from the tree.
  116. */
  117. delete(): void;
  118. /**
  119. * Remove a route from the tree. The path shouldn't start with a `/` but it can be a nested one. e.g. `foo/bar.vue`.
  120. * The `path` should be relative to the page folder.
  121. *
  122. * @param path - path segment of the file
  123. */
  124. remove(path: string): void;
  125. /**
  126. * Returns the route path of the node without parent paths. If the path was overridden, it returns the override.
  127. */
  128. get path(): string;
  129. /**
  130. * Returns the route path of the node including parent paths.
  131. */
  132. get fullPath(): string;
  133. /**
  134. * Returns the route name of the node. If the name was overridden, it returns the override.
  135. */
  136. get name(): string;
  137. /**
  138. * Returns the meta property as an object.
  139. */
  140. get metaAsObject(): Readonly<RouteMeta>;
  141. /**
  142. * Returns the JSON string of the meta object of the node. If the meta was overridden, it returns the override. If
  143. * there is no override, it returns an empty string.
  144. */
  145. get meta(): string;
  146. get params(): TreeRouteParam[];
  147. /**
  148. * Returns wether this tree node is the root node of the tree.
  149. *
  150. * @returns true if the node is the root node
  151. */
  152. isRoot(): boolean;
  153. toString(): string;
  154. }
  155. /**
  156. * Creates a new prefix tree. This is meant to only be the root node. It has access to extra methods that only make
  157. * sense on the root node.
  158. */
  159. declare class PrefixTree extends TreeNode {
  160. map: Map<string, TreeNode>;
  161. constructor(options: ResolvedOptions);
  162. insert(path: string, filePath?: string): TreeNode;
  163. getChild(filePath: string): TreeNode | undefined;
  164. /**
  165. *
  166. * @param filePath -
  167. */
  168. removeChild(filePath: string): void;
  169. }
  170. declare function createPrefixTree(options: ResolvedOptions): PrefixTree;
  171. type Awaitable<T> = T | PromiseLike<T>;
  172. type LiteralStringUnion<LiteralType, BaseType extends string = string> = LiteralType | (BaseType & Record<never, never>);
  173. /**
  174. * Creates a name based of the node path segments.
  175. *
  176. * @param node - the node to get the path from
  177. * @param parent - the parent node
  178. * @returns a route name
  179. */
  180. declare function getPascalCaseRouteName(node: TreeNode): string;
  181. /**
  182. * Joins the path segments of a node into a name that corresponds to the filepath represented by the node.
  183. *
  184. * @param node - the node to get the path from
  185. * @returns a route name
  186. */
  187. declare function getFileBasedRouteName(node: TreeNode): string;
  188. /**
  189. * A route node that can be modified by the user. The tree can be iterated to be traversed.
  190. * @example
  191. * ```js
  192. * [...node] // creates an array of all the children
  193. * for (const child of node) {
  194. * // do something with the child node
  195. * }
  196. * ```
  197. *
  198. * @experimental
  199. */
  200. declare class EditableTreeNode {
  201. private node;
  202. constructor(node: TreeNode);
  203. /**
  204. * Remove and detach the current route node from the tree. Subsequently, its children will be removed as well.
  205. */
  206. delete(): void;
  207. /**
  208. * Inserts a new route as a child of this route. This route cannot use `definePage()`. If it was meant to be included,
  209. * add it to the `routesFolder` option.
  210. */
  211. insert(path: string, filePath: string): EditableTreeNode;
  212. /**
  213. * Get an editable version of the parent node if it exists.
  214. */
  215. get parent(): EditableTreeNode | undefined;
  216. /**
  217. * Return a Map of the files associated to the current route. The key of the map represents the name of the view (Vue
  218. * Router feature) while the value is the file path. By default, the name of the view is `default`.
  219. */
  220. get components(): Map<string, string>;
  221. /**
  222. * Name of the route. Note that **all routes are named** but when the final `routes` array is generated, routes
  223. * without a `component` will not include their `name` property to avoid accidentally navigating to them and display
  224. * nothing. {@see isPassThrough}
  225. */
  226. get name(): string;
  227. /**
  228. * Override the name of the route.
  229. */
  230. set name(name: string | undefined);
  231. /**
  232. * Whether the route is a pass-through route. A pass-through route is a route that does not have a component and is
  233. * used to group other routes under the same prefix `path` and/or `meta` properties.
  234. */
  235. get isPassThrough(): boolean;
  236. /**
  237. * Meta property of the route as an object. Note this property is readonly and will be serialized as JSON. It won't contain the meta properties defined with `definePage()` as it could contain expressions **but it does contain the meta properties defined with `<route>` blocks**.
  238. */
  239. get meta(): Readonly<RouteMeta>;
  240. /**
  241. * Override the meta property of the route. This will discard any other meta property defined with `<route>` blocks or
  242. * through other means.
  243. */
  244. set meta(meta: RouteMeta);
  245. /**
  246. * Add meta properties to the route keeping the existing ones. The passed object will be deeply merged with the
  247. * existing meta object if any. Note that the meta property is later on serialized as JSON so you can't pass functions
  248. * or any other non-serializable value.
  249. */
  250. addToMeta(meta: Partial<RouteMeta>): void;
  251. /**
  252. * Path of the route without parent paths.
  253. */
  254. get path(): string;
  255. /**
  256. * Override the path of the route. You must ensure `params` match with the existing path.
  257. */
  258. set path(path: string);
  259. /**
  260. * Alias of the route.
  261. */
  262. get alias(): string | string[] | undefined;
  263. /**
  264. * Add an alias to the route.
  265. *
  266. * @param alias - Alias to add to the route
  267. */
  268. addAlias(alias: CustomRouteBlock['alias']): void;
  269. /**
  270. * Array of the route params and all of its parent's params.
  271. */
  272. get params(): TreeRouteParam[];
  273. /**
  274. * Path of the route including parent paths.
  275. */
  276. get fullPath(): string;
  277. /**
  278. * DFS traversal of the tree.
  279. * @example
  280. * ```ts
  281. * for (const node of tree) {
  282. * // ...
  283. * }
  284. * ```
  285. */
  286. traverseDFS(): Generator<EditableTreeNode, void, unknown>;
  287. [Symbol.iterator](): Generator<EditableTreeNode, void, unknown>;
  288. /**
  289. * BFS traversal of the tree as a generator.
  290. *
  291. * @example
  292. * ```ts
  293. * for (const node of tree) {
  294. * // ...
  295. * }
  296. * ```
  297. */
  298. traverseBFS(): Generator<EditableTreeNode, void, unknown>;
  299. }
  300. interface RoutesFolderOption {
  301. /**
  302. * Path to the folder containing the components that should be used for routes.
  303. */
  304. src: string;
  305. /**
  306. * Prefix to add to the route path. Defaults to `''`. Must **end with a slash** and **start without one**.
  307. */
  308. path?: string;
  309. /**
  310. * Allows to override the global `filePattern` option for this folder.
  311. */
  312. filePatterns?: string | string[];
  313. /**
  314. * Allows to override the global `exclude` option for this folder.
  315. */
  316. exclude?: string[];
  317. /**
  318. * Allows to override the global `extensions` option for this folder.
  319. */
  320. extensions?: string[];
  321. }
  322. type _RoutesFolder = string | RoutesFolderOption;
  323. type RoutesFolder = _RoutesFolder[] | _RoutesFolder;
  324. interface ResolvedOptions {
  325. /**
  326. * Extensions of files to be considered as pages. Defaults to `['.vue']`. Cannot be empty.
  327. */
  328. extensions: string[];
  329. /**
  330. * Folder containing the components that should be used for routes. Can also be an array if you want to add multiple
  331. * folders, or an object if you want to define a route prefix. Supports glob patterns but must be a folder, use
  332. * `extensions` and `exclude` to filter files.
  333. *
  334. * @default "src/pages"
  335. */
  336. routesFolder: RoutesFolderOption[];
  337. /**
  338. * Array of `picomatch` globs to ignore. Defaults to `[]`. Note the globs are relative to the `routesFolder`, so avoid
  339. * writing something like `['src/pages']` as **it won't match anything**.
  340. */
  341. exclude: string[];
  342. /**
  343. * Pattern to match files in the `routesFolder`. Defaults to `**‍/*` plus a combination of all the possible extensions,
  344. * e.g. `**‍/*.{vue,md}` if `extensions` is set to `['.vue', '.md']`.
  345. * @default "**‍/*"
  346. */
  347. filePatterns: string | string[];
  348. /**
  349. * Method to generate the name of a route.
  350. */
  351. getRouteName: (node: TreeNode) => string;
  352. /**
  353. * Allows to extend a route by modifying its node, adding children, or even deleting it. This will be invoked once for
  354. * each route.
  355. *
  356. * @experimental See https://github.com/posva/unplugin-vue-router/issues/43
  357. *
  358. * @param route - {@link EditableTreeNode} of the route to extend
  359. */
  360. extendRoute?: (route: EditableTreeNode) => Awaitable<void>;
  361. /**
  362. * Allows to do some changes before writing the files. This will be invoked **every time** the files need to be written.
  363. *
  364. * @experimental See https://github.com/posva/unplugin-vue-router/issues/43
  365. *
  366. * @param rootRoute - {@link EditableTreeNode} of the root route
  367. */
  368. beforeWriteFiles?: (rootRoute: EditableTreeNode) => Awaitable<void>;
  369. /**
  370. * Enables EXPERIMENTAL data fetching. See https://github.com/posva/unplugin-vue-router/tree/main/src/data-fetching
  371. * @experimental
  372. */
  373. dataFetching: boolean;
  374. /**
  375. * Defines how page components should be imported. Defaults to dynamic imports to enable lazy loading of pages.
  376. */
  377. importMode: _OptionsImportMode;
  378. /**
  379. * Root of the project. All paths are resolved relatively to this one. Defaults to `process.cwd()`.
  380. */
  381. root: string;
  382. /**
  383. * Language for `<route>` blocks in SFC files. Defaults to `'json5'`.
  384. */
  385. routeBlockLang: 'yaml' | 'yml' | 'json5' | 'json';
  386. /**
  387. * Should generate d.ts files. Defaults to `true` if `typescript` is installed.
  388. */
  389. dts: boolean | string;
  390. /**
  391. * Allows inspection by vite-plugin-inspect by not adding the leading `\0` to the id of virtual modules.
  392. * @internal
  393. */
  394. _inspect: boolean;
  395. /**
  396. * Activates debug logs.
  397. */
  398. logs: boolean;
  399. }
  400. /**
  401. * @internal
  402. */
  403. type _OptionsImportMode = 'sync' | 'async' | ((filepath: string) => 'sync' | 'async');
  404. interface Options extends Partial<Omit<ResolvedOptions, 'routesFolder'>> {
  405. routesFolder?: RoutesFolder;
  406. }
  407. declare const DEFAULT_OPTIONS: ResolvedOptions;
  408. interface ServerContext {
  409. invalidate: (module: string) => void;
  410. reload: () => void;
  411. }
  412. /**
  413. * Normalize user options with defaults and resolved paths.
  414. *
  415. * @param options - user provided options
  416. * @returns normalized options
  417. */
  418. declare function resolveOptions(options: Options): ResolvedOptions;
  419. export { Awaitable as A, DEFAULT_OPTIONS as D, EditableTreeNode as E, LiteralStringUnion as L, Options as O, ResolvedOptions as R, ServerContext as S, TreeNode as T, _RoutesFolder as _, getPascalCaseRouteName as a, createTreeNodeValue as b, createPrefixTree as c, TreeNodeValueParam as d, TreeNodeValueStatic as e, RoutesFolderOption as f, getFileBasedRouteName as g, RoutesFolder as h, _OptionsImportMode as i, resolveOptions as r };