UNPKG

one

Version:

One is a React framework for web, iOS, and Android apps.

1 lines • 3.51 kB
{"version":3,"file":"generateRouteTypes.js","names":[],"sources":["esm/typed-routes/generateRouteTypes.js"],"sourcesContent":["import { readFile, writeFile } from \"node:fs/promises\";\nimport { dirname, join } from \"node:path\";\nimport FSExtra from \"fs-extra\";\nimport { removeSupportedExtensions } from \"../router/matchers\";\nimport { globbedRoutesToRouteContext } from \"../router/useViteRoutes\";\nimport { getRoutePaths } from \"../utils/routeIndex\";\nimport { getTypedRoutesDeclarationFile } from \"./getTypedRoutesDeclarationFile\";\nimport { injectRouteHelpers } from \"./injectRouteHelpers\";\nasync function generateRouteTypes(outFile, routerRoot, ignoredRouteFiles, typedRoutesMode, routePathsIn) {\n const routePaths = routePathsIn ?? getRoutePaths(routerRoot, ignoredRouteFiles);\n const routes = routePaths.reduce((acc, cur) => {\n const vitePath = `/${routerRoot}/${cur.replace(/^\\.\\//, \"\")}`;\n acc[vitePath] = {};\n return acc;\n }, {});\n const context = globbedRoutesToRouteContext(routes, routerRoot);\n const declarations = getTypedRoutesDeclarationFile(context);\n const outDir = dirname(outFile);\n let currentDeclarations;\n try {\n currentDeclarations = await readFile(outFile, \"utf8\");\n } catch (error) {\n if (error.code !== \"ENOENT\") {\n throw error;\n }\n }\n if (currentDeclarations !== declarations) {\n await FSExtra.ensureDir(outDir);\n await writeFile(outFile, declarations);\n }\n if (typedRoutesMode) {\n const mode = typedRoutesMode === \"type\" ? \"type\" : \"runtime\";\n for (const routePath of routePaths) {\n if (routePath.includes(\"_layout\") || routePath.includes(\"+api\") || routePath.startsWith(\"_\") || routePath.endsWith(\".d.ts\")) {\n continue;\n }\n const fullPath = join(process.cwd(), routerRoot, routePath);\n const routeName = routePath.replace(/^\\.\\//, \"\").replace(/\\+[^/]*$/, \"\").replace(/\\/index$/, \"\").replace(/index$/, \"\");\n let cleanRouteName = removeSupportedExtensions(routeName).replace(/\\/?index$/, \"\");\n if (!cleanRouteName.startsWith(\"/\")) {\n cleanRouteName = \"/\" + cleanRouteName;\n }\n if (!cleanRouteName.includes(\"[\")) {\n continue;\n }\n await injectRouteHelpers(fullPath, cleanRouteName, mode);\n }\n }\n}\nexport {\n generateRouteTypes\n};\n//# sourceMappingURL=generateRouteTypes.js.map\n"],"mappings":";;;;;;;;;;AAQA,eAAe,mBAAmB,SAAS,YAAY,mBAAmB,iBAAiB,cAAc;CACvG,MAAM,aAAa,gBAAgB,cAAc,YAAY,iBAAiB;CAC9E,MAAM,SAAS,WAAW,QAAQ,KAAK,QAAQ;EAC7C,MAAM,WAAW,IAAI,WAAW,GAAG,IAAI,QAAQ,SAAS,EAAE;EAC1D,IAAI,YAAY,CAAC;EACjB,OAAO;CACT,GAAG,CAAC,CAAC;CACL,MAAM,UAAU,4BAA4B,QAAQ,UAAU;CAC9D,MAAM,eAAe,8BAA8B,OAAO;CAC1D,MAAM,SAAS,QAAQ,OAAO;CAC9B,IAAI;CACJ,IAAI;EACF,sBAAsB,MAAM,SAAS,SAAS,MAAM;CACtD,SAAS,OAAO;EACd,IAAI,MAAM,SAAS,UAAU;GAC3B,MAAM;EACR;CACF;CACA,IAAI,wBAAwB,cAAc;EACxC,MAAM,QAAQ,UAAU,MAAM;EAC9B,MAAM,UAAU,SAAS,YAAY;CACvC;CACA,IAAI,iBAAiB;EACnB,MAAM,OAAO,oBAAoB,SAAS,SAAS;EACnD,KAAK,MAAM,aAAa,YAAY;GAClC,IAAI,UAAU,SAAS,SAAS,KAAK,UAAU,SAAS,MAAM,KAAK,UAAU,WAAW,GAAG,KAAK,UAAU,SAAS,OAAO,GAAG;IAC3H;GACF;GACA,MAAM,WAAW,KAAK,QAAQ,IAAI,GAAG,YAAY,SAAS;GAC1D,MAAM,YAAY,UAAU,QAAQ,SAAS,EAAE,CAAC,CAAC,QAAQ,YAAY,EAAE,CAAC,CAAC,QAAQ,YAAY,EAAE,CAAC,CAAC,QAAQ,UAAU,EAAE;GACrH,IAAI,iBAAiB,0BAA0B,SAAS,CAAC,CAAC,QAAQ,aAAa,EAAE;GACjF,IAAI,CAAC,eAAe,WAAW,GAAG,GAAG;IACnC,iBAAiB,MAAM;GACzB;GACA,IAAI,CAAC,eAAe,SAAS,GAAG,GAAG;IACjC;GACF;GACA,MAAM,mBAAmB,UAAU,gBAAgB,IAAI;EACzD;CACF;AACF"}