1 line
6.0 KiB
Plaintext
1 line
6.0 KiB
Plaintext
|
{"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,CAC5C
|