UNPKG

@vaadin/hilla-file-router

Version:

Hilla file-based router

36 lines 1.3 kB
import { readFile } from "node:fs/promises"; import { join, relative } from "node:path/posix"; import { transformTree } from "../shared/transformTree.js"; import { strip } from "./utils.js"; /** * Enables Flow layout flag on the matching routes based on the information from the layouts JSON file. * * @param routeMeta - The routes tree to process. * @param layoutsFile - The server layouts JSON file. * @returns Processed routes tree. */ export default async function applyLayouts(routeMeta, layoutsFile) { try { const layoutContents = await readFile(layoutsFile, "utf-8"); const availableLayouts = JSON.parse(layoutContents); const layoutPaths = availableLayouts.map((layout) => strip(layout.path)); return transformTree(routeMeta, { path: "" }, (metas, next, ctx) => metas.map((meta) => { const currentPath = join(ctx.path, strip(meta.path)); const children = meta.children ? next(meta.children, { path: currentPath }) : undefined; return layoutPaths.some((path) => !relative(path, currentPath).startsWith("..")) ? { ...meta, flowLayout: true, children } : { ...meta, children }; })); } catch (e) { if (e instanceof Error && "code" in e && e.code === "ENOENT") { return routeMeta; } throw e; } } //# sourceMappingURL=./applyLayouts.js.map