index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. exports.get = get;
  7. exports.getDependencies = getDependencies;
  8. exports.list = void 0;
  9. exports.minVersion = minVersion;
  10. var _t = require("@babel/types");
  11. var _helpersGenerated = require("./helpers-generated.js");
  12. const {
  13. cloneNode,
  14. identifier
  15. } = _t;
  16. function deep(obj, path, value) {
  17. try {
  18. const parts = path.split(".");
  19. let last = parts.shift();
  20. while (parts.length > 0) {
  21. obj = obj[last];
  22. last = parts.shift();
  23. }
  24. if (arguments.length > 2) {
  25. obj[last] = value;
  26. } else {
  27. return obj[last];
  28. }
  29. } catch (e) {
  30. e.message += ` (when accessing ${path})`;
  31. throw e;
  32. }
  33. }
  34. function permuteHelperAST(ast, metadata, bindingName, localBindings, getDependency, adjustAst) {
  35. const {
  36. locals,
  37. dependencies,
  38. exportBindingAssignments,
  39. exportName
  40. } = metadata;
  41. const bindings = new Set(localBindings || []);
  42. if (bindingName) bindings.add(bindingName);
  43. for (const [name, paths] of (Object.entries || (o => Object.keys(o).map(k => [k, o[k]])))(locals)) {
  44. let newName = name;
  45. if (bindingName && name === exportName) {
  46. newName = bindingName;
  47. } else {
  48. while (bindings.has(newName)) newName = "_" + newName;
  49. }
  50. if (newName !== name) {
  51. for (const path of paths) {
  52. deep(ast, path, identifier(newName));
  53. }
  54. }
  55. }
  56. for (const [name, paths] of (Object.entries || (o => Object.keys(o).map(k => [k, o[k]])))(dependencies)) {
  57. const ref = typeof getDependency === "function" && getDependency(name) || identifier(name);
  58. for (const path of paths) {
  59. deep(ast, path, cloneNode(ref));
  60. }
  61. }
  62. adjustAst == null || adjustAst(ast, exportName, map => {
  63. exportBindingAssignments.forEach(p => deep(ast, p, map(deep(ast, p))));
  64. });
  65. }
  66. const helperData = Object.create(null);
  67. function loadHelper(name) {
  68. if (!helperData[name]) {
  69. const helper = _helpersGenerated.default[name];
  70. if (!helper) {
  71. throw Object.assign(new ReferenceError(`Unknown helper ${name}`), {
  72. code: "BABEL_HELPER_UNKNOWN",
  73. helper: name
  74. });
  75. }
  76. helperData[name] = {
  77. minVersion: helper.minVersion,
  78. build(getDependency, bindingName, localBindings, adjustAst) {
  79. const ast = helper.ast();
  80. permuteHelperAST(ast, helper.metadata, bindingName, localBindings, getDependency, adjustAst);
  81. return {
  82. nodes: ast.body,
  83. globals: helper.metadata.globals
  84. };
  85. },
  86. getDependencies() {
  87. return Object.keys(helper.metadata.dependencies);
  88. }
  89. };
  90. }
  91. return helperData[name];
  92. }
  93. function get(name, getDependency, bindingName, localBindings, adjustAst) {
  94. {
  95. if (typeof bindingName === "object") {
  96. const id = bindingName;
  97. if ((id == null ? void 0 : id.type) === "Identifier") {
  98. bindingName = id.name;
  99. } else {
  100. bindingName = undefined;
  101. }
  102. }
  103. }
  104. return loadHelper(name).build(getDependency, bindingName, localBindings, adjustAst);
  105. }
  106. function minVersion(name) {
  107. return loadHelper(name).minVersion;
  108. }
  109. function getDependencies(name) {
  110. return loadHelper(name).getDependencies();
  111. }
  112. {
  113. exports.ensure = name => {
  114. loadHelper(name);
  115. };
  116. }
  117. const list = exports.list = Object.keys(_helpersGenerated.default).map(name => name.replace(/^_/, ""));
  118. var _default = exports.default = get;
  119. //# sourceMappingURL=index.js.map