You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
3.0 KiB
62 lines
3.0 KiB
4 years ago
|
"use strict";
|
||
|
|
||
|
Object.defineProperty(exports, "__esModule", {
|
||
|
value: true
|
||
|
});
|
||
|
exports.default = splitExportDeclaration;
|
||
|
|
||
|
var t = _interopRequireWildcard(require("@babel/types"));
|
||
|
|
||
|
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
||
|
|
||
|
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
||
|
|
||
|
function splitExportDeclaration(exportDeclaration) {
|
||
|
if (!exportDeclaration.isExportDeclaration()) {
|
||
|
throw new Error("Only export declarations can be split.");
|
||
|
}
|
||
|
|
||
|
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.registerDeclaration(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;
|
||
|
}
|