in packages/ice/src/routes.ts [74:143]
export function getRoutesDefinition(options: GetDefinationOptions) {
const { manifest, lazy = false, depth = 0, matchRoute = () => true } = options;
const routeImports: string[] = [];
const routeDefinition = manifest.reduce((prev, route) => {
if (!matchRoute(route)) {
return prev;
}
const { children, path: routePath, index, componentName, file, id, layout, exports } = route;
const componentPath = id.startsWith('__') ? file : getFilePath(file);
let loadStatement = '';
if (lazy) {
loadStatement = `import(/* webpackChunkName: "p_${componentName}" */ '${formatPath(componentPath)}')`;
} else {
const routeSpecifier = formatRouteSpecifier(id);
routeImports.push(`import * as ${routeSpecifier} from '${formatPath(componentPath)}';`);
loadStatement = routeSpecifier;
}
const component = `Component: () => WrapRouteComponent({
routeId: '${id}',
isLayout: ${layout},
routeExports: ${lazy ? 'componentModule' : loadStatement},
})`;
const loader = `loader: createRouteLoader({
routeId: '${id}',
requestContext,
renderMode,
module: ${lazy ? 'componentModule' : loadStatement},
})`;
const routeProperties: string[] = [
`path: '${formatPath(routePath || '')}',`,
`async lazy() {
${lazy ? `const componentModule = await ${loadStatement}` : ''};
return {
${lazy ? '...componentModule' : `...${loadStatement}`},
${component},
${loader},
};
},`,
// Empty errorElement to avoid default ui provided by react-router.
'errorElement: <RouteErrorComponent />,',
`componentName: '${componentName}',`,
`index: ${index},`,
`id: '${id}',`,
'exact: true,',
`exports: ${JSON.stringify(exports)},`,
].filter(Boolean);
if (layout) {
routeProperties.push('layout: true,');
}
if (children) {
const res = getRoutesDefinition({
manifest: children,
lazy,
depth: depth + 1,
matchRoute,
});
routeImports.push(...res.routeImports);
routeProperties.push(`children: [${res.routeDefinition}]`);
}
prev += formatRoutesStr(depth, routeProperties);
return prev;
}, '');
return {
routeImports,
routeDefinition,
};
}