one
Version:
One is a new React Framework that makes Vite serve both native and web.
36 lines (35 loc) • 2.13 kB
JavaScript
import { writeFile } from "node:fs/promises";
import { dirname, join } from "node:path";
import FSExtra from "fs-extra";
import micromatch from "micromatch";
import { removeSupportedExtensions } from "../router/matchers.mjs";
import { globbedRoutesToRouteContext } from "../router/useViteRoutes.mjs";
import { globDir } from "../utils/globDir.mjs";
import { getTypedRoutesDeclarationFile } from "./getTypedRoutesDeclarationFile.mjs";
import { injectRouteHelpers } from "./injectRouteHelpers.mjs";
async function generateRouteTypes(outFile, routerRoot, ignoredRouteFiles, typedRoutesMode) {
let 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
}));
const routes = routePaths.reduce((acc, cur) => {
const vitePath = `/${routerRoot}/${cur.replace(/^\.\//, "")}`;
return acc[vitePath] = {}, acc;
}, {}),
context = globbedRoutesToRouteContext(routes, routerRoot),
declarations = getTypedRoutesDeclarationFile(context),
outDir = dirname(outFile);
if (await FSExtra.ensureDir(outDir), await writeFile(outFile, declarations), typedRoutesMode) {
const mode = typedRoutesMode === "type" ? "type" : "runtime";
for (const routePath of routePaths) {
if (routePath.includes("_layout") || routePath.includes("+api") || routePath.startsWith("_") || routePath.endsWith(".d.ts")) continue;
const fullPath = join(process.cwd(), routerRoot, routePath),
routeName = routePath.replace(/^\.\//, "").replace(/\+[^/]*$/, "").replace(/\/index$/, "").replace(/index$/, "");
let cleanRouteName = removeSupportedExtensions(routeName).replace(/\/?index$/, "");
cleanRouteName.startsWith("/") || (cleanRouteName = "/" + cleanRouteName), cleanRouteName.includes("[") && (await injectRouteHelpers(fullPath, cleanRouteName, mode));
}
}
}
export { generateRouteTypes };
//# sourceMappingURL=generateRouteTypes.mjs.map