{"version":3,"sources":["../../../../src/build/segment-config/app/app-segments.ts"],"sourcesContent":["import type { LoadComponentsReturnType } from '../../../server/load-components'\nimport type { Params } from '../../../server/request/params'\nimport type {\n  AppPageRouteModule,\n  AppPageModule,\n} from '../../../server/route-modules/app-page/module.compiled'\nimport type {\n  AppRouteRouteModule,\n  AppRouteModule,\n} from '../../../server/route-modules/app-route/module.compiled'\nimport {\n  type AppSegmentConfig,\n  parseAppSegmentConfig,\n} from './app-segment-config'\n\nimport { InvariantError } from '../../../shared/lib/invariant-error'\nimport {\n  isAppRouteRouteModule,\n  isAppPageRouteModule,\n} from '../../../server/route-modules/checks'\nimport { isClientReference } from '../../../lib/client-and-server-references'\nimport { getSegmentParam } from '../../../server/app-render/get-segment-param'\nimport {\n  getLayoutOrPageModule,\n  type LoaderTree,\n} from '../../../server/lib/app-dir-module'\nimport { PAGE_SEGMENT_KEY } from '../../../shared/lib/segment'\n\ntype GenerateStaticParams = (options: { params?: Params }) => Promise<Params[]>\n\n/**\n * Parses the app config and attaches it to the segment.\n */\nfunction attach(segment: AppSegment, userland: unknown, route: string) {\n  // If the userland is not an object, then we can't do anything with it.\n  if (typeof userland !== 'object' || userland === null) {\n    return\n  }\n\n  // Try to parse the application configuration.\n  const config = parseAppSegmentConfig(userland, route)\n\n  // If there was any keys on the config, then attach it to the segment.\n  if (Object.keys(config).length > 0) {\n    segment.config = config\n  }\n\n  if (\n    'generateStaticParams' in userland &&\n    typeof userland.generateStaticParams === 'function'\n  ) {\n    segment.generateStaticParams =\n      userland.generateStaticParams as GenerateStaticParams\n\n    // Validate that `generateStaticParams` makes sense in this context.\n    if (segment.config?.runtime === 'edge') {\n      throw new Error(\n        'Edge runtime is not supported with `generateStaticParams`.'\n      )\n    }\n  }\n}\n\nexport type AppSegment = {\n  name: string\n  param: string | undefined\n  filePath: string | undefined\n  config: AppSegmentConfig | undefined\n  isDynamicSegment: boolean\n  generateStaticParams: GenerateStaticParams | undefined\n}\n\n/**\n * Walks the loader tree and collects the generate parameters for each segment.\n *\n * @param routeModule the app page route module\n * @returns the segments for the app page route module\n */\nasync function collectAppPageSegments(routeModule: AppPageRouteModule) {\n  // We keep track of unique segments, since with parallel routes, it's possible\n  // to see the same segment multiple times.\n  const uniqueSegments = new Map<string, AppSegment>()\n\n  // Queue will store tuples of [loaderTree, currentSegments]\n  type QueueItem = [LoaderTree, AppSegment[]]\n  const queue: QueueItem[] = [[routeModule.userland.loaderTree, []]]\n\n  while (queue.length > 0) {\n    const [loaderTree, currentSegments] = queue.shift()!\n    const [name, parallelRoutes] = loaderTree\n\n    // Process current node\n    const { mod: userland, filePath } = await getLayoutOrPageModule(loaderTree)\n    const isClientComponent = userland && isClientReference(userland)\n\n    const param = getSegmentParam(name)?.param\n\n    const segment: AppSegment = {\n      name,\n      param,\n      filePath,\n      config: undefined,\n      isDynamicSegment: !!param,\n      generateStaticParams: undefined,\n    }\n\n    // Only server components can have app segment configurations\n    if (!isClientComponent) {\n      attach(segment, userland, routeModule.definition.pathname)\n    }\n\n    // Create a unique key for the segment\n    const segmentKey = getSegmentKey(segment)\n    if (!uniqueSegments.has(segmentKey)) {\n      uniqueSegments.set(segmentKey, segment)\n    }\n\n    const updatedSegments = [...currentSegments, segment]\n\n    // If this is a page segment, we've reached a leaf node\n    if (name === PAGE_SEGMENT_KEY) {\n      // Add all segments in the current path\n      updatedSegments.forEach((seg) => {\n        const key = getSegmentKey(seg)\n        uniqueSegments.set(key, seg)\n      })\n    }\n\n    // Add all parallel routes to the queue\n    for (const parallelRouteKey in parallelRoutes) {\n      const parallelRoute = parallelRoutes[parallelRouteKey]\n      queue.push([parallelRoute, updatedSegments])\n    }\n  }\n\n  return Array.from(uniqueSegments.values())\n}\n\nfunction getSegmentKey(segment: AppSegment) {\n  return `${segment.name}-${segment.filePath ?? ''}-${segment.param ?? ''}`\n}\n\n/**\n * Collects the segments for a given app route module.\n *\n * @param routeModule the app route module\n * @returns the segments for the app route module\n */\nfunction collectAppRouteSegments(\n  routeModule: AppRouteRouteModule\n): AppSegment[] {\n  // Get the pathname parts, slice off the first element (which is empty).\n  const parts = routeModule.definition.pathname.split('/').slice(1)\n  if (parts.length === 0) {\n    throw new InvariantError('Expected at least one segment')\n  }\n\n  // Generate all the segments.\n  const segments: AppSegment[] = parts.map((name) => {\n    const param = getSegmentParam(name)?.param\n\n    return {\n      name,\n      param,\n      filePath: undefined,\n      isDynamicSegment: !!param,\n      config: undefined,\n      generateStaticParams: undefined,\n    }\n  })\n\n  // We know we have at least one, we verified this above. We should get the\n  // last segment which represents the root route module.\n  const segment = segments[segments.length - 1]\n\n  segment.filePath = routeModule.definition.filename\n\n  // Extract the segment config from the userland module.\n  attach(segment, routeModule.userland, routeModule.definition.pathname)\n\n  return segments\n}\n\n/**\n * Collects the segments for a given route module.\n *\n * @param components the loaded components\n * @returns the segments for the route module\n */\nexport function collectSegments({\n  routeModule,\n}: LoadComponentsReturnType<AppPageModule | AppRouteModule>):\n  | Promise<AppSegment[]>\n  | AppSegment[] {\n  if (isAppRouteRouteModule(routeModule)) {\n    return collectAppRouteSegments(routeModule)\n  }\n\n  if (isAppPageRouteModule(routeModule)) {\n    return collectAppPageSegments(routeModule)\n  }\n\n  throw new InvariantError(\n    'Expected a route module to be one of app route or page'\n  )\n}\n"],"names":["parseAppSegmentConfig","InvariantError","isAppRouteRouteModule","isAppPageRouteModule","isClientReference","getSegmentParam","getLayoutOrPageModule","PAGE_SEGMENT_KEY","attach","segment","userland","route","config","Object","keys","length","generateStaticParams","runtime","Error","collectAppPageSegments","routeModule","uniqueSegments","Map","queue","loaderTree","currentSegments","shift","name","parallelRoutes","mod","filePath","isClientComponent","param","undefined","isDynamicSegment","definition","pathname","segmentKey","getSegmentKey","has","set","updatedSegments","forEach","seg","key","parallelRouteKey","parallelRoute","push","Array","from","values","collectAppRouteSegments","parts","split","slice","segments","map","filename","collectSegments"],"mappings":"AAUA,SAEEA,qBAAqB,QAChB,uBAAsB;AAE7B,SAASC,cAAc,QAAQ,sCAAqC;A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