1 line
20 KiB
Plaintext
1 line
20 KiB
Plaintext
|
{"version":3,"names":["_traverse","require","_t","_helpers","assignmentExpression","cloneNode","expressionStatement","file","identifier","makePath","path","parts","parentPath","push","key","inList","listKey","reverse","join","FileClass","undefined","getHelperMetadata","globals","Set","localBindingNames","dependencies","Map","exportName","exportPath","exportBindingAssignments","importPaths","importBindingsReferences","dependencyVisitor","ImportDeclaration","child","name","node","source","value","helpers","buildCodeFrameError","get","length","isImportDefaultSpecifier","bindingIdentifier","specifiers","local","set","ExportDefaultDeclaration","decl","isFunctionDeclaration","id","ExportAllDeclaration","ExportNamedDeclaration","Statement","isImportDeclaration","isExportDeclaration","skip","referenceVisitor","Program","bindings","scope","getAllBindings","Object","keys","forEach","has","add","ReferencedIdentifier","binding","getBinding","isProgram","AssignmentExpression","left","getBindingIdentifiers","isIdentifier","traverse","ast","Error","Array","from","permuteHelperAST","metadata","localBindings","getDependency","dependenciesRefs","toRename","type","newName","exp","imps","map","p","impsBindingRefs","replaceWith","assignPath","assign","pushContainer","rename","remove","helperData","create","loadHelper","helper","ReferenceError","code","fn","fakeFile","stop","filename","inputMap","minVersion","build","nodes","program","body","getDependencies","values","ensure","newFileClass","list","exports","replace","_default","default"],"sources":["../src/index.ts"],"sourcesContent":["import type { File } from \"@babel/core\";\nimport type { NodePath, Visitor } from \"@babel/traverse\";\nimport traverse from \"@babel/traverse\";\nimport {\n assignmentExpression,\n cloneNode,\n expressionStatement,\n file,\n identifier,\n} from \"@babel/types\";\nimport type * as t from \"@babel/types\";\nimport helpers from \"./helpers.ts\";\n\nfunction makePath(path: NodePath) {\n const parts = [];\n\n for (; path.parentPath; path = path.parentPath) {\n parts.push(path.key);\n if (path.inList) parts.push(path.listKey);\n }\n\n return parts.reverse().join(\".\");\n}\n\nlet FileClass: typeof File | undefined = undefined;\n\ninterface HelperMetadata {\n globals: string[];\n localBindingNames: string[];\n dependencies: Map<t.Identifier, string>;\n exportBindingAssignments: string[];\n exportPath: string;\n exportName: string;\n importBindingsReferences: string[];\n importPaths: string[];\n}\n\n/**\n * Given a file AST for a given helper, get a bunch of metadata about it so that Babel can quickly render\n * the helper is whatever context it is needed in.\n */\nfunction getHelperMetadata(file: File): HelperMetadata {\n const globals = new Set<string>();\n const localBindingNames = new Set<string>();\n // Maps imported identifier -> helper name\n const dependencies = new Map<t.Identifier, string>();\n\n let exportName: string | undefined;\n let exportPath: string | undefined;\n const exportBindingAssignments: string[] = [];\n const importPaths: string[] = [];\n const importBindingsReferences: string[] = [];\n\n const dependencyVisitor: Visitor = {\n ImportDeclaration(child) {\n const name = child.node.source.value;\n if (!helpers[name]) {\n throw child.buildCodeFrameError(`Unknown helper ${name}`);\n }\n if (\n child.get(\"specifiers\").length !== 1 ||\n // @ts-expect-error isImportDefaultSpecifier does not work with NodePath union\n !child.get(\"specifiers.0\").isImportDefaultSpecifier()\n ) {\n throw child.buildCodeFrameError(\n \"Helpers can only import a default value\",\n );\n }\n const bindingIdentifier = child.node.specifiers[0].local;\n dependencies.set(bindingIdentifier, name);\n importPaths.push(makePath(child));\n },\n ExportDefaultDeclaration(child) {\n const decl = child.get(\"declaration\");\n\n if (!decl.isFunctionDeclaration() || !decl.node.id) {\n throw decl.buildCodeFrameError(\n \"H
|