utils.js 5.2 KB

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