Added babel-helper-split-export-declaration (#7313)
This commit is contained in:
committed by
GitHub
parent
ea3f2d9299
commit
4d164bd8e6
76
packages/babel-helper-split-export-declaration/src/index.js
Normal file
76
packages/babel-helper-split-export-declaration/src/index.js
Normal file
@@ -0,0 +1,76 @@
|
||||
import * as t from "@babel/types";
|
||||
|
||||
export default function splitExportDeclaration(exportDeclaration) {
|
||||
if (!exportDeclaration.isExportDeclaration()) {
|
||||
throw new Error("Only export declarations can be splitted.");
|
||||
}
|
||||
|
||||
// build specifiers that point back to this export declaration
|
||||
const isDefault = exportDeclaration.isExportDefaultDeclaration();
|
||||
const declaration = exportDeclaration.get("declaration");
|
||||
const isClassDeclaration = declaration.isClassDeclaration();
|
||||
|
||||
if (isDefault) {
|
||||
const standaloneDeclaration =
|
||||
declaration.isFunctionDeclaration() || isClassDeclaration;
|
||||
|
||||
const scope = declaration.isScope()
|
||||
? declaration.scope.parent
|
||||
: declaration.scope;
|
||||
|
||||
let id = declaration.node.id;
|
||||
let needBindingRegistration = false;
|
||||
|
||||
if (!id) {
|
||||
needBindingRegistration = true;
|
||||
|
||||
id = scope.generateUidIdentifier("default");
|
||||
|
||||
if (
|
||||
standaloneDeclaration ||
|
||||
declaration.isFunctionExpression() ||
|
||||
declaration.isClassExpression()
|
||||
) {
|
||||
declaration.node.id = t.cloneNode(id);
|
||||
}
|
||||
}
|
||||
|
||||
const updatedDeclaration = standaloneDeclaration
|
||||
? declaration
|
||||
: t.variableDeclaration("var", [
|
||||
t.variableDeclarator(t.cloneNode(id), declaration.node),
|
||||
]);
|
||||
|
||||
const updatedExportDeclaration = t.exportNamedDeclaration(null, [
|
||||
t.exportSpecifier(t.cloneNode(id), t.identifier("default")),
|
||||
]);
|
||||
|
||||
exportDeclaration.insertAfter(updatedExportDeclaration);
|
||||
exportDeclaration.replaceWith(updatedDeclaration);
|
||||
|
||||
if (needBindingRegistration) {
|
||||
scope.registerBinding(
|
||||
isClassDeclaration ? "let" : "var",
|
||||
exportDeclaration,
|
||||
);
|
||||
}
|
||||
|
||||
return exportDeclaration;
|
||||
}
|
||||
|
||||
if (exportDeclaration.get("specifiers").length > 0) {
|
||||
throw new Error("It doesn't make sense to split exported specifiers.");
|
||||
}
|
||||
|
||||
const bindingIdentifiers = declaration.getOuterBindingIdentifiers();
|
||||
|
||||
const specifiers = Object.keys(bindingIdentifiers).map(name => {
|
||||
return t.exportSpecifier(t.identifier(name), t.identifier(name));
|
||||
});
|
||||
|
||||
const aliasDeclar = t.exportNamedDeclaration(null, specifiers);
|
||||
|
||||
exportDeclaration.insertAfter(aliasDeclar);
|
||||
exportDeclaration.replaceWith(declaration.node);
|
||||
return exportDeclaration;
|
||||
}
|
||||
Reference in New Issue
Block a user