UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

218 lines (215 loc) 7.77 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var getTypedRoutesDeclarationFile_exports = {}; __export(getTypedRoutesDeclarationFile_exports, { getTypedRoutesDeclarationFile: () => getTypedRoutesDeclarationFile }); module.exports = __toCommonJS(getTypedRoutesDeclarationFile_exports); var import_getRoutes = require("../router/getRoutes.native.js"); var import_matchers = require("../router/matchers.native.js"); var CATCH_ALL = /\[\.\.\..+?\]/g; var SLUG = /\[.+?\]/g; function getTypedRoutesDeclarationFile(ctx) { var staticRoutes = /* @__PURE__ */new Set(); var dynamicRoutes = /* @__PURE__ */new Set(); var dynamicRouteContextKeys = /* @__PURE__ */new Set(); walkRouteNode((0, import_getRoutes.getRoutes)(ctx, { platformRoutes: false, // We don't need to generate platform specific routes ignoreEntryPoints: true, ignoreRequireErrors: true }), // importMode: 'async', "", staticRoutes, dynamicRoutes, dynamicRouteContextKeys); var hasRoutes = dynamicRouteContextKeys.size > 0; return `// deno-lint-ignore-file /* eslint-disable */ // biome-ignore: needed import import type { OneRouter } from 'one' declare module 'one' { export namespace OneRouter { export interface __routes<T extends string = string> extends Record<string, unknown> { StaticRoutes: ${setToUnionType(staticRoutes)} DynamicRoutes: ${setToUnionType(dynamicRoutes)} DynamicRouteTemplate: ${setToUnionType(dynamicRouteContextKeys)} IsTyped: true ${hasRoutes ? `RouteTypes: ${generateRouteTypesMap(dynamicRouteContextKeys)}` : ""} } } } ${hasRoutes ? ` /** * Helper type for route information */ type RouteInfo<Params = Record<string, never>> = { Params: Params LoaderProps: { path: string; search?: string; subdomain?: string; params: Params; request?: Request } }` : ""} `.trim(); } function generateRouteTypesMap(dynamicRouteContextKeys) { if (dynamicRouteContextKeys.size === 0) { return "{}"; } var routes = [...dynamicRouteContextKeys].sort(); var entries = routes.map(function (routePath) { var params = extractParams(routePath); var paramsType = params.length === 0 ? "{}" : generateInlineParamsType(params); return ` '${routePath}': RouteInfo<${paramsType}>`; }).join("\n"); return `{ ${entries} }`; } function extractParams(routePath) { var params = []; var paramRegex = /\[(\.\.\.)?([\w]+)\]/g; var match; while ((match = paramRegex.exec(routePath)) !== null) { params.push({ name: match[2], isCatchAll: match[1] === "..." }); } return params; } function generateInlineParamsType(params) { var entries = params.map(function (p) { var type = p.isCatchAll ? "string[]" : "string"; return `${p.name}: ${type}`; }); return `{ ${entries.join("; ")} }`; } function walkRouteNode(routeNode, parentRoutePath, staticRoutes, dynamicRoutes, dynamicRouteContextKeys) { if (!routeNode) return; addRouteNode(routeNode, parentRoutePath, staticRoutes, dynamicRoutes, dynamicRouteContextKeys); parentRoutePath = `${(0, import_matchers.removeSupportedExtensions)(`${parentRoutePath}/${routeNode.route}`).replace(/\/?index$/, "")}`; var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0; try { for (var _iterator = routeNode.children[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { var child = _step.value; walkRouteNode(child, parentRoutePath, staticRoutes, dynamicRoutes, dynamicRouteContextKeys); } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator.return != null) { _iterator.return(); } } finally { if (_didIteratorError) { throw _iteratorError; } } } } function addRouteNode(routeNode, parentRoutePath, staticRoutes, dynamicRoutes, dynamicRouteContextKeys) { if (!(routeNode === null || routeNode === void 0 ? void 0 : routeNode.route)) return; if (!(0, import_matchers.isTypedRoute)(routeNode.route)) return; var routePath = `${parentRoutePath}/${(0, import_matchers.removeSupportedExtensions)(routeNode.route).replace(/\/?index$/, "")}`; if (!routePath.startsWith("/")) { routePath = `/${routePath}`; } if (routeNode.dynamic) { var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0; try { for (var _iterator = generateCombinations(routePath)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { var path = _step.value; dynamicRouteContextKeys.add(path); dynamicRoutes.add( // biome-ignore lint/suspicious/noTemplateCurlyInString: intentionally generating type string `${path.replaceAll(CATCH_ALL, "${string}").replaceAll(SLUG, "${OneRouter.SingleRoutePart<T>}")}`); } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator.return != null) { _iterator.return(); } } finally { if (_didIteratorError) { throw _iteratorError; } } } } else { var _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = void 0; try { for (var _iterator1 = generateCombinations(routePath)[Symbol.iterator](), _step1; !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true) { var combination = _step1.value; staticRoutes.add(combination); } } catch (err) { _didIteratorError1 = true; _iteratorError1 = err; } finally { try { if (!_iteratorNormalCompletion1 && _iterator1.return != null) { _iterator1.return(); } } finally { if (_didIteratorError1) { throw _iteratorError1; } } } } } var setToUnionType = function (set) { if (set.size === 0) return "never"; var sorted = [...set].sort(); if (sorted.length === 1) return `\`${sorted[0]}\``; return "\n | " + sorted.map(function (s) { return `\`${s}\``; }).join("\n | "); }; function generateCombinations(pathname) { var groups = pathname.split("/").filter(function (part) { return part.startsWith("(") && part.endsWith(")"); }); var combinations = []; function generate(currentIndex, currentPath) { if (currentIndex === groups.length) { combinations.push(currentPath.replace(/\/{2,}/g, "/")); return; } var group = groups[currentIndex]; var withoutGroup = currentPath.replace(`/${group}`, ""); generate(currentIndex + 1, withoutGroup); generate(currentIndex + 1, currentPath); } generate(0, pathname); return combinations; } //# sourceMappingURL=getTypedRoutesDeclarationFile.native.js.map