one
Version:
One is a new React Framework that makes Vite serve both native and web.
44 lines (43 loc) • 1.6 kB
JavaScript
import micromatch from "micromatch";
import path from "node:path";
import { globDir } from "./globDir.mjs";
import { isPathInsideDirectory, isRouteFilePath } from "./routeFileWatch.mjs";
function getRoutePaths(routerRoot, ignoredRouteFiles) {
const routePaths = globDir(routerRoot).filter(routePath => !routePath.endsWith(".d.ts"));
if (!ignoredRouteFiles?.length) return routePaths;
return micromatch.not(routePaths, ignoredRouteFiles, {
matchBase: true
});
}
function createRouteIndex({
routerRoot,
ignoredRouteFiles
}) {
const absoluteRouterRoot = path.resolve(routerRoot);
const routePaths = new Set(getRoutePaths(absoluteRouterRoot, ignoredRouteFiles));
return {
getPaths() {
return [...routePaths].sort();
},
update(event, filePath) {
if (event !== "add" && event !== "delete" && event !== "unlink") return false;
if (!isPathInsideDirectory(filePath, absoluteRouterRoot)) return false;
if (!isRouteFilePath(filePath)) return false;
const relativePath = path.relative(absoluteRouterRoot, path.resolve(filePath)).replaceAll("\\", "/");
const routePath = `./${relativePath}`;
if (ignoredRouteFiles?.length && micromatch.isMatch(routePath, ignoredRouteFiles, {
matchBase: true
})) {
return false;
}
if (event === "add") {
const size = routePaths.size;
routePaths.add(routePath);
return routePaths.size !== size;
}
return routePaths.delete(routePath);
}
};
}
export { createRouteIndex, getRoutePaths };
//# sourceMappingURL=routeIndex.mjs.map