@storm-stack/core
Version:
A build toolkit and runtime used by Storm Software in TypeScript applications
1 lines • 7.38 kB
Source Map (JSON)
{"version":3,"sources":["../../src/lib/babel/module.ts"],"names":["findExport","ast","key","type","node","program","body","declaration","declarations","id","name","init","listExports","codeOrAst","isString","parseAst","flatMap","i","map","d","filter","Boolean","listImports","specifiers","s","imported","value","isImportCall","calleePath","isImport","callee","getImport","specifier","named","importDeclaration","importSpecifier","identifier","stringLiteral"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AA+BO,SAASA,UAAAA,CAAWC,KAA0BC,GAAAA,EAAW;AAC9D,EAAA,MAAMC,IAAAA,GACJD,GAAAA,KAAQ,SAAA,GAAY,0BAAA,GAA6B,wBAAA;AAEnD,EAAA,KAAA,MAAWE,IAAAA,IAAQH,GAAAA,CAAII,OAAAA,CAAQC,IAAAA,EAAM;AACnC,IAAA,IAAIF,IAAAA,CAAKD,SAASA,IAAAA,EAAM;AACtB,MAAA,IAAID,QAAQ,SAAA,EAAW;AACrB,QAAA,OAAOE,IAAAA,CAAKG,WAAAA;AACd,MAAA;AACA,MAAA,IAAIH,IAAAA,CAAKG,WAAAA,IAAe,cAAA,IAAkBH,IAAAA,CAAKG,WAAAA,EAAa;AAC1D,QAAA,MAAMA,WAAAA,GAAcH,IAAAA,CAAKG,WAAAA,CAAYC,YAAAA,CAAa,CAAA,CAAA;AAClD,QAAA,IACED,eACA,MAAA,IAAUA,WAAAA,CAAYE,MACtBF,WAAAA,CAAYE,EAAAA,CAAGC,SAASR,GAAAA,EACxB;AACA,UAAA,OAAOK,WAAAA,CAAYI,IAAAA;AACrB,QAAA;AACF,MAAA;AACF,IAAA;AACF,EAAA;AACF;AArBgBX,wBAAAA,CAAAA,UAAAA,EAAAA,YAAAA,CAAAA;AA6BT,SAASY,YAAYC,SAAAA,EAAuC;AACjE,EAAA,MAAMZ,MAAMa,iBAAAA,CAASD,SAAAA,CAAAA,GAAaE,0BAAAA,CAASF,SAAAA,CAAAA,GAAaA,S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,QAAA,OAAO,EAAA;MACT,CAAA,CAAA;AACF,IAAA;AAEA,IAAA,OAAO,EAAA;EACT,CAAA,CAAA,CACCN,OAAOC,OAAAA,CAAAA;AACZ;AApBgBC,wBAAAA,CAAAA,WAAAA,EAAAA,aAAAA,CAAAA;AAsBT,SAASK,aACdC,UAAAA,EAAwD;AAExD,EAAA,OAASC,YAAAA,CAAAA,QAAAA,CAASD,UAAAA,CAAWxB,IAAAA,CAAK0B,MAAM,CAAA;AAC1C;AAJgBH,wBAAAA,CAAAA,YAAAA,EAAAA,cAAAA,CAAAA;AAcT,SAASI,SAAAA,CACdC,SAAAA,EACAtB,IAAAA,EACAuB,KAAAA,EAAc;AAEd,EAAA,OAASC,YAAAA,CAAAA,iBAAAA,CACP;AAAGC,IAAAA,YAAAA,CAAAA,eAAAA,CAAkBC,wBAAW1B,IAAAA,CAAAA,EAAS2B,YAAAA,CAAAA,aAAAA,CAAcJ,KAAAA,IAASvB,IAAAA,CAAAA;AAC9D2B,GAAAA,EAAAA,YAAAA,CAAAA,aAAAA,CAAcL,SAAAA,CAAAA,CAAAA;AAEpB;AATgBD,wBAAAA,CAAAA,SAAAA,EAAAA,WAAAA,CAAAA","file":"chunk-SQHGN4CZ.cjs","sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Storm Stack\n\n This code was released as part of the Storm Stack project. Storm Stack\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/storm-stack.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/storm-stack\n Documentation: https://docs.stormsoftware.com/projects/storm-stack\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { NodePath } from \"@babel/core\";\nimport { ParseResult } from \"@babel/parser\";\nimport * as t from \"@babel/types\";\nimport { isString } from \"@stryke/type-checks/is-string\";\nimport { parseAst } from \"./ast\";\n\n/**\n * Finds an export in the given Babel AST program by its key.\n *\n * @param ast - The parsed Babel AST result containing the program body.\n * @param key - The name of the export to find (e.g., \"default\" or a named export).\n * @returns The declaration of the export if found, otherwise undefined.\n */\nexport function findExport(ast: ParseResult<t.File>, key: string) {\n const type =\n key === \"default\" ? \"ExportDefaultDeclaration\" : \"ExportNamedDeclaration\";\n\n for (const node of ast.program.body) {\n if (node.type === type) {\n if (key === \"default\") {\n return node.declaration;\n }\n if (node.declaration && \"declarations\" in node.declaration) {\n const declaration = node.declaration.declarations[0];\n if (\n declaration &&\n \"name\" in declaration.id &&\n declaration.id.name === key\n ) {\n return declaration.init as any;\n }\n }\n }\n }\n}\n\n/**\n * Lists all exports from the given Babel AST program.\n *\n * @param codeOrAst - The parsed Babel AST result containing the program body.\n * @returns An array of export names, including \"default\" for default exports.\n */\nexport function listExports(codeOrAst: ParseResult<t.File> | string) {\n const ast = isString(codeOrAst) ? parseAst(codeOrAst) : codeOrAst;\n\n return ast.program.body\n .flatMap(i => {\n if (i.type === \"ExportDefaultDeclaration\") {\n return [\"default\"];\n }\n if (\n i.type === \"ExportNamedDeclaration\" &&\n i.declaration &&\n \"declarations\" in i.declaration\n ) {\n return i.declaration.declarations.map(d =>\n \"name\" in d.id ? d.id.name : \"\"\n );\n }\n return [];\n })\n .filter(Boolean);\n}\n\n/**\n * Lists all imports from the given Babel AST program.\n *\n * @param ast - The parsed Babel AST result containing the program body.\n * @returns An array of import names, including \"default\" for default imports.\n */\nexport function listImports(ast: ParseResult<t.File> | t.File) {\n return ast.program.body\n .flatMap(i => {\n if (i.type === \"ImportDeclaration\") {\n return i.specifiers.map(s => {\n if (s.type === \"ImportDefaultSpecifier\") {\n return \"default\";\n }\n if (s.type === \"ImportSpecifier\" && \"imported\" in s) {\n return s.imported.type === \"Identifier\"\n ? s.imported.name\n : s.imported.value;\n }\n return \"\";\n });\n }\n\n return [];\n })\n .filter(Boolean);\n}\n\nexport function isImportCall(\n calleePath: NodePath<t.CallExpression | t.NewExpression>\n) {\n return t.isImport(calleePath.node.callee);\n}\n\n/**\n * Gets the import declaration for a given name and specifier.\n *\n * @param specifier - The specifier of the import.\n * @param name - The name of the import.\n * @param named - Optional named import.\n * @returns The import declaration.\n */\nexport function getImport(\n specifier: string,\n name: string,\n named?: string\n): t.ImportDeclaration {\n return t.importDeclaration(\n [t.importSpecifier(t.identifier(name), t.stringLiteral(named || name))],\n t.stringLiteral(specifier)\n );\n}\n"]}