@vaadin/hilla-file-router
Version:
Hilla file-based router
47 lines • 1.58 kB
JavaScript
import { createElement } from "react";
import { convertComponentNameToTitle } from "../../shared/convertComponentNameToTitle.js";
import { isReactRouteModule } from "./utils.js";
/**
* Transforms a framework-agnostic route into a route object compatible with
* React Router and merges it with the existing route object if any.
*
* @param options - The route transformer options containing the existing route,
* any overrides, and child routes.
*
* @returns A `RouteObject` representing the transformed route, or the original
* route if no override is provided.
*
* @throws If the provided module does not export a React component by default
* nor a ViewConfig object as "config".
*/
export default function fileRouteTransformer({ original, override, children }) {
if (!override) {
return original;
}
const { module, component = module?.default, config = module?.config, path, flowLayout } = override;
if (module && !isReactRouteModule(module)) {
throw new Error(`The module for the "${path}" section doesn't have the React component exported by default or a ViewConfig object exported as "config"`);
}
const element = component ? createElement(component) : undefined;
const handle = {
...config,
title: config?.title ?? convertComponentNameToTitle(component),
flowLayout: config?.flowLayout ?? flowLayout
};
if (path === "" && !children) {
return {
...original,
element,
handle,
index: true
};
}
return {
...original,
path: config?.route ?? path,
element,
children,
handle
};
}
//# sourceMappingURL=./fileRouteTransformer.js.map