UNPKG

one

Version:

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

57 lines (56 loc) 2.63 kB
let lastVersion = 0, context; function useViteRoutes(routes, routerRoot, options, version) { return version && version > lastVersion && (context = null, lastVersion = version), context || loadRoutes(routes, routerRoot, options), context; } function loadRoutes(paths, routerRoot, options) { return context || (globalThis.__importMetaGlobbed = paths, context = globbedRoutesToRouteContext(paths, routerRoot, options), context); } function globbedRoutesToRouteContext(paths, routerRoot, options) { const routesSync = {}, promises = {}, loadedRoutes = {}, clears = {}; Object.keys(paths).map((path) => { if (!paths[path]) { console.error(`Error: Missing route at path ${path}`); return; } const loadRouteFunction = paths[path], pathWithoutRelative = path.replace(`/${routerRoot}/`, "./"), originalPath = pathWithoutRelative.slice(1).replace(/\.[jt]sx?$/, ""); options?.routeModes?.[originalPath] === "spa" ? (console.info(`Spa mode: ${originalPath}`), loadedRoutes[pathWithoutRelative] = () => null) : routesSync[pathWithoutRelative] = loadRouteFunction; }); const moduleKeys = Object.keys(routesSync); function resolve(id) { if (clearTimeout(clears[id]), loadedRoutes[id]) return loadedRoutes[id]; if (typeof routesSync[id] != "function") return routesSync[id]; throw promises[id] || (promises[id] = routesSync[id]().then((val) => (loadedRoutes[id] = val, delete promises[id], clears[id] = setTimeout(() => { delete loadedRoutes[id]; }, 500), val)).catch((err) => { console.error("Error loading route", id, err, new Error().stack), loadedRoutes[id] = { default: () => null // <View // style={{ // position: 'absolute', // top: 0, // left: 0, // right: 0, // bottom: 0, // alignItems: 'center', // justifyContent: 'center', // backgroundColor: '#000', // gap: 20, // }} // > // <Text style={{ fontSize: 24, color: '#fff' }}>Error loading route</Text> // <Text style={{ fontSize: 16, color: '#fff' }}>{id}</Text> // <Text style={{ fontSize: 18, color: '#fff', maxWidth: 800 }}>{`${err}`}</Text> // </View> }, delete promises[id]; }), process.env.NODE_ENV === "development" && (promises[id].stack = new Error().stack)), promises[id]; } return resolve.keys = () => moduleKeys, resolve.id = "", resolve.resolve = (id) => id, resolve; } export { globbedRoutesToRouteContext, loadRoutes, useViteRoutes }; //# sourceMappingURL=useViteRoutes.js.map