UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

108 lines (106 loc) 4.92 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: !0 }); }, __copyProps = (to, from, except, desc) => { if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod); var injectRouteHelpers_exports = {}; __export(injectRouteHelpers_exports, { injectRouteHelpers: () => injectRouteHelpers }); module.exports = __toCommonJS(injectRouteHelpers_exports); var import_node_fs = require("node:fs"), import_promises = require("node:fs/promises"); async function injectRouteHelpers(filePath, routePath, mode) { if (!(0, import_node_fs.existsSync)(filePath)) return !1; try { let content = await (0, import_promises.readFile)(filePath, "utf-8"), modified = !1; const hasTypeRoute = /^type\s+Route\s*=/m.test(content), hasConstRoute = /^const\s+route\s*=/m.test(content); if (mode === "runtime" && !hasConstRoute) { const { updatedContent } = addCreateRouteImport(content); content = updatedContent; const routeDeclaration = `const route = createRoute<'${routePath}'>()`; content = insertAfterImports(content, routeDeclaration), modified = !0; } if (mode === "type" && !hasTypeRoute) { const { updatedContent } = addRouteTypeImport(content); content = updatedContent; const typeDeclaration = `type Route = RouteType<'${routePath}'>`; content = insertAfterImports(content, typeDeclaration), modified = !0; } return modified ? (await (0, import_promises.writeFile)(filePath, content, "utf-8"), !0) : !1; } catch (error) { return console.error(`Failed to inject route helpers into ${filePath}:`, error), !1; } } function addCreateRouteImport(content) { if (/import\s+[^'"]*createRoute[^'"]*from\s+['"]one['"]/m.test(content)) return { updatedContent: content, importAdded: !1 }; const oneImportRegex = /import\s+{([^}]*)}\s+from\s+['"]one['"]/m, match = content.match(oneImportRegex); if (match) { const existingImports = match[1].trim(), newImports = existingImports ? `${existingImports}, createRoute` : "createRoute"; return { updatedContent: content.replace( oneImportRegex, `import { ${newImports} } from 'one'` ), importAdded: !0 }; } const lastImportIndex = findLastImportIndex(content); if (lastImportIndex >= 0) { const lines = content.split(` `); return lines.splice(lastImportIndex + 1, 0, "import { createRoute } from 'one'"), { updatedContent: lines.join(` `), importAdded: !0 }; } return { updatedContent: `import { createRoute } from 'one' ` + content, importAdded: !0 }; } function addRouteTypeImport(content) { if (/import\s+type\s+[^'"]*RouteType[^'"]*from\s+['"]one['"]/m.test(content)) return { updatedContent: content, importAdded: !1 }; const oneTypeImportRegex = /import\s+type\s+{([^}]*)}\s+from\s+['"]one['"]/m, match = content.match(oneTypeImportRegex); if (match) { const existingImports = match[1].trim(), newImports = existingImports ? `${existingImports}, RouteType` : "RouteType"; return { updatedContent: content.replace( oneTypeImportRegex, `import type { ${newImports} } from 'one'` ), importAdded: !0 }; } const lastImportIndex = findLastImportIndex(content); if (lastImportIndex >= 0) { const lines = content.split(` `); return lines.splice(lastImportIndex + 1, 0, "import type { RouteType } from 'one'"), { updatedContent: lines.join(` `), importAdded: !0 }; } return { updatedContent: `import type { RouteType } from 'one' ` + content, importAdded: !0 }; } function findLastImportIndex(content) { const lines = content.split(` `); let lastImportIndex = -1; for (let i = 0; i < lines.length; i++) { const line = lines[i].trim(); if (line.startsWith("import ") || lastImportIndex >= 0 && (line.startsWith("from ") || line === "}")) lastImportIndex = i; else if (lastImportIndex >= 0 && line && !line.startsWith("//")) break; } return lastImportIndex; } function insertAfterImports(content, codeToInsert) { const lines = content.split(` `), lastImportIndex = findLastImportIndex(content); return lastImportIndex >= 0 ? (lines[lastImportIndex + 1] === "" ? lines.splice(lastImportIndex + 2, 0, codeToInsert, "") : lines.splice(lastImportIndex + 1, 0, "", codeToInsert, ""), lines.join(` `)) : codeToInsert + ` ` + content; } //# sourceMappingURL=injectRouteHelpers.js.map