UNPKG

one

Version:

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

50 lines (49 loc) 2.64 kB
import { writeFile } from "fs/promises"; import { dirname, join } from "path"; import FSExtra from "fs-extra"; import micromatch from "micromatch"; import { removeSupportedExtensions } from "../router/matchers.native.js"; import { globbedRoutesToRouteContext } from "../router/useViteRoutes.native.js"; import { globDir } from "../utils/globDir.native.js"; import { getTypedRoutesDeclarationFile } from "./getTypedRoutesDeclarationFile.native.js"; import { injectRouteHelpers } from "./injectRouteHelpers.native.js"; async function generateRouteTypes(outFile, routerRoot, ignoredRouteFiles, typedRoutesMode) { var routePaths = globDir(routerRoot); ignoredRouteFiles && ignoredRouteFiles.length > 0 && (routePaths = micromatch.not(routePaths, ignoredRouteFiles, { // The path starts with './', such as './foo/bar/baz.test.tsx', and ignoredRouteFiles is like ['**/*.test.*'], so we need matchBase here. matchBase: !0 })); var routes = routePaths.reduce(function (acc, cur) { return acc[cur] = {}, acc; }, {}), context = globbedRoutesToRouteContext(routes, routerRoot), declarations = getTypedRoutesDeclarationFile(context), outDir = dirname(outFile); if (await FSExtra.ensureDir(outDir), await writeFile(outFile, declarations), typedRoutesMode) { var mode = typedRoutesMode === "type" ? "type" : "runtime", _iteratorNormalCompletion = !0, _didIteratorError = !1, _iteratorError = void 0; try { for (var _iterator = routePaths[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = !0) { var routePath = _step.value; if (!(routePath.includes("_layout") || routePath.includes("+api") || routePath.startsWith("_") || routePath.endsWith(".d.ts"))) { var fullPath = join(process.cwd(), routerRoot, routePath), routeName = routePath.replace(/^\.\//, "").replace(/\+[^/]*$/, "").replace(/\/index$/, "").replace(/index$/, ""), cleanRouteName = removeSupportedExtensions(routeName).replace(/\/?index$/, ""); cleanRouteName.startsWith("/") || (cleanRouteName = "/" + cleanRouteName), cleanRouteName.includes("[") && (await injectRouteHelpers(fullPath, cleanRouteName, mode)); } } } catch (err) { _didIteratorError = !0, _iteratorError = err; } finally { try { !_iteratorNormalCompletion && _iterator.return != null && _iterator.return(); } finally { if (_didIteratorError) throw _iteratorError; } } } } export { generateRouteTypes }; //# sourceMappingURL=generateRouteTypes.native.js.map