one
Version:
One is a new React Framework that makes Vite serve both native and web.
68 lines (67 loc) • 2.89 kB
JavaScript
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);
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
});
}
var routes = routePaths.reduce(function (acc, cur) {
var vitePath = `/${routerRoot}/${cur.replace(/^\.\//, "")}`;
acc[vitePath] = {};
return acc;
}, {});
var context = globbedRoutesToRouteContext(routes, routerRoot);
var declarations = getTypedRoutesDeclarationFile(context);
var outDir = dirname(outFile);
await FSExtra.ensureDir(outDir);
await writeFile(outFile, declarations);
if (typedRoutesMode) {
var mode = typedRoutesMode === "type" ? "type" : "runtime";
var _iteratorNormalCompletion = true,
_didIteratorError = false,
_iteratorError = void 0;
try {
for (var _iterator = routePaths[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var routePath = _step.value;
if (routePath.includes("_layout") || routePath.includes("+api") || routePath.startsWith("_") || routePath.endsWith(".d.ts")) {
continue;
}
var fullPath = join(process.cwd(), routerRoot, routePath);
var routeName = routePath.replace(/^\.\//, "").replace(/\+[^/]*$/, "").replace(/\/index$/, "").replace(/index$/, "");
var cleanRouteName = removeSupportedExtensions(routeName).replace(/\/?index$/, "");
if (!cleanRouteName.startsWith("/")) {
cleanRouteName = "/" + cleanRouteName;
}
if (!cleanRouteName.includes("[")) {
continue;
}
await injectRouteHelpers(fullPath, cleanRouteName, mode);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
}
}
export { generateRouteTypes };
//# sourceMappingURL=generateRouteTypes.native.js.map