one
Version:
One is a new React Framework that makes Vite serve both native and web.
84 lines • 4.01 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all) __defProp(target, name, {
get: all[name],
enumerable: true
});
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: () => from[key],
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
value: true
}), mod);
var generateRouteTypes_exports = {};
__export(generateRouteTypes_exports, {
generateRouteTypes: () => generateRouteTypes
});
module.exports = __toCommonJS(generateRouteTypes_exports);
var import_promises = require("node:fs/promises");
var import_node_path = require("node:path");
var import_fs_extra = __toESM(require("fs-extra"), 1);
var import_micromatch = __toESM(require("micromatch"), 1);
var import_matchers = require("../router/matchers.cjs");
var import_useViteRoutes = require("../router/useViteRoutes.cjs");
var import_globDir = require("../utils/globDir.cjs");
var import_getTypedRoutesDeclarationFile = require("./getTypedRoutesDeclarationFile.cjs");
var import_injectRouteHelpers = require("./injectRouteHelpers.cjs");
async function generateRouteTypes(outFile, routerRoot, ignoredRouteFiles, typedRoutesMode) {
let routePaths = (0, import_globDir.globDir)(routerRoot);
if (ignoredRouteFiles && ignoredRouteFiles.length > 0) {
routePaths = import_micromatch.default.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 = (0, import_useViteRoutes.globbedRoutesToRouteContext)(routes, routerRoot);
const declarations = (0, import_getTypedRoutesDeclarationFile.getTypedRoutesDeclarationFile)(context);
const outDir = (0, import_node_path.dirname)(outFile);
await import_fs_extra.default.ensureDir(outDir);
await (0, import_promises.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 = (0, import_node_path.join)(process.cwd(), routerRoot, routePath);
const routeName = routePath.replace(/^\.\//, "").replace(/\+[^/]*$/, "").replace(/\/index$/, "").replace(/index$/, "");
let cleanRouteName = (0, import_matchers.removeSupportedExtensions)(routeName).replace(/\/?index$/, "");
if (!cleanRouteName.startsWith("/")) {
cleanRouteName = "/" + cleanRouteName;
}
if (!cleanRouteName.includes("[")) {
continue;
}
await (0, import_injectRouteHelpers.injectRouteHelpers)(fullPath, cleanRouteName, mode);
}
}
}