{"version":3,"file":"react-refresh-route-components.cjs","names":[],"sources":["../../../../../src/core/code-splitter/plugins/react-refresh-route-components.ts"],"sourcesContent":["import * as t from '@babel/types'\nimport {\n  getObjectPropertyKeyName,\n  getUniqueProgramIdentifier,\n} from '../../utils'\nimport type { ReferenceRouteCompilerPlugin } from '../plugins'\n\nconst REACT_REFRESH_ROUTE_COMPONENT_IDENTS = new Set([\n  'component',\n  'shellComponent',\n  'pendingComponent',\n  'errorComponent',\n  'notFoundComponent',\n])\n\ntype RouteComponentContext = {\n  programPath: Parameters<typeof getUniqueProgramIdentifier>[0]\n  insertionPath: { insertBefore: (nodes: Array<t.VariableDeclaration>) => void }\n  routeOptions: t.ObjectExpression\n}\n\nfunction isReactComponentName(name: string) {\n  const firstCharacter = name[0]\n\n  return (\n    firstCharacter !== undefined &&\n    firstCharacter >= 'A' &&\n    firstCharacter <= 'Z'\n  )\n}\n\nfunction getRouteComponentKey(prop: t.ObjectProperty) {\n  const key = getObjectPropertyKeyName(prop)\n\n  return key && REACT_REFRESH_ROUTE_COMPONENT_IDENTS.has(key) ? key : undefined\n}\n\nfunction prepareRouteComponentsForReactRefresh(ctx: RouteComponentContext) {\n  const hoistedDeclarations: Array<t.VariableDeclaration> = []\n  let modified = false\n\n  for (const prop of ctx.routeOptions.properties) {\n    if (!t.isObjectProperty(prop)) {\n      continue\n    }\n\n    const key = getRouteComponentKey(prop)\n\n    if (!key) {\n      continue\n    }\n\n    if (t.isIdentifier(prop.value)) {\n      if (isReactComponentName(prop.value.name)) {\n        continue\n      }\n\n      const bindingNode = ctx.programPath.scope.getBinding(prop.value.name)\n        ?.path.node\n      const isLocalComponentBinding =\n        t.isFunctionDeclaration(bindingNode) ||\n        t.isClassDeclaration(bindingNode) ||\n        t.isVariableDeclarator(bindingNode)\n\n      if (!isLocalComponentBinding) {\n        continue\n      }\n\n      const componentIdentifier = getUniqueProgramIdentifier(\n        ctx.programPath,\n        `TSR${key[0]!.toUpperCase()}${key.slice(1)}`,\n      )\n\n      ctx.programPath.scope.rename(prop.value.name, componentIdentifier.name)\n      modified = true\n      continue\n    }\n\n    if (\n      !t.isArrowFunctionExpression(prop.value) &&\n      !t.isFunctionExpression(prop.value)\n    ) {\n      continue\n    }\n\n    const hoistedIdentifier = getUniqueProgramIdentifier(\n      ctx.programPath,\n      `TSR${key[0]!.toUpperCase()}${key.slice(1)}`,\n    )\n\n    hoistedDeclarations.push(\n      t.variableDeclaration('const', [\n        t.variableDeclarator(hoistedIdentifier, t.cloneNode(prop.value, true)),\n      ]),\n    )\n\n    prop.value = t.cloneNode(hoistedIdentifier)\n    modified = true\n  }\n\n  if (hoistedDeclarations.length > 0) {\n    ctx.insertionPath.insertBefore(hoistedDeclarations)\n  }\n\n  return modified\n}\n\nexport function createReactRefreshRouteComponentsPlugin(): ReferenceRouteCompilerPlugin {\n  return {\n    name: 'react-refresh-route-components',\n    getStableRouteOptionKeys() {\n      return [...REACT_REFRESH_ROUTE_COMPONENT_IDENTS]\n    },\n    onAddHmr(ctx) {\n      if (prepareRouteComponentsForReactRefresh(ctx)) {\n        return { modified: true }\n      }\n\n      return\n    },\n    onVirtualRouteSplitNode(ctx) {\n      if (\n        ctx.splitNodeMeta.splitStrategy !== 'lazyRouteComponent' ||\n        !t.isFunctionDeclaration(ctx.splitNode) ||\n        !ctx.splitNode.id ||\n        isReactComponentName(ctx.splitNode.id.name)\n      ) {\n        return\n      }\n\n      const componentIdentifier = getUniqueProgramIdentifier(\n        ctx.programPath,\n        ctx.splitNodeMeta.localExporterIdent,\n      )\n\n      ctx.programPath.scope.rename(\n        ctx.splitNode.id.name,\n        componentIdentifier.name,\n      )\n    },\n  }\n}\n"],"mappings":";;;;;AAOA,IAAM,uCAAuC,IAAI,IAAI;CACnD;CACA;CACA;CACA;CACA;AACF,CAAC;AAQD,SAAS,qBAAqB,MAAc;CAC1C,MAAM,iBAAiB,KAAK;CAE5B,OACE,mBAAmB,KAAA,KACnB,kBAAkB,OAClB,kBAAkB;AAEtB;AAEA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