UNPKG

one

Version:

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

556 lines 20 kB
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 getRoutes_exports = {}; __export(getRoutes_exports, { extrapolateGroups: () => extrapolateGroups, generateDynamic: () => generateDynamic, getIgnoreList: () => getIgnoreList, getRoutes: () => getRoutes }); module.exports = __toCommonJS(getRoutes_exports); var import_config = require("../config.cjs"); var import_getPageExport = require("../utils/getPageExport.cjs"); var import_matchers = require("./matchers.cjs"); const validPlatforms = /* @__PURE__ */new Set(["android", "ios", "native", "web"]); function getRoutes(contextModule, options = {}) { const directoryTree = getDirectoryTree(contextModule, options); if (!directoryTree) { return null; } const rootNode = flattenDirectoryTreeToRoutes(directoryTree, options); if (!options.ignoreEntryPoints) { crawlAndAppendInitialRoutesAndEntryFiles(rootNode, options); } return rootNode; } function getDirectoryTree(contextModule, options) { const importMode = options.importMode || process.env.One_ROUTER_IMPORT_MODE; const ignoreList = [/^\.\/\+html\.[tj]sx?$/, // Ignore the top level ./+html file /\.d\.ts$/ // Ignore TypeScript declaration files ]; if (options.ignore) { ignoreList.push(...options.ignore); } if (!options.preserveApiRoutes) { ignoreList.push(/\+api\.[tj]sx?$/); } const rootDirectory = { files: /* @__PURE__ */new Map(), subdirectories: /* @__PURE__ */new Map(), slots: /* @__PURE__ */new Map() }; let hasRoutes = false; let isValid = false; for (const filePath of contextModule.keys()) { if (ignoreList.some(regex => regex.test(filePath))) { continue; } isValid = true; let parentRenderMode; const pathParts = filePath.replace(/^\.\//, "").split("/"); const directoryParts = pathParts.slice(0, -1); for (const part of directoryParts) { const dirRenderMode = (0, import_matchers.matchDirectoryRenderMode)(part); if (dirRenderMode) { parentRenderMode = dirRenderMode.renderMode; } } const meta = getFileMeta(filePath, options, parentRenderMode); if (meta.specificity < 0) { continue; } const type = meta.isLayout ? "layout" : meta.renderMode || (0, import_config.getDefaultRenderMode)(); const layoutRenderMode = meta.isLayout && meta.renderMode && meta.renderMode !== "api" ? meta.renderMode : void 0; let node = { type, // store layout render mode if this is a layout with an explicit mode ...(layoutRenderMode && { layoutRenderMode }), loadRoute() { if (options.ignoreRequireErrors) { try { return contextModule(filePath); } catch { return {}; } } else { return contextModule(filePath); } }, contextKey: filePath, route: "", // This is overwritten during hoisting based upon the _layout dynamic: null, children: [] // While we are building the directory tree, we don't know the node's children just yet. This is added during hoisting }; if (process.env.NODE_ENV === "development") { if (node.type !== "api" && importMode === "sync") { if (!(0, import_getPageExport.getPageExport)(node.loadRoute())) { continue; } } } for (const route of extrapolateGroups(meta.route)) { const subdirectoryParts = route.split("/").slice(0, -1); let directory = rootDirectory; for (const part of subdirectoryParts) { const slotName = (0, import_matchers.matchSlotName)(part); if (slotName) { let slotDirectory = directory.slots.get(slotName); if (!slotDirectory) { slotDirectory = { files: /* @__PURE__ */new Map(), subdirectories: /* @__PURE__ */new Map(), slots: /* @__PURE__ */new Map() }; directory.slots.set(slotName, slotDirectory); } directory = slotDirectory; } else { const dirRenderMode = (0, import_matchers.matchDirectoryRenderMode)(part); const dirName = dirRenderMode?.name ?? part; let subDirectory = directory.subdirectories.get(dirName); if (!subDirectory) { subDirectory = { files: /* @__PURE__ */new Map(), subdirectories: /* @__PURE__ */new Map(), slots: /* @__PURE__ */new Map(), renderMode: dirRenderMode?.renderMode }; directory.subdirectories.set(dirName, subDirectory); } directory = subDirectory; } } node = { ...node, route, slotName: meta.slotName, intercept: meta.interceptMatch ? { levels: meta.interceptMatch.levels, targetPath: meta.interceptMatch.targetPath } : void 0 }; if (meta.isLayout) { directory.layout ??= []; const existing = directory.layout[meta.specificity]; if (existing) { if (process.env.NODE_ENV !== "production") { throw new Error(`The layouts "${filePath}" and "${existing.contextKey}" conflict on the route "/${route}". Please remove or rename one of these files.`); } } else { node = getLayoutNode(node, options); directory.layout[meta.specificity] = node; } } else if (meta.isMiddleware) { directory.middleware = node; } else if (type === "api") { const fileKey = `${route}+api`; let nodes = directory.files.get(fileKey); if (!nodes) { nodes = []; directory.files.set(fileKey, nodes); } const existing = nodes[0]; if (existing) { if (process.env.NODE_ENV !== "production") { throw new Error(`The API route file "${filePath}" and "${existing.contextKey}" conflict on the route "/${route}". Please remove or rename one of these files.`); } } else { nodes[0] = node; } } else { let nodes = directory.files.get(route); if (!nodes) { nodes = []; directory.files.set(route, nodes); } const existing = nodes[meta.specificity]; if (existing) { if (process.env.NODE_ENV !== "production") { throw new Error(`The route files "${filePath}" and "${existing.contextKey}" conflict on the route "/${route}". Please remove or rename one of these files.`); } } else { hasRoutes ||= true; nodes[meta.specificity] = node; } } } } if (!isValid) { return null; } if (!rootDirectory.layout) { rootDirectory.layout = [{ type: "layout", loadRoute: () => ({ default: (() => { try { return require("../views/Navigator.cjs"); } catch (e) { return { DefaultNavigator: () => { throw e; } }; } })().DefaultNavigator }), // Generate a fake file name for the directory contextKey: "router/build/views/Navigator.js", route: "", generated: true, dynamic: null, children: [] }]; } if (hasRoutes) { appendSitemapRoute(rootDirectory); } appendNotFoundRoute(rootDirectory); return rootDirectory; } function flattenDirectoryTreeToRoutes(directory, options, layout, pathToRemove = "", parentMiddlewares) { if (directory.layout) { const previousLayout = layout; layout = getMostSpecific(directory.layout); if (previousLayout) { previousLayout.children.push(layout); } const newRoute = layout.route.replace(pathToRemove, ""); pathToRemove = layout.route ? `${layout.route}/` : ""; layout.route = newRoute; layout.dynamic = generateDynamic(layout.route); } if (!layout) throw new Error("One Internal Error: No nearest layout"); const middlewares = directory.middleware ? [...(parentMiddlewares || []), directory.middleware] : parentMiddlewares; for (const routes of directory.files.values()) { const routeNode = getMostSpecific(routes); routeNode.route = routeNode.route.replace(pathToRemove, ""); routeNode.dynamic = generateDynamic(routeNode.route); routeNode.middlewares = middlewares; layout.children.push(routeNode); } for (const child of directory.subdirectories.values()) { flattenDirectoryTreeToRoutes(child, options, layout, pathToRemove, middlewares); } if (directory.slots.size > 0) { layout.slots = /* @__PURE__ */new Map(); for (const [slotName, slotDir] of directory.slots) { const slotConfig = flattenSlotDirectory(slotDir, slotName, options, pathToRemove); layout.slots.set(slotName, slotConfig); } } return layout; } function flattenSlotDirectory(directory, slotName, options, pathToRemove) { const interceptRoutes = []; let defaultRoute; for (const routes of directory.files.values()) { const routeNode = getMostSpecific(routes); let cleanRoute = routeNode.route.replace(pathToRemove, ""); cleanRoute = cleanRoute.split("/").filter(segment => !(0, import_matchers.matchSlotName)(segment)).map(segment => (0, import_matchers.stripInterceptPrefix)(segment)).join("/"); if (cleanRoute.endsWith("default") || cleanRoute === "default") { defaultRoute = { ...routeNode, route: cleanRoute, slotName, dynamic: generateDynamic(cleanRoute) }; } else { interceptRoutes.push({ ...routeNode, route: cleanRoute, slotName, dynamic: generateDynamic(cleanRoute) }); } } for (const [subDirName, subDir] of directory.subdirectories) { const subRoutes = flattenSlotSubdirectory(subDir, slotName, options, pathToRemove, subDirName); interceptRoutes.push(...subRoutes); } return { name: slotName, defaultRoute, interceptRoutes }; } function flattenSlotSubdirectory(directory, slotName, options, pathToRemove, currentPath) { const routes = []; for (const fileRoutes of directory.files.values()) { const routeNode = getMostSpecific(fileRoutes); let cleanRoute = routeNode.route.replace(pathToRemove, ""); cleanRoute = cleanRoute.split("/").filter(segment => !(0, import_matchers.matchSlotName)(segment)).map(segment => (0, import_matchers.stripInterceptPrefix)(segment)).join("/"); routes.push({ ...routeNode, route: cleanRoute, slotName, dynamic: generateDynamic(cleanRoute) }); } for (const [subDirName, subDir] of directory.subdirectories) { const subPath = currentPath ? `${currentPath}/${subDirName}` : subDirName; routes.push(...flattenSlotSubdirectory(subDir, slotName, options, pathToRemove, subPath)); } return routes; } function getFileMeta(key, options, parentRenderMode) { key = key.replace(/^\.\//, ""); const parts = key.split("/"); let route = (0, import_matchers.removeSupportedExtensions)(key); const filename = parts[parts.length - 1]; const filenameWithoutExtensions = (0, import_matchers.removeSupportedExtensions)(filename); const isLayout = filenameWithoutExtensions.startsWith("_layout"); const isMiddleware = filenameWithoutExtensions.startsWith("_middleware"); const [, renderModeFound] = filename.match(/\+(api|ssg|ssr|spa)\.(\w+\.)?[jt]sx?$/) || []; const fileRenderMode = renderModeFound; const renderMode = fileRenderMode ?? parentRenderMode; route = route.split("/").map(segment => { const dirRenderMode = (0, import_matchers.matchDirectoryRenderMode)(segment); return dirRenderMode ? dirRenderMode.name : segment; }).join("/"); if (filenameWithoutExtensions.startsWith("(") && filenameWithoutExtensions.endsWith(")")) { throw new Error(`Invalid route ./${key}. Routes cannot end with '(group)' syntax`); } if (renderMode !== "api" && filename.startsWith("+") && filenameWithoutExtensions !== "+not-found") { const renamedRoute = [...parts.slice(0, -1), filename.slice(1)].join("/"); throw new Error(`Invalid route ./${key}. Route nodes cannot start with the '+' character. "Please rename to ${renamedRoute}"`); } let slotName; for (const part of parts) { const match = (0, import_matchers.matchSlotName)(part); if (match) { slotName = match; break; } } let interceptMatch; for (let i = 0; i < parts.length; i++) { const part = parts[i]; const match = (0, import_matchers.matchInterceptPrefix)(part); if (match) { const remainingParts = parts.slice(i + 1).map(p => (0, import_matchers.removeSupportedExtensions)(p)).filter(p => p !== "index"); const fullTargetPath = [match.targetPath, ...remainingParts].filter(Boolean).join("/"); interceptMatch = { ...match, targetPath: fullTargetPath }; break; } } let specificity = 0; const platformExtension = filenameWithoutExtensions.split(".")[1]; const hasPlatformExtension = validPlatforms.has(platformExtension); const usePlatformRoutes = options.platformRoutes ?? true; if (hasPlatformExtension) { if (!usePlatformRoutes) { specificity = -1; } else if (!options.platform) { specificity = -1; } else if (platformExtension === options.platform) { specificity = 2; } else if (platformExtension === "native" && options.platform !== "web") { specificity = 1; } else if (platformExtension !== options.platform) { specificity = -1; } if (renderMode === "api" && specificity !== 0) { throw new Error(`Api routes cannot have platform extensions. Please remove '.${platformExtension}' from './${key}'`); } route = route.replace(new RegExp(`.${platformExtension}$`), ""); } return { route, specificity, isLayout, isMiddleware, renderMode, slotName, interceptMatch }; } function getMostSpecific(routes) { const route = routes[routes.length - 1]; if (!routes[0]) { throw new Error(` [one] The file ${route.contextKey} does not have a fallback sibling file without a platform extension in routes (${routes[0]}, ${routes.length}): ${routes.map(r => r.contextKey || "NONE").join("\n")}.`); } return route; } function getIgnoreList(options) { const ignore = [/^\.\/\+html\.[tj]sx?$/, ...(options?.ignore ?? [])]; if (options?.preserveApiRoutes !== true) { ignore.push(/\+api\.[tj]sx?$/); } return ignore; } function extrapolateGroups(key, keys = /* @__PURE__ */new Set()) { const match = (0, import_matchers.matchArrayGroupName)(key); if (!match) { keys.add(key); return keys; } const groups = match.split(","); const groupsSet = new Set(groups); if (groupsSet.size !== groups.length) { throw new Error(`Array syntax cannot contain duplicate group name "${groups}" in "${key}".`); } if (groups.length === 1) { keys.add(key); return keys; } for (const group of groups) { extrapolateGroups(key.replace(match, group.trim()), keys); } return keys; } function generateDynamic(path) { const dynamic = path.split("/").map(part => { if (part === "+not-found") { return { name: "+not-found", deep: true, notFound: true }; } const dynamicMatch = (0, import_matchers.matchDynamicName)(part); if (!dynamicMatch) return null; return { name: dynamicMatch.name, deep: dynamicMatch.deep }; }).filter(part => !!part); return dynamic.length === 0 ? null : dynamic; } function appendSitemapRoute(directory) { if (!directory.files.has("_sitemap")) { directory.files.set("_sitemap", [{ loadRoute() { return { default: () => null, getNavOptions: () => {} }; }, route: "_sitemap", type: "ssg", contextKey: "", generated: true, internal: true, dynamic: null, children: [] }]); } } function hasNotFoundInGroupSubdirs(directory) { for (const [dirName, subDir] of directory.subdirectories) { if (!(0, import_matchers.matchGroupName)(dirName)) continue; for (const fileKey of subDir.files.keys()) { if (fileKey.endsWith("+not-found")) { return true; } } if (hasNotFoundInGroupSubdirs(subDir)) { return true; } } return false; } function appendNotFoundRoute(directory) { if (!directory.files.has("+not-found") && !hasNotFoundInGroupSubdirs(directory)) { directory.files.set("+not-found", [{ loadRoute() { return { default: () => null }; }, type: "spa", route: "+not-found", contextKey: "", generated: true, internal: true, dynamic: [{ name: "+not-found", deep: true, notFound: true }], children: [] }]); } } function getLayoutNode(node, options) { const groupName = (0, import_matchers.matchGroupName)(node.route); const childMatchingGroup = node.children.find(child => { return child.route.replace(/\/index$/, "") === groupName; }); const initialRouteName = childMatchingGroup?.route; return { ...node, route: node.route.replace(/\/?_layout$/, ""), children: [], // Each layout should have its own children initialRouteName }; } function crawlAndAppendInitialRoutesAndEntryFiles(node, options, entryPoints = []) { if (node.type === "spa" || node.type === "ssg" || node.type === "ssr") { node.entryPoints = [... /* @__PURE__ */new Set([...entryPoints, node.contextKey])]; } else if (node.type === "layout") { if (!node.children) { throw new Error(`Layout "${node.contextKey}" does not contain any child routes`); } entryPoints = [...entryPoints, node.contextKey]; const groupName = (0, import_matchers.matchGroupName)(node.route); const childMatchingGroup = node.children.find(child => { return child.route.replace(/\/index$/, "") === groupName; }); let initialRouteName = childMatchingGroup?.route; const loaded = node.loadRoute(); if (loaded?.unstable_settings) { initialRouteName = loaded.unstable_settings.initialRouteName ?? initialRouteName; if (groupName) { const groupSpecificInitialRouteName = loaded.unstable_settings?.[groupName]?.initialRouteName; initialRouteName = groupSpecificInitialRouteName ?? initialRouteName; } } if (initialRouteName) { const initialRoute = node.children.find(child => child.route === initialRouteName); if (!initialRoute) { const validInitialRoutes = node.children.filter(child => !child.generated).map(child => `'${child.route}'`).join(", "); if (groupName) { throw new Error(`Layout ${node.contextKey} has invalid initialRouteName '${initialRouteName}' for group '(${groupName})'. Valid options are: ${validInitialRoutes}`); } throw new Error(`Layout ${node.contextKey} has invalid initialRouteName '${initialRouteName}'. Valid options are: ${validInitialRoutes}`); } node.initialRouteName = initialRouteName; entryPoints.push(initialRoute.contextKey); } for (const child of node.children) { crawlAndAppendInitialRoutesAndEntryFiles(child, options, entryPoints); } } }