babel-plugin-transform-fs-promises
Version:
A Babel plugin to transform fs promises APIs
163 lines (162 loc) • 8.51 kB
JavaScript
;
var __webpack_require__ = {};
!function() {
__webpack_require__.d = function(exports1, definition) {
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
enumerable: true,
get: definition[key]
});
};
}();
!function() {
__webpack_require__.o = function(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
};
}();
!function() {
__webpack_require__.r = function(exports1) {
if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
value: 'Module'
});
Object.defineProperty(exports1, '__esModule', {
value: true
});
};
}();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
babelPluginTransformFsPromises: function() {
return babelPluginTransformFsPromises;
}
});
function babelPluginTransformFsPromises(babel) {
var t = babel.types;
var moduleNames = new Set([
"fs/promises",
"node:fs/promises"
]);
var generateImportSourceStringLiteral = function(source) {
var moduleName = source.replace(/\/promises$/, "").replace(/^node:/, "");
return t.stringLiteral(moduleName);
};
return {
name: "transform-fs-promises",
visitor: {
CallExpression: function(path) {
var node = path.node;
if ((t.isIdentifier(node.callee, {
name: "require"
}) || t.isMemberExpression(node.callee) && t.isIdentifier(node.callee.object, {
name: "module"
}) && t.isIdentifier(node.callee.property, {
name: "require"
})) && 1 === node["arguments"].length && t.isStringLiteral(node["arguments"][0]) && moduleNames.has(node["arguments"][0].value)) path.replaceWith(t.memberExpression(t.callExpression(node.callee, [
generateImportSourceStringLiteral(node["arguments"][0].value)
]), t.identifier("promises")));
if ((t.isImport(node.callee) || t.isIdentifier(node.callee, {
name: "import"
})) && 1 === node["arguments"].length && t.isStringLiteral(node["arguments"][0]) && moduleNames.has(node["arguments"][0].value)) {
var internalIdentifier = path.scope.generateUidIdentifier("esModule");
path.replaceWith(t.callExpression(t.memberExpression(t.callExpression(t.identifier("import"), [
generateImportSourceStringLiteral(node["arguments"][0].value)
]), t.identifier("then")), [
t.arrowFunctionExpression([
internalIdentifier
], t.memberExpression(internalIdentifier, t.identifier("promises")))
]));
}
},
ImportDeclaration: function(path) {
var node = path.node;
if (moduleNames.has(node.source.value) && 1 === node.specifiers.length && t.isImportDefaultSpecifier(node.specifiers[0])) {
var localName = node.specifiers[0].local.name;
path.replaceWith(t.importDeclaration([
t.importSpecifier(t.identifier(localName), t.identifier("promises"))
], generateImportSourceStringLiteral(node.source.value)));
}
if (moduleNames.has(node.source.value) && node.specifiers.some(function(spec) {
return t.isImportSpecifier(spec);
})) {
var namesSpecifiers = node.specifiers.filter(function(spec) {
return t.isImportSpecifier(spec);
});
var defaultSpec = node.specifiers.find(function(spec) {
return t.isImportDefaultSpecifier(spec);
});
var localPromises = path.scope.generateUidIdentifier("promises");
path.replaceWithMultiple([
t.importDeclaration([
defaultSpec,
t.importSpecifier(localPromises, t.identifier("promises"))
].filter(Boolean), generateImportSourceStringLiteral(node.source.value)),
t.variableDeclaration("const", [
t.variableDeclarator(t.objectPattern(namesSpecifiers.map(function(spec) {
return t.objectProperty(spec.local, spec.imported, false, true);
})), localPromises)
])
]);
}
if (moduleNames.has(node.source.value) && 1 === node.specifiers.length && t.isImportNamespaceSpecifier(node.specifiers[0])) path.replaceWith(t.importDeclaration([
t.importSpecifier(node.specifiers[0].local, t.identifier("promises"))
], generateImportSourceStringLiteral(node.source.value)));
},
ExportAllDeclaration: function(path) {
var node = path.node;
if (moduleNames.has(node.source.value)) {
var localPromises = path.scope.generateUidIdentifier("promises");
path.replaceWithMultiple([
t.importDeclaration([
t.importSpecifier(localPromises, t.identifier("promises"))
], generateImportSourceStringLiteral(node.source.value)),
t.exportDefaultDeclaration(localPromises)
]);
}
},
ExportNamedDeclaration: function(path) {
var _node_source;
var node = path.node;
if (moduleNames.has(null === (_node_source = node.source) || void 0 === _node_source ? void 0 : _node_source.value) && node.specifiers.some(function(spec) {
return t.isExportSpecifier(spec);
})) {
var namesSpecifiers = node.specifiers.filter(function(spec) {
return t.isExportSpecifier(spec);
});
var defaultSpec = node.specifiers.find(function(spec) {
return t.isExportDefaultSpecifier(spec);
});
var localPromises = path.scope.generateUidIdentifier("promises");
var specUidIdentifierMap = new Map();
path.replaceWithMultiple([
t.importDeclaration([
defaultSpec ? t.importDefaultSpecifier(t.identifier(defaultSpec.exported.name)) : void 0,
t.importSpecifier(localPromises, t.identifier("promises"))
].filter(Boolean), generateImportSourceStringLiteral(node.source.value)),
t.variableDeclaration("const", [
t.variableDeclarator(t.objectPattern(namesSpecifiers.map(function(spec) {
var UidIdentifier = path.scope.generateUidIdentifier(spec.local.name);
specUidIdentifierMap.set(spec, UidIdentifier);
return t.objectProperty(spec.local, UidIdentifier, false, false);
})), localPromises)
]),
t.exportNamedDeclaration(null, namesSpecifiers.map(function(spec) {
return t.exportSpecifier(specUidIdentifierMap.get(spec), spec.local);
})),
defaultSpec ? defaultSpec : null
].filter(Boolean));
}
}
}
};
}
__webpack_exports__["default"] = babelPluginTransformFsPromises;
exports.babelPluginTransformFsPromises = __webpack_exports__.babelPluginTransformFsPromises;
exports["default"] = __webpack_exports__["default"];
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
"babelPluginTransformFsPromises",
"default"
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
Object.defineProperty(exports, '__esModule', {
value: true
});
//# sourceMappingURL=index.js.map