File size: 6,154 Bytes
1df763a
1
{"version":3,"names":["_t","require","cloneNode","exportNamedDeclaration","exportSpecifier","identifier","variableDeclaration","variableDeclarator","splitExportDeclaration","exportDeclaration","isExportDeclaration","isExportAllDeclaration","Error","isExportDefaultDeclaration","declaration","get","standaloneDeclaration","isFunctionDeclaration","isClassDeclaration","exportExpr","isFunctionExpression","isClassExpression","scope","isScope","parent","id","node","needBindingRegistration","generateUidIdentifier","hasBinding","name","updatedDeclaration","updatedExportDeclaration","insertAfter","replaceWith","registerDeclaration","length","bindingIdentifiers","getOuterBindingIdentifiers","specifiers","Object","keys","map","aliasDeclar"],"sources":["../src/index.ts"],"sourcesContent":["import {\n  cloneNode,\n  exportNamedDeclaration,\n  exportSpecifier,\n  identifier,\n  variableDeclaration,\n  variableDeclarator,\n} from \"@babel/types\";\nimport type * as t from \"@babel/types\";\nimport type { NodePath } from \"@babel/traverse\";\n\nexport default function splitExportDeclaration(\n  exportDeclaration: NodePath<\n    t.ExportDefaultDeclaration | t.ExportNamedDeclaration\n  >,\n) {\n  if (\n    !exportDeclaration.isExportDeclaration() ||\n    exportDeclaration.isExportAllDeclaration()\n  ) {\n    throw new Error(\"Only default and named export declarations can be split.\");\n  }\n\n  // build specifiers that point back to this export declaration\n\n  if (exportDeclaration.isExportDefaultDeclaration()) {\n    const declaration = exportDeclaration.get(\"declaration\");\n    const standaloneDeclaration =\n      declaration.isFunctionDeclaration() || declaration.isClassDeclaration();\n    const exportExpr =\n      declaration.isFunctionExpression() || declaration.isClassExpression();\n\n    const scope = declaration.isScope()\n      ? declaration.scope.parent\n      : declaration.scope;\n\n    // @ts-expect-error id is not defined in expressions other than function/class\n    let id = declaration.node.id;\n    let needBindingRegistration = false;\n\n    if (!id) {\n      needBindingRegistration = true;\n\n      id = scope.generateUidIdentifier(\"default\");\n\n      if (standaloneDeclaration || exportExpr) {\n        declaration.node.id = cloneNode(id);\n      }\n    } else if (exportExpr && scope.hasBinding(id.name)) {\n      needBindingRegistration = true;\n\n      id = scope.generateUidIdentifier(id.name);\n    }\n\n    const updatedDeclaration = standaloneDeclaration\n      ? declaration.node\n      : variableDeclaration(\"var\", [\n          variableDeclarator(\n            cloneNode(id),\n            // @ts-expect-error When `standaloneDeclaration` is false, declaration must not be a Function/ClassDeclaration\n            declaration.node,\n          ),\n        ]);\n\n    const updatedExportDeclaration = exportNamedDeclaration(null, [\n      exportSpecifier(cloneNode(id), identifier(\"default\")),\n    ]);\n\n    exportDeclaration.insertAfter(updatedExportDeclaration);\n    exportDeclaration.replaceWith(updatedDeclaration);\n\n    if (needBindingRegistration) {\n      scope.registerDeclaration(exportDeclaration);\n    }\n\n    return exportDeclaration;\n  } else if (\n    // @ts-expect-error TS can not narrow down to NodePath<t.ExportNamedDeclaration>\n    exportDeclaration.get(\"specifiers\").length > 0\n  ) {\n    throw new Error(\"It doesn't make sense to split exported specifiers.\");\n  }\n\n  const declaration = exportDeclaration.get(\"declaration\");\n  const bindingIdentifiers = declaration.getOuterBindingIdentifiers();\n\n  const specifiers = Object.keys(bindingIdentifiers).map(name => {\n    return exportSpecifier(identifier(name), identifier(name));\n  });\n\n  const aliasDeclar = exportNamedDeclaration(null, specifiers);\n\n  exportDeclaration.insertAfter(aliasDeclar);\n  exportDeclaration.replaceWith(declaration.node);\n  return exportDeclaration;\n}\n"],"mappings":";;;;;;AAAA,IAAAA,EAAA,GAAAC,OAAA;AAOsB;EANpBC,SAAS;EACTC,sBAAsB;EACtBC,eAAe;EACfC,UAAU;EACVC,mBAAmB;EACnBC;AAAkB,IAAAP,EAAA;AAKL,SAASQ,sBAAsBA,CAC5CC,iBAEC,EACD;EACA,IACE,CAACA,iBAAiB,CAACC,mBAAmB,CAAC,CAAC,IACxCD,iBAAiB,CAACE,sBAAsB,CAAC,CAAC,EAC1C;IACA,MAAM,IAAIC,KAAK,CAAC,0DAA0D,CAAC;EAC7E;EAIA,IAAIH,iBAAiB,CAACI,0BAA0B,CAAC,CAAC,EAAE;IAClD,MAAMC,WAAW,GAAG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