one
Version:
One is a new React Framework that makes Vite serve both native and web.
49 lines (48 loc) • 2.24 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);
if (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: true
});
}
const routes = routePaths.reduce((acc, cur) => {
const vitePath = `/${routerRoot}/${cur.replace(/^\.\//, "")}`;
acc[vitePath] = {};
return acc;
}, {});
const context = globbedRoutesToRouteContext(routes, routerRoot);
const declarations = getTypedRoutesDeclarationFile(context);
const outDir = dirname(outFile);
await FSExtra.ensureDir(outDir);
await writeFile(outFile, declarations);
if (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);
const routeName = routePath.replace(/^\.\//, "").replace(/\+[^/]*$/, "").replace(/\/index$/, "").replace(/index$/, "");
let cleanRouteName = removeSupportedExtensions(routeName).replace(/\/?index$/, "");
if (!cleanRouteName.startsWith("/")) {
cleanRouteName = "/" + cleanRouteName;
}
if (!cleanRouteName.includes("[")) {
continue;
}
await injectRouteHelpers(fullPath, cleanRouteName, mode);
}
}
}
export { generateRouteTypes };
//# sourceMappingURL=generateRouteTypes.mjs.map