版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

221 lines
6.4 KiB

  1. import {
  2. __spreadProps,
  3. __spreadValues
  4. } from "./chunk-E3WVRKG7.mjs";
  5. // src/options.ts
  6. import { isPackageExists } from "local-pkg";
  7. // src/core/utils.ts
  8. import { pascalCase } from "scule";
  9. function warn(msg, type = "warn") {
  10. console[type](`\u26A0\uFE0F [unplugin-vue-router]: ${msg}`);
  11. }
  12. function logTree(tree, log) {
  13. log(printTree(tree));
  14. }
  15. var MAX_LEVEL = 1e3;
  16. function printTree(tree, level = 0, parentPre = "", treeStr = "") {
  17. if (typeof tree !== "object" || level >= MAX_LEVEL)
  18. return "";
  19. if (tree instanceof Map) {
  20. const total = tree.size;
  21. let index = 0;
  22. for (const [_key, child] of tree) {
  23. const hasNext = index++ < total - 1;
  24. const { children } = child;
  25. treeStr += `${`${parentPre}${hasNext ? "\u251C" : "\u2514"}${"\u2500" + (children.size > 0 ? "\u252C" : "")} `}${child}
  26. `;
  27. if (children) {
  28. treeStr += printTree(
  29. children,
  30. level + 1,
  31. `${parentPre}${hasNext ? "\u2502" : " "} `
  32. );
  33. }
  34. }
  35. } else {
  36. const children = tree.children;
  37. treeStr = `${tree}
  38. `;
  39. if (children) {
  40. treeStr += printTree(children, level + 1);
  41. }
  42. }
  43. return treeStr;
  44. }
  45. var isArray = Array.isArray;
  46. function trimExtension(path, extensions) {
  47. for (const extension of extensions) {
  48. const lastDot = path.endsWith(extension) ? -extension.length : 0;
  49. if (lastDot < 0) {
  50. return path.slice(0, lastDot);
  51. }
  52. }
  53. return path;
  54. }
  55. function throttle(fn, wait, initialWait) {
  56. let pendingExecutionTimeout = null;
  57. let pendingExecution = false;
  58. let executionTimeout = null;
  59. return () => {
  60. if (pendingExecutionTimeout == null) {
  61. pendingExecutionTimeout = setTimeout(() => {
  62. pendingExecutionTimeout = null;
  63. if (pendingExecution) {
  64. pendingExecution = false;
  65. fn();
  66. }
  67. }, wait);
  68. executionTimeout = setTimeout(() => {
  69. executionTimeout = null;
  70. fn();
  71. }, initialWait);
  72. } else if (executionTimeout == null) {
  73. pendingExecution = true;
  74. }
  75. };
  76. }
  77. var LEADING_SLASH_RE = /^\//;
  78. var TRAILING_SLASH_RE = /\/$/;
  79. function joinPath(...paths) {
  80. let result = "";
  81. for (const path of paths) {
  82. result = result.replace(TRAILING_SLASH_RE, "") + // check path to avoid adding a trailing slash when joining an empty string
  83. (path && "/" + path.replace(LEADING_SLASH_RE, ""));
  84. }
  85. return result;
  86. }
  87. function paramToName({ paramName, modifier, isSplat }) {
  88. return `${isSplat ? "$" : ""}${paramName.charAt(0).toUpperCase() + paramName.slice(1)}${modifier}`;
  89. }
  90. function getPascalCaseRouteName(node) {
  91. var _a;
  92. if (((_a = node.parent) == null ? void 0 : _a.isRoot()) && node.value.pathSegment === "")
  93. return "Root";
  94. let name = node.value.subSegments.map((segment) => {
  95. if (typeof segment === "string") {
  96. return pascalCase(segment);
  97. }
  98. return paramToName(segment);
  99. }).join("");
  100. if (node.value.components.size && node.children.has("index")) {
  101. name += "Parent";
  102. }
  103. const parent = node.parent;
  104. return (parent && !parent.isRoot() ? getPascalCaseRouteName(parent).replace(/Parent$/, "") : "") + name;
  105. }
  106. function getFileBasedRouteName(node) {
  107. if (!node.parent)
  108. return "";
  109. return getFileBasedRouteName(node.parent) + "/" + (node.value.rawSegment === "index" ? "" : node.value.rawSegment);
  110. }
  111. function mergeRouteRecordOverride(a, b) {
  112. var _a;
  113. const merged = {};
  114. const keys = [
  115. .../* @__PURE__ */ new Set([
  116. ...Object.keys(a),
  117. ...Object.keys(b)
  118. ])
  119. ];
  120. for (const key of keys) {
  121. if (key === "alias") {
  122. const newAlias = [];
  123. merged[key] = newAlias.concat(a.alias || [], b.alias || []);
  124. } else if (key === "meta") {
  125. merged[key] = mergeDeep(a[key] || {}, b[key] || {});
  126. } else {
  127. merged[key] = (_a = b[key]) != null ? _a : a[key];
  128. }
  129. }
  130. return merged;
  131. }
  132. function isObject(obj) {
  133. return obj && typeof obj === "object";
  134. }
  135. function mergeDeep(...objects) {
  136. return objects.reduce((prev, obj) => {
  137. Object.keys(obj).forEach((key) => {
  138. const pVal = prev[key];
  139. const oVal = obj[key];
  140. if (Array.isArray(pVal) && Array.isArray(oVal)) {
  141. prev[key] = pVal.concat(...oVal);
  142. } else if (isObject(pVal) && isObject(oVal)) {
  143. prev[key] = mergeDeep(pVal, oVal);
  144. } else {
  145. prev[key] = oVal;
  146. }
  147. });
  148. return prev;
  149. }, {});
  150. }
  151. function asRoutePath({ src, path = "" }, filePath) {
  152. return (
  153. // add the path prefix if any
  154. path + // remove the absolute path to the pages folder
  155. filePath.slice(src.length + 1)
  156. );
  157. }
  158. function appendExtensionListToPattern(filePatterns, extensions) {
  159. const extensionPattern = extensions.length === 1 ? extensions[0] : `.{${extensions.map((extension) => extension.replace(".", "")).join(",")}}`;
  160. return Array.isArray(filePatterns) ? filePatterns.map((filePattern) => `${filePattern}${extensionPattern}`) : `${filePatterns}${extensionPattern}`;
  161. }
  162. // src/options.ts
  163. import { resolve } from "pathe";
  164. var DEFAULT_OPTIONS = {
  165. extensions: [".vue"],
  166. exclude: [],
  167. routesFolder: [{ src: "src/pages" }],
  168. filePatterns: "**/*",
  169. routeBlockLang: "json5",
  170. getRouteName: getFileBasedRouteName,
  171. dataFetching: false,
  172. importMode: "async",
  173. root: process.cwd(),
  174. dts: isPackageExists("typescript"),
  175. logs: false,
  176. _inspect: false
  177. };
  178. function normalizeRoutesFolderOption(routesFolder) {
  179. return (isArray(routesFolder) ? routesFolder : [routesFolder]).map(
  180. (routeOption) => typeof routeOption === "string" ? { src: routeOption } : routeOption
  181. );
  182. }
  183. function resolveOptions(options) {
  184. const root = options.root || DEFAULT_OPTIONS.root;
  185. const routesFolder = normalizeRoutesFolderOption(
  186. options.routesFolder || DEFAULT_OPTIONS.routesFolder
  187. ).map((routeOption) => __spreadProps(__spreadValues({}, routeOption), {
  188. src: resolve(root, routeOption.src)
  189. }));
  190. if (options.extensions) {
  191. options.extensions = options.extensions.map((ext) => {
  192. if (!ext.startsWith(".")) {
  193. warn(`Invalid extension "${ext}". Extensions must start with a dot.`);
  194. return "." + ext;
  195. }
  196. return ext;
  197. }).sort((a, b) => b.length - a.length);
  198. }
  199. return __spreadProps(__spreadValues(__spreadValues({}, DEFAULT_OPTIONS), options), {
  200. routesFolder
  201. });
  202. }
  203. export {
  204. warn,
  205. logTree,
  206. trimExtension,
  207. throttle,
  208. joinPath,
  209. getPascalCaseRouteName,
  210. getFileBasedRouteName,
  211. mergeRouteRecordOverride,
  212. asRoutePath,
  213. appendExtensionListToPattern,
  214. DEFAULT_OPTIONS,
  215. resolveOptions
  216. };