utils.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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,
  30. /* noGlobals */
  31. true)) {
  32. return path.node.name;
  33. }
  34. const {
  35. deopt
  36. } = path.evaluate();
  37. if (deopt && deopt.isIdentifier()) {
  38. return deopt.node.name;
  39. }
  40. }
  41. function resolveKey(path, computed = false) {
  42. const {
  43. scope
  44. } = path;
  45. if (path.isStringLiteral()) return path.node.value;
  46. const isIdentifier = path.isIdentifier();
  47. if (isIdentifier && !(computed || path.parent.computed)) {
  48. return path.node.name;
  49. }
  50. if (computed && path.isMemberExpression() && path.get("object").isIdentifier({
  51. name: "Symbol"
  52. }) && !scope.hasBinding("Symbol",
  53. /* noGlobals */
  54. true)) {
  55. const sym = resolveKey(path.get("property"), path.node.computed);
  56. if (sym) return "Symbol." + sym;
  57. }
  58. if (!isIdentifier || scope.hasBinding(path.node.name,
  59. /* noGlobals */
  60. true)) {
  61. const {
  62. value
  63. } = path.evaluate();
  64. if (typeof value === "string") return value;
  65. }
  66. }
  67. function resolveSource(obj) {
  68. if (obj.isMemberExpression() && obj.get("property").isIdentifier({
  69. name: "prototype"
  70. })) {
  71. const id = resolveId(obj.get("object"));
  72. if (id) {
  73. return {
  74. id,
  75. placement: "prototype"
  76. };
  77. }
  78. return {
  79. id: null,
  80. placement: null
  81. };
  82. }
  83. const id = resolveId(obj);
  84. if (id) {
  85. return {
  86. id,
  87. placement: "static"
  88. };
  89. }
  90. const {
  91. value
  92. } = obj.evaluate();
  93. if (value !== undefined) {
  94. return {
  95. id: getType(value),
  96. placement: "prototype"
  97. };
  98. } else if (obj.isRegExpLiteral()) {
  99. return {
  100. id: "RegExp",
  101. placement: "prototype"
  102. };
  103. } else if (obj.isFunction()) {
  104. return {
  105. id: "Function",
  106. placement: "prototype"
  107. };
  108. }
  109. return {
  110. id: null,
  111. placement: null
  112. };
  113. }
  114. function getImportSource({
  115. node
  116. }) {
  117. if (node.specifiers.length === 0) return node.source.value;
  118. }
  119. function getRequireSource({
  120. node
  121. }) {
  122. if (!t.isExpressionStatement(node)) return;
  123. const {
  124. expression
  125. } = node;
  126. if (t.isCallExpression(expression) && t.isIdentifier(expression.callee) && expression.callee.name === "require" && expression.arguments.length === 1 && t.isStringLiteral(expression.arguments[0])) {
  127. return expression.arguments[0].value;
  128. }
  129. }
  130. function hoist(node) {
  131. // @ts-expect-error
  132. node._blockHoist = 3;
  133. return node;
  134. }
  135. function createUtilsGetter(cache) {
  136. return path => {
  137. const prog = path.findParent(p => p.isProgram());
  138. return {
  139. injectGlobalImport(url) {
  140. cache.storeAnonymous(prog, url, (isScript, source) => {
  141. return isScript ? template.statement.ast`require(${source})` : t.importDeclaration([], source);
  142. });
  143. },
  144. injectNamedImport(url, name, hint = name) {
  145. return cache.storeNamed(prog, url, name, (isScript, source, name) => {
  146. const id = prog.scope.generateUidIdentifier(hint);
  147. return {
  148. node: isScript ? hoist(template.statement.ast`
  149. var ${id} = require(${source}).${name}
  150. `) : t.importDeclaration([t.importSpecifier(id, name)], source),
  151. name: id.name
  152. };
  153. });
  154. },
  155. injectDefaultImport(url, hint = url) {
  156. return cache.storeNamed(prog, url, "default", (isScript, source) => {
  157. const id = prog.scope.generateUidIdentifier(hint);
  158. return {
  159. node: isScript ? hoist(template.statement.ast`var ${id} = require(${source})`) : t.importDeclaration([t.importDefaultSpecifier(id)], source),
  160. name: id.name
  161. };
  162. });
  163. }
  164. };
  165. };
  166. }