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.

61 lines
3.0 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = splitExportDeclaration;
  6. var t = _interopRequireWildcard(require("@babel/types"));
  7. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  8. 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; }
  9. function splitExportDeclaration(exportDeclaration) {
  10. if (!exportDeclaration.isExportDeclaration()) {
  11. throw new Error("Only export declarations can be splitted.");
  12. }
  13. const isDefault = exportDeclaration.isExportDefaultDeclaration();
  14. const declaration = exportDeclaration.get("declaration");
  15. const isClassDeclaration = declaration.isClassDeclaration();
  16. if (isDefault) {
  17. const standaloneDeclaration = declaration.isFunctionDeclaration() || isClassDeclaration;
  18. const scope = declaration.isScope() ? declaration.scope.parent : declaration.scope;
  19. let id = declaration.node.id;
  20. let needBindingRegistration = false;
  21. if (!id) {
  22. needBindingRegistration = true;
  23. id = scope.generateUidIdentifier("default");
  24. if (standaloneDeclaration || declaration.isFunctionExpression() || declaration.isClassExpression()) {
  25. declaration.node.id = t.cloneNode(id);
  26. }
  27. }
  28. const updatedDeclaration = standaloneDeclaration ? declaration : t.variableDeclaration("var", [t.variableDeclarator(t.cloneNode(id), declaration.node)]);
  29. const updatedExportDeclaration = t.exportNamedDeclaration(null, [t.exportSpecifier(t.cloneNode(id), t.identifier("default"))]);
  30. exportDeclaration.insertAfter(updatedExportDeclaration);
  31. exportDeclaration.replaceWith(updatedDeclaration);
  32. if (needBindingRegistration) {
  33. scope.registerDeclaration(exportDeclaration);
  34. }
  35. return exportDeclaration;
  36. }
  37. if (exportDeclaration.get("specifiers").length > 0) {
  38. throw new Error("It doesn't make sense to split exported specifiers.");
  39. }
  40. const bindingIdentifiers = declaration.getOuterBindingIdentifiers();
  41. const specifiers = Object.keys(bindingIdentifiers).map(name => {
  42. return t.exportSpecifier(t.identifier(name), t.identifier(name));
  43. });
  44. const aliasDeclar = t.exportNamedDeclaration(null, specifiers);
  45. exportDeclaration.insertAfter(aliasDeclar);
  46. exportDeclaration.replaceWith(declaration.node);
  47. return exportDeclaration;
  48. }