one
Version:
One is a new React Framework that makes Vite serve both native and web.
266 lines (265 loc) • 12.8 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf, __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: !0 });
}, __copyProps = (to, from, except, desc) => {
if (from && typeof from == "object" || typeof from == "function")
for (let key of __getOwnPropNames(from))
!__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: !0 }) : target,
mod
)), __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
var getStateFromPath_exports = {};
__export(getStateFromPath_exports, {
getStateFromPath: () => getStateFromPath
});
module.exports = __toCommonJS(getStateFromPath_exports);
var import_escape_string_regexp = __toESM(require("escape-string-regexp"), 1), import_findFocusedRoute = require("./findFocusedRoute"), import_validatePathConfig = require("./validatePathConfig"), import_getStateFromPath_mods = require("./getStateFromPath-mods"), import_shared = require("./_shared");
function getStateFromPath(path, options) {
const { initialRoutes, configs, configWithRegexes } = getConfigResources(options), screens = options?.screens, pathData = (0, import_getStateFromPath_mods.getUrlWithReactNavigationConcessions)(path);
let remaining = pathData.nonstandardPathname.replace(/\/+/g, "/").replace(/^\//, "").replace(/\?.*$/, "");
remaining = remaining.endsWith("/") ? remaining : `${remaining}/`;
const prefix = options?.path?.replace(/^\//, "");
if (prefix) {
const normalizedPrefix = prefix.endsWith("/") ? prefix : `${prefix}/`;
if (!remaining.startsWith(normalizedPrefix))
return;
remaining = remaining.replace(normalizedPrefix, "");
}
if (screens === void 0) {
const routes2 = remaining.split("/").filter(Boolean).map((segment) => ({ name: decodeURIComponent(segment) }));
return routes2.length ? createNestedStateObject(pathData, routes2, initialRoutes, []) : void 0;
}
if (remaining === "/") {
const match = (0, import_getStateFromPath_mods.matchForEmptyPath)(configWithRegexes);
return match ? createNestedStateObject(
pathData,
// @modified: pass pathData instead of path
match.routeNames.map((name) => ({ name })),
initialRoutes,
configs
) : void 0;
}
let result, current;
const { routes, remainingPath } = matchAgainstConfigs(remaining, configWithRegexes);
if (routes !== void 0 && (current = createNestedStateObject(pathData, routes, initialRoutes, configs), remaining = remainingPath, result = current), !(current == null || result == null))
return result;
}
const cachedConfigResources = /* @__PURE__ */ new WeakMap();
function getConfigResources(options) {
if (!options) return prepareConfigResources();
const cached = cachedConfigResources.get(options);
if (cached) return cached;
const resources = prepareConfigResources(options);
return cachedConfigResources.set(options, resources), resources;
}
function prepareConfigResources(options, previousSegments) {
options && (0, import_validatePathConfig.validatePathConfig)(options);
const initialRoutes = getInitialRoutes(options), configs = getNormalizedConfigs(initialRoutes, options?.screens, previousSegments);
checkForDuplicatedConfigs(configs);
const configWithRegexes = getConfigsWithRegexes(configs);
return {
initialRoutes,
configs,
configWithRegexes
};
}
function getInitialRoutes(options) {
const initialRoutes = [];
return options?.initialRouteName && initialRoutes.push({
initialRouteName: options.initialRouteName,
parentScreens: []
}), initialRoutes;
}
function getNormalizedConfigs(initialRoutes, screens = {}, previousSegments) {
return [].concat(
...Object.keys(screens).map(
(key) => createNormalizedConfigs(key, screens, [], initialRoutes, [])
)
).map((0, import_getStateFromPath_mods.appendIsInitial)(initialRoutes)).sort((0, import_getStateFromPath_mods.getRouteConfigSorter)(previousSegments));
}
function checkForDuplicatedConfigs(configs) {
configs.reduce((acc, config) => {
if (acc[config.pattern]) {
const a = acc[config.pattern].routeNames, b = config.routeNames;
if (!(a.length > b.length ? b.every((it, i) => a[i] === it) : a.every((it, i) => b[i] === it)))
throw new Error(
`Found conflicting screens with the same pattern. The pattern '${config.pattern}' resolves to both '${a.join(" > ")}' and '${b.join(
" > "
)}'. Patterns must be unique and cannot resolve to more than one screen.`
);
}
return Object.assign(acc, {
[config.pattern]: config
});
}, {});
}
function getConfigsWithRegexes(configs) {
return configs.map((c) => ({
...c,
// Add `$` to the regex to make sure it matches till end of the path and not just beginning
// @modified - start
// regex: c.regex ? new RegExp(c.regex.source + '$') : undefined,
regex: c.pattern ? new RegExp(`^(${c.pattern.split("/").map(import_getStateFromPath_mods.formatRegexPattern).join("")})$`) : void 0
// @modified - end
}));
}
const joinPaths = (...paths) => [].concat(...paths.map((p) => p.split("/"))).filter(Boolean).join("/"), matchAgainstConfigs = (remaining, configs) => {
let routes, remainingPath = remaining;
const allParams = /* @__PURE__ */ Object.create(null);
for (const config of configs) {
if (!config.regex)
continue;
const match = remainingPath.match(config.regex);
if (match) {
const matchedParams = (config.pattern?.split("/").reduce(
(acc, p, index) => {
if (!(0, import_shared.isDynamicPart)(p))
return acc;
acc.pos += 1;
const decodedParamSegment = (0, import_getStateFromPath_mods.decodeURIComponentSafe)(
// @modified: use decodeURIComponent**Safe**
// The param segments appear every second item starting from 2 in the regex match result
match[(acc.pos + 1) * 2].replace(/\/$/, "")
);
return Object.assign(acc.matchedParams, {
[p]: Object.assign(acc.matchedParams[p] || {}, {
[index]: decodedParamSegment
})
}), acc;
},
{ pos: -1, matchedParams: {} }
)).matchedParams || {};
routes = config.routeNames.map((name) => {
const routeConfig = configs.find((c) => c.screen === name && config.pattern.startsWith(c.pattern)), normalizedPath = routeConfig?.path.split("/").filter(Boolean).join("/"), numInitialSegments = routeConfig?.pattern.replace(new RegExp(`${(0, import_escape_string_regexp.default)(normalizedPath)}$`), "")?.split("/").length, params = normalizedPath?.split("/").reduce((acc, p, index) => {
if (!(0, import_shared.isDynamicPart)(p))
return acc;
const offset = numInitialSegments ? numInitialSegments - 1 : 0, value = (0, import_shared.getParamValue)(p, matchedParams[p]?.[index + offset]);
if (value) {
const key = (0, import_shared.replacePart)(p);
acc[key] = routeConfig?.parse?.[key] ? routeConfig.parse[key](value) : value;
}
return acc;
}, {});
return params && Object.keys(params).length ? (Object.assign(allParams, params), { name, params }) : { name };
}), remainingPath = remainingPath.replace(match[1], "");
break;
}
}
return (0, import_getStateFromPath_mods.populateParams)(routes, allParams), { routes, remainingPath };
}, createNormalizedConfigs = (screen, routeConfig, routeNames = [], initials, parentScreens, parentPattern) => {
const configs = [];
routeNames.push(screen), parentScreens.push(screen);
const config = routeConfig[screen];
if (typeof config == "string") {
const pattern = parentPattern ? joinPaths(parentPattern, config) : config;
configs.push(createConfigItem(screen, routeNames, pattern, config));
} else if (typeof config == "object") {
let pattern;
if (typeof config.path == "string") {
if (config.exact && config.path === void 0)
throw new Error(
"A 'path' needs to be specified when specifying 'exact: true'. If you don't want this screen in the URL, specify it as empty string, e.g. `path: ''`."
);
pattern = config.exact !== !0 ? joinPaths(parentPattern || "", config.path || "") : config.path || "", configs.push(
createConfigItem(screen, routeNames, pattern, config.path, config.parse, config)
);
}
config.screens && (config.initialRouteName && initials.push({
initialRouteName: config.initialRouteName,
parentScreens
}), Object.keys(config.screens).forEach((nestedConfig) => {
const result = createNormalizedConfigs(
nestedConfig,
config.screens,
routeNames,
initials,
[...parentScreens],
pattern ?? parentPattern
);
configs.push(...result);
}));
}
return routeNames.pop(), configs;
}, createConfigItem = (screen, routeNames, pattern, path, parse = void 0, config = {}) => {
pattern = pattern.split("/").filter(Boolean).join("/");
const regex = pattern ? new RegExp(
`^(${pattern.split("/").map((it) => it.startsWith(":") ? `(([^/]+\\/)${it.endsWith("?") ? "?" : ""})` : `${it === "*" ? ".*" : (0, import_escape_string_regexp.default)(it)}\\/`).join("")})`
) : void 0;
return {
screen,
regex,
pattern,
path,
// The routeNames array is mutated, so copy it to keep the current state
routeNames: [...routeNames],
parse,
// @modified - start
...(0, import_getStateFromPath_mods.createConfigItemAdditionalProperties)(screen, pattern, routeNames, config)
// @modified - end
};
}, findParseConfigForRoute = (routeName, flatConfig) => {
for (const config of flatConfig)
if (routeName === config.routeNames[config.routeNames.length - 1])
return config.parse;
}, findInitialRoute = (routeName, parentScreens, initialRoutes) => {
for (const config of initialRoutes)
if (parentScreens.length === config.parentScreens.length) {
let sameParents = !0;
for (let i = 0; i < parentScreens.length; i++)
if (parentScreens[i].localeCompare(config.parentScreens[i]) !== 0) {
sameParents = !1;
break;
}
if (sameParents)
return routeName !== config.initialRouteName ? config.initialRouteName : void 0;
}
}, createStateObject = (initialRoute, route, isEmpty) => isEmpty ? initialRoute ? {
index: 1,
routes: [{ name: initialRoute }, route]
} : {
routes: [route]
} : initialRoute ? {
index: 1,
routes: [{ name: initialRoute }, { ...route, state: { routes: [] } }]
} : {
routes: [{ ...route, state: { routes: [] } }]
}, createNestedStateObject = ({ path, ...restPathData }, routes, initialRoutes, flatConfig) => {
let route = routes.shift();
const parentScreens = [];
let initialRoute = findInitialRoute(route.name, parentScreens, initialRoutes);
parentScreens.push(route.name);
const state = createStateObject(initialRoute, route, routes.length === 0);
if (routes.length > 0) {
let nestedState = state;
for (; route = routes.shift(); ) {
initialRoute = findInitialRoute(route.name, parentScreens, initialRoutes);
const nestedStateIndex = nestedState.index || nestedState.routes.length - 1;
nestedState.routes[nestedStateIndex].state = createStateObject(
initialRoute,
route,
routes.length === 0
), routes.length > 0 && (nestedState = nestedState.routes[nestedStateIndex].state), parentScreens.push(route.name);
}
}
route = (0, import_findFocusedRoute.findFocusedRoute)(state), route.path = restPathData.pathWithoutGroups;
const params = (0, import_getStateFromPath_mods.parseQueryParamsExtended)(
path,
route,
flatConfig ? findParseConfigForRoute(route.name, flatConfig) : void 0,
restPathData.url.hash
);
return params && (route.params = { ...route.params, ...params }), state;
};
//# sourceMappingURL=getStateFromPath.js.map