utils.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.createUtilsGetter = createUtilsGetter;
  4. exports.getImportSource = getImportSource;
  5. exports.getRequireSource = getRequireSource;
  6. exports.has = has;
  7. exports.intersection = intersection;
  8. exports.resolveKey = resolveKey;
  9. exports.resolveSource = resolveSource;
  10. var _babel = _interopRequireWildcard(require("@babel/core"));
  11. function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
  12. function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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; }
  13. const {
  14. types: t,
  15. template: template
  16. } = _babel.default || _babel;
  17. function intersection(a, b) {
  18. const result = new Set();
  19. a.forEach(v => b.has(v) && result.add(v));
  20. return result;
  21. }
  22. function has(object, key) {
  23. return Object.prototype.hasOwnProperty.call(object, key);
  24. }
  25. function getType(target) {
  26. return Object.prototype.toString.call(target).slice(8, -1);
  27. }
  28. function resolveId(path) {
  29. if (path.isIdentifier() && !path.scope.hasBinding(path.node.name, /* noGlobals */true)) {
  30. return path.node.name;
  31. }
  32. const {
  33. deopt
  34. } = path.evaluate();
  35. if (deopt && deopt.isIdentifier()) {
  36. return deopt.node.name;
  37. }
  38. }
  39. function resolveKey(path, computed = false) {
  40. const {
  41. scope
  42. } = path;
  43. if (path.isStringLiteral()) return path.node.value;
  44. const isIdentifier = path.isIdentifier();
  45. if (isIdentifier && !(computed || path.parent.computed)) {
  46. return path.node.name;
  47. }
  48. if (computed && path.isMemberExpression() && path.get("object").isIdentifier({
  49. name: "Symbol"
  50. }) && !scope.hasBinding("Symbol", /* noGlobals */true)) {
  51. const sym = resolveKey(path.get("property"), path.node.computed);
  52. if (sym) return "Symbol." + sym;
  53. }
  54. if (!isIdentifier || scope.hasBinding(path.node.name, /* noGlobals */true)) {
  55. const {
  56. value
  57. } = path.evaluate();
  58. if (typeof value === "string") return value;
  59. }
  60. }
  61. function resolveSource(obj) {
  62. if (obj.isMemberExpression() && obj.get("property").isIdentifier({
  63. name: "prototype"
  64. })) {
  65. const id = resolveId(obj.get("object"));
  66. if (id) {
  67. return {
  68. id,
  69. placement: "prototype"
  70. };
  71. }
  72. return {
  73. id: null,
  74. placement: null
  75. };
  76. }
  77. const id = resolveId(obj);
  78. if (id) {
  79. return {
  80. id,
  81. placement: "static"
  82. };
  83. }
  84. const {
  85. value
  86. } = obj.evaluate();
  87. if (value !== undefined) {
  88. return {
  89. id: getType(value),
  90. placement: "prototype"
  91. };
  92. } else if (obj.isRegExpLiteral()) {
  93. return {
  94. id: "RegExp",
  95. placement: "prototype"
  96. };
  97. } else if (obj.isFunction()) {
  98. return {
  99. id: "Function",
  100. placement: "prototype"
  101. };
  102. }
  103. return {
  104. id: null,
  105. placement: null
  106. };
  107. }
  108. function getImportSource({
  109. node
  110. }) {
  111. if (node.specifiers.length === 0) return node.source.value;
  112. }
  113. function getRequireSource({
  114. node
  115. }) {
  116. if (!t.isExpressionStatement(node)) return;
  117. const {
  118. expression
  119. } = node;
  120. if (t.isCallExpression(expression) && t.isIdentifier(expression.callee) && expression.callee.name === "require" && expression.arguments.length === 1 && t.isStringLiteral(expression.arguments[0])) {
  121. return expression.arguments[0].value;
  122. }
  123. }
  124. function hoist(node) {
  125. // @ts-expect-error
  126. node._blockHoist = 3;
  127. return node;
  128. }
  129. function createUtilsGetter(cache) {
  130. return path => {
  131. const prog = path.findParent(p => p.isProgram());
  132. return {
  133. injectGlobalImport(url) {
  134. cache.storeAnonymous(prog, url, (isScript, source) => {
  135. return isScript ? template.statement.ast`require(${source})` : t.importDeclaration([], source);
  136. });
  137. },
  138. injectNamedImport(url, name, hint = name) {
  139. return cache.storeNamed(prog, url, name, (isScript, source, name) => {
  140. const id = prog.scope.generateUidIdentifier(hint);
  141. return {
  142. node: isScript ? hoist(template.statement.ast`
  143. var ${id} = require(${source}).${name}
  144. `) : t.importDeclaration([t.importSpecifier(id, name)], source),
  145. name: id.name
  146. };
  147. });
  148. },
  149. injectDefaultImport(url, hint = url) {
  150. return cache.storeNamed(prog, url, "default", (isScript, source) => {
  151. const id = prog.scope.generateUidIdentifier(hint);
  152. return {
  153. node: isScript ? hoist(template.statement.ast`var ${id} = require(${source})`) : t.importDeclaration([t.importDefaultSpecifier(id)], source),
  154. name: id.name
  155. };
  156. });
  157. }
  158. };
  159. };
  160. }