UNPKG

one

Version:

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

290 lines 10.5 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; 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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod)); var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var getStateFromPath_mods_exports = {}; __export(getStateFromPath_mods_exports, { appendIsInitial: () => appendIsInitial, createConfigItemAdditionalProperties: () => createConfigItemAdditionalProperties, decodeURIComponentSafe: () => decodeURIComponentSafe, formatRegexPattern: () => formatRegexPattern, getRouteConfigSorter: () => getRouteConfigSorter, getUrlWithReactNavigationConcessions: () => getUrlWithReactNavigationConcessions, matchForEmptyPath: () => matchForEmptyPath, parseQueryParamsExtended: () => parseQueryParamsExtended, populateParams: () => populateParams, stripBaseUrl: () => stripBaseUrl }); module.exports = __toCommonJS(getStateFromPath_mods_exports); var import_escape_string_regexp = __toESM(require("escape-string-regexp"), 1); var import_matchers = require("../router/matchers.cjs"); function getUrlWithReactNavigationConcessions(path, baseUrl = process.env.EXPO_BASE_URL) { const pathWithoutGroups = (0, import_matchers.stripGroupSegmentsFromPath)(stripBaseUrl(path, baseUrl)); let pathname = ""; let hash = ""; try { const hashIndex = path.indexOf("#"); const queryIndex = path.indexOf("?"); if (hashIndex >= 0) { hash = path.slice(hashIndex); } let end = path.length; if (queryIndex >= 0) end = Math.min(end, queryIndex); if (hashIndex >= 0) end = Math.min(end, hashIndex); pathname = path.slice(0, end); if (pathname.startsWith("http://") || pathname.startsWith("https://")) { const originEnd = pathname.indexOf("/", pathname.indexOf("//") + 2); if (originEnd >= 0) { pathname = pathname.slice(originEnd); } } } catch {} const withoutBaseUrl = stripBaseUrl(pathname, baseUrl); return { path, // Make sure there is a trailing slash // The slashes are at the end, not the beginning nonstandardPathname: withoutBaseUrl.replace(/^\/+/g, "").replace(/\/+$/g, "") + "/", hash, pathWithoutGroups }; } function matchForEmptyPath(configs) { const leafNodes = configs.filter(config => !config.hasChildren).map(value => { return { ...value, // Collapse all levels of group segments before testing. // This enables `app/(one)/(two)/index.js` to be matched. path: (0, import_matchers.stripGroupSegmentsFromPath)(value.path) }; }); const match = leafNodes.find(config => // NOTE: Test leaf node index routes that either don't have a regex or match an empty string. config.path === "" && (!config.regex || config.regex.test(""))) ?? leafNodes.find(config => // NOTE: Test leaf node dynamic routes that match an empty string. config.path.startsWith(":") && config.regex.test("")) ?? // NOTE: Test leaf node deep dynamic routes that match a slash. // This should be done last to enable dynamic routes having a higher priority. leafNodes.find(config => config.path.startsWith("*") && config.regex.test("/")); return match; } function appendIsInitial(initialRoutes) { const resolvedInitialPatterns = initialRoutes.map(route => joinPaths(...route.parentScreens, route.initialRouteName)); return config => { config.isInitial = resolvedInitialPatterns.includes(config.routeNames.join("/")); return config; }; } const joinPaths = (...paths) => [].concat(...paths.map(p => p.split("/"))).filter(Boolean).join("/"); function getRouteConfigSorter(previousSegments = []) { return function sortConfigs(a, b) { if (a.pattern === b.pattern) { return b.routeNames.join(">").localeCompare(a.routeNames.join(">")); } if (a.pattern.startsWith(b.pattern) && !b.isIndex) { return -1; } if (b.pattern.startsWith(a.pattern) && !a.isIndex) { return 1; } if (a.type === "static" && b.type !== "static") { return -1; } if (a.type !== "static" && b.type === "static") { return 1; } if (a.staticPartCount !== b.staticPartCount) { return b.staticPartCount - a.staticPartCount; } const similarToPreviousA = previousSegments.filter((value, index) => { return value === a.expandedRouteNames[index] && value.startsWith("(") && value.endsWith(")"); }); const similarToPreviousB = previousSegments.filter((value, index) => { return value === b.expandedRouteNames[index] && value.startsWith("(") && value.endsWith(")"); }); if ((similarToPreviousA.length > 0 || similarToPreviousB.length > 0) && similarToPreviousA.length !== similarToPreviousB.length) { return similarToPreviousB.length - similarToPreviousA.length; } for (let i = 0; i < Math.max(a.parts.length, b.parts.length); i++) { if (a.parts[i] == null) { return 1; } if (b.parts[i] == null) { return -1; } const aWildCard = a.parts[i].startsWith("*"); const bWildCard = b.parts[i].startsWith("*"); if (aWildCard && bWildCard) { const aNotFound = a.parts[i].match(/^[*]not-found$/); const bNotFound = b.parts[i].match(/^[*]not-found$/); if (aNotFound && bNotFound) { continue; } if (aNotFound) { return 1; } if (bNotFound) { return -1; } continue; } if (aWildCard) { return 1; } if (bWildCard) { return -1; } const aSlug = a.parts[i].startsWith(":"); const bSlug = b.parts[i].startsWith(":"); if (aSlug && bSlug) { const aNotFound = a.parts[i].match(/^[*]not-found$/); const bNotFound = b.parts[i].match(/^[*]not-found$/); if (aNotFound && bNotFound) { continue; } if (aNotFound) { return 1; } if (bNotFound) { return -1; } continue; } if (aSlug) { return 1; } if (bSlug) { return -1; } } if (a.isInitial && !b.isInitial) { return -1; } if (!a.isInitial && b.isInitial) { return 1; } return b.parts.length - a.parts.length; }; } function formatRegexPattern(it) { it = it.replace(/ /g, "%20"); if (it.startsWith(":")) { return `(((?!\\+not-found(?:/|$))[^/]+\\/)${it.endsWith("?") ? "?" : ""})`; } if (it.startsWith("*")) { return `((.*\\/)${it.endsWith("?") ? "?" : ""})`; } if ((0, import_matchers.matchGroupName)(it) != null) { return `(?:${escape(it)}\\/)?`; } return (0, import_escape_string_regexp.default)(it) + `\\/`; } function decodeURIComponentSafe(str) { try { return decodeURIComponent(str); } catch { return str; } } function populateParams(routes, params) { if (!routes || !params || Object.keys(params).length === 0) return; for (const route of routes) { Object.assign(route, { params }); } return routes; } function createConfigItemAdditionalProperties(screen, pattern, routeNames, config = {}) { const parts = []; let isDynamic = false; let staticPartCount = 0; const isIndex = screen === "index" || screen.endsWith("/index"); for (const part of pattern.split("/")) { if (part) { const isDynamicPart = part.startsWith(":") || part.startsWith("*") || part.includes("*not-found"); isDynamic ||= isDynamicPart; if (!(0, import_matchers.matchGroupName)(part)) { parts.push(part); if (!isDynamicPart) { staticPartCount++; } } } } const hasChildren = config.screens ? !!Object.keys(config.screens)?.length : false; const type = hasChildren ? "layout" : isDynamic ? "dynamic" : "static"; if (isIndex) { parts.push("index"); staticPartCount++; } return { type, isIndex, hasChildren, parts, staticPartCount, userReadableName: [...routeNames.slice(0, -1), config.path || screen].join("/"), expandedRouteNames: routeNames.flatMap(name => { return name.split("/"); }) }; } function parseQueryParamsExtended(path, route, parseConfig, hash) { const queryIndex = path.indexOf("?"); const hashIndex = path.indexOf("#"); const queryEnd = hashIndex >= 0 && hashIndex > queryIndex ? hashIndex : void 0; const searchParams = new URLSearchParams(queryIndex >= 0 ? path.slice(queryIndex + 1, queryEnd) : ""); const params = /* @__PURE__ */Object.create(null); if (hash) { params["#"] = hash.slice(1); } for (const name of searchParams.keys()) { if (route.params?.[name]) { if (process.env.NODE_ENV !== "production") { console.warn(`Route '/${route.name}' with param '${name}' was specified both in the path and as a param, removing from path`); } } else { const values = parseConfig?.hasOwnProperty(name) ? searchParams.getAll(name).map(value => parseConfig[name](value)) : searchParams.getAll(name); params[name] = values.length === 1 ? values[0] : values; } } return Object.keys(params).length ? params : void 0; } function stripBaseUrl(path, baseUrl = process.env.EXPO_BASE_URL) { if (process.env.NODE_ENV !== "development") { if (baseUrl) { return path.replace(/^\/+/g, "/").replace(new RegExp(`^\\/?${escape(baseUrl)}`, "g"), ""); } } return path; }