UNPKG

babel-plugin-transform-fs-promises

Version:
1 lines 14.7 kB
{"version":3,"file":"esm/index.mjs","sources":["webpack://babel-plugin-transform-fs-promises/./src/index.ts"],"sourcesContent":["import type Babel from \"@babel/core\";\nimport type { PluginObj } from \"@babel/core\";\n\nfunction babelPluginTransformFsPromises(babel: typeof Babel) {\n const { types: t } = babel;\n\n const moduleNames = new Set([\"fs/promises\", \"node:fs/promises\"]);\n\n const generateImportSourceStringLiteral = (source: string) => {\n const moduleName = source.replace(/\\/promises$/, \"\").replace(/^node:/, \"\");\n return t.stringLiteral(moduleName);\n };\n\n return {\n name: \"transform-fs-promises\",\n visitor: {\n CallExpression(path) {\n const { node } = path;\n\n // 1. 处理 require('fs/promises') → require('fs').promises\n if (\n // require('xxx')\n (t.isIdentifier(node.callee, { name: \"require\" }) ||\n // module.require('xxx')\n (t.isMemberExpression(node.callee) &&\n t.isIdentifier(node.callee.object, { name: \"module\" }) &&\n t.isIdentifier(node.callee.property, { name: \"require\" }))) &&\n node.arguments.length === 1 &&\n t.isStringLiteral(node.arguments[0]) &&\n moduleNames.has(node.arguments[0].value)\n ) {\n path.replaceWith(\n t.memberExpression(\n t.callExpression(node.callee, [generateImportSourceStringLiteral(node.arguments[0].value)]),\n t.identifier(\"promises\"),\n ),\n );\n }\n\n // 2. 处理动态导入 await import('fs/promises') → await import('fs').then(m => m.promises)\n if (\n (t.isImport(node.callee) || t.isIdentifier(node.callee, { name: \"import\" })) &&\n node.arguments.length === 1 &&\n t.isStringLiteral(node.arguments[0]) &&\n moduleNames.has(node.arguments[0].value)\n ) {\n const internalIdentifier = path.scope.generateUidIdentifier(\"esModule\");\n\n path.replaceWith(\n t.callExpression(\n t.memberExpression(\n t.callExpression(t.identifier(\"import\"), [generateImportSourceStringLiteral(node.arguments[0].value)]),\n t.identifier(\"then\"),\n ),\n [\n t.arrowFunctionExpression(\n [internalIdentifier],\n t.memberExpression(internalIdentifier, t.identifier(\"promises\")),\n ),\n ],\n ),\n );\n }\n },\n\n ImportDeclaration(path) {\n const { node } = path;\n\n // 1. 处理 import fs from 'fs/promises' → import { promises as fs } from 'fs'\n if (moduleNames.has(node.source.value) && node.specifiers.length === 1 && t.isImportDefaultSpecifier(node.specifiers[0])) {\n const localName = node.specifiers[0].local.name;\n path.replaceWith(\n t.importDeclaration(\n [t.importSpecifier(t.identifier(localName), t.identifier(\"promises\"))],\n generateImportSourceStringLiteral(node.source.value),\n ),\n );\n }\n\n // 2. 处理 import { readFile, stat } from 'fs/promises' → import { promises: _promises } from 'fs';\\nconst { readFile, stat } = _promises;\n if (moduleNames.has(node.source.value) && node.specifiers.some((spec) => t.isImportSpecifier(spec))) {\n const namesSpecifiers = node.specifiers.filter((spec) => t.isImportSpecifier(spec));\n\n const defaultSpec = node.specifiers.find((spec) => t.isImportDefaultSpecifier(spec));\n\n const localPromises = path.scope.generateUidIdentifier(\"promises\");\n\n path.replaceWithMultiple([\n t.importDeclaration(\n [defaultSpec, t.importSpecifier(localPromises, t.identifier(\"promises\"))].filter(Boolean),\n generateImportSourceStringLiteral(node.source.value),\n ),\n t.variableDeclaration(\"const\", [\n t.variableDeclarator(\n t.objectPattern(\n namesSpecifiers.map((spec) => {\n const importedKey = t.isIdentifier(spec.imported)\n ? spec.imported\n : t.identifier(spec.imported.value);\n const isShorthand =\n t.isIdentifier(spec.imported) && spec.imported.name === spec.local.name;\n return t.objectProperty(importedKey, spec.local, false, isShorthand);\n }),\n ),\n localPromises,\n ),\n ]),\n ]);\n }\n\n // 3. 处理 import * as fs from 'fs/promises' → import { promises as fs } from 'fs'\n if (\n moduleNames.has(node.source.value) &&\n node.specifiers.length === 1 &&\n t.isImportNamespaceSpecifier(node.specifiers[0])\n ) {\n path.replaceWith(\n t.importDeclaration(\n [t.importSpecifier(node.specifiers[0].local, t.identifier(\"promises\"))],\n generateImportSourceStringLiteral(node.source.value),\n ),\n );\n }\n },\n ExportAllDeclaration(path) {\n const { node } = path;\n\n // 1. 处理 export * from 'fs/promises' → export * from 'fs'\n if (moduleNames.has(node.source.value)) {\n const localPromises = path.scope.generateUidIdentifier(\"promises\");\n\n path.replaceWithMultiple([\n t.importDeclaration(\n [t.importSpecifier(localPromises, t.identifier(\"promises\"))],\n generateImportSourceStringLiteral(node.source.value),\n ),\n t.exportDefaultDeclaration(localPromises),\n ]);\n }\n },\n\n ExportNamedDeclaration(path) {\n const { node } = path;\n\n // 1. 处理 export { readFile } from 'fs/promises' → import { promises as _promises } from 'fs'\\nexport { readFile } from _promises;\n if (moduleNames.has(node.source?.value) && node.specifiers.some((spec) => t.isExportSpecifier(spec))) {\n const namesSpecifiers = node.specifiers.filter((spec) => t.isExportSpecifier(spec));\n\n const defaultSpec = node.specifiers.find((spec) => t.isExportDefaultSpecifier(spec));\n\n const localPromises = path.scope.generateUidIdentifier(\"promises\");\n\n const specUidIdentifierMap = new Map<Babel.types.ExportSpecifier, Babel.types.Identifier>();\n\n path.replaceWithMultiple(\n [\n t.importDeclaration(\n [\n defaultSpec ? t.importDefaultSpecifier(t.identifier(defaultSpec.exported.name)) : undefined,\n t.importSpecifier(localPromises, t.identifier(\"promises\")),\n ].filter(Boolean),\n generateImportSourceStringLiteral(node.source.value),\n ),\n t.variableDeclaration(\"const\", [\n t.variableDeclarator(\n t.objectPattern(\n namesSpecifiers.map((spec) => {\n const UidIdentifier = path.scope.generateUidIdentifier(spec.local.name);\n\n specUidIdentifierMap.set(spec, UidIdentifier);\n\n return t.objectProperty(spec.local, UidIdentifier, false, false);\n }),\n ),\n localPromises,\n ),\n ]),\n t.exportNamedDeclaration(\n null,\n namesSpecifiers.map((spec) => t.exportSpecifier(specUidIdentifierMap.get(spec), spec.exported)),\n ),\n defaultSpec ? defaultSpec : null,\n ].filter(Boolean),\n );\n }\n },\n },\n } satisfies PluginObj<Babel.PluginPass>;\n}\n\nexport { babelPluginTransformFsPromises };\nexport default babelPluginTransformFsPromises;\n"],"names":["babelPluginTransformFsPromises","babel","t","moduleNames","Set","generateImportSourceStringLiteral","source","moduleName","path","node","internalIdentifier","localName","spec","namesSpecifiers","defaultSpec","localPromises","Boolean","importedKey","isShorthand","_node_source","specUidIdentifierMap","Map","undefined","UidIdentifier"],"mappings":"AAGA,SAASA,+BAA+BC,KAAmB;IACvD,IAAeC,IAAMD,MAAb,KAAK;IAEb,IAAME,cAAc,IAAIC,IAAI;QAAC;QAAe;KAAmB;IAE/D,IAAMC,oCAAoC,SAACC,MAAM;QAC7C,IAAMC,aAAaD,OAAO,OAAO,CAAC,eAAe,IAAI,OAAO,CAAC,UAAU;QACvE,OAAOJ,EAAE,aAAa,CAACK;IAC3B;IAEA,OAAO;QACH,MAAM;QACN,SAAS;YACL,yBAAeC,IAAI;gBACf,IAAQC,OAASD,KAATC,IAAI;gBAGZ,IAEKP,AAAAA,CAAAA,EAAE,YAAY,CAACO,KAAK,MAAM,EAAE;oBAAE,MAAM;gBAAU,MAE1CP,EAAE,kBAAkB,CAACO,KAAK,MAAM,KAC7BP,EAAE,YAAY,CAACO,KAAK,MAAM,CAAC,MAAM,EAAE;oBAAE,MAAM;gBAAS,MACpDP,EAAE,YAAY,CAACO,KAAK,MAAM,CAAC,QAAQ,EAAE;oBAAE,MAAM;gBAAU,EAAC,KAChEA,AAA0B,MAA1BA,IAAK,aAAS,CAAC,MAAM,IACrBP,EAAE,eAAe,CAACO,IAAK,aAAS,CAAC,EAAE,KACnCN,YAAY,GAAG,CAACM,IAAK,aAAS,CAAC,EAAE,CAAC,KAAK,GAEvCD,KAAK,WAAW,CACZN,EAAE,gBAAgB,CACdA,EAAE,cAAc,CAACO,KAAK,MAAM,EAAE;oBAACJ,kCAAkCI,IAAK,aAAS,CAAC,EAAE,CAAC,KAAK;iBAAE,GAC1FP,EAAE,UAAU,CAAC;gBAMzB,IACKA,AAAAA,CAAAA,EAAE,QAAQ,CAACO,KAAK,MAAM,KAAKP,EAAE,YAAY,CAACO,KAAK,MAAM,EAAE;oBAAE,MAAM;gBAAS,EAAC,KAC1EA,AAA0B,MAA1BA,IAAK,aAAS,CA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