utils.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
  12. const {
  13. types: t,
  14. template: template
  15. } = _babel.default || _babel;
  16. function intersection(a, b) {
  17. const result = new Set();
  18. a.forEach(v => b.has(v) && result.add(v));
  19. return result;
  20. }
  21. function has(object, key) {
  22. return Object.prototype.hasOwnProperty.call(object, key);
  23. }
  24. function resolve(path, resolved = new Set()) {
  25. if (resolved.has(path)) return;
  26. resolved.add(path);
  27. if (path.isVariableDeclarator()) {
  28. if (path.get("id").isIdentifier()) {
  29. return resolve(path.get("init"), resolved);
  30. }
  31. } else if (path.isReferencedIdentifier()) {
  32. const binding = path.scope.getBinding(path.node.name);
  33. if (!binding) return path;
  34. if (!binding.constant) return;
  35. return resolve(binding.path, resolved);
  36. }
  37. return path;
  38. }
  39. function resolveId(path) {
  40. if (path.isIdentifier() && !path.scope.hasBinding(path.node.name, /* noGlobals */true)) {
  41. return path.node.name;
  42. }
  43. const resolved = resolve(path);
  44. if (resolved != null && resolved.isIdentifier()) {
  45. return resolved.node.name;
  46. }
  47. }
  48. function resolveKey(path, computed = false) {
  49. const {
  50. scope
  51. } = path;
  52. if (path.isStringLiteral()) return path.node.value;
  53. const isIdentifier = path.isIdentifier();
  54. if (isIdentifier && !(computed || path.parent.computed)) {
  55. return path.node.name;
  56. }
  57. if (computed && path.isMemberExpression() && path.get("object").isIdentifier({
  58. name: "Symbol"
  59. }) && !scope.hasBinding("Symbol", /* noGlobals */true)) {
  60. const sym = resolveKey(path.get("property"), path.node.computed);
  61. if (sym) return "Symbol." + sym;
  62. }
  63. if (isIdentifier ? scope.hasBinding(path.node.name, /* noGlobals */true) : path.isPure()) {
  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 path = resolve(obj);
  94. switch (path == null ? void 0 : path.type) {
  95. case "RegExpLiteral":
  96. return {
  97. id: "RegExp",
  98. placement: "prototype"
  99. };
  100. case "FunctionExpression":
  101. return {
  102. id: "Function",
  103. placement: "prototype"
  104. };
  105. case "StringLiteral":
  106. return {
  107. id: "String",
  108. placement: "prototype"
  109. };
  110. case "NumberLiteral":
  111. return {
  112. id: "Number",
  113. placement: "prototype"
  114. };
  115. case "BooleanLiteral":
  116. return {
  117. id: "Boolean",
  118. placement: "prototype"
  119. };
  120. case "ObjectExpression":
  121. return {
  122. id: "Object",
  123. placement: "prototype"
  124. };
  125. case "ArrayExpression":
  126. return {
  127. id: "Array",
  128. placement: "prototype"
  129. };
  130. }
  131. return {
  132. id: null,
  133. placement: null
  134. };
  135. }
  136. function getImportSource({
  137. node
  138. }) {
  139. if (node.specifiers.length === 0) return node.source.value;
  140. }
  141. function getRequireSource({
  142. node
  143. }) {
  144. if (!t.isExpressionStatement(node)) return;
  145. const {
  146. expression
  147. } = node;
  148. if (t.isCallExpression(expression) && t.isIdentifier(expression.callee) && expression.callee.name === "require" && expression.arguments.length === 1 && t.isStringLiteral(expression.arguments[0])) {
  149. return expression.arguments[0].value;
  150. }
  151. }
  152. function hoist(node) {
  153. // @ts-expect-error
  154. node._blockHoist = 3;
  155. return node;
  156. }
  157. function createUtilsGetter(cache) {
  158. return path => {
  159. const prog = path.findParent(p => p.isProgram());
  160. return {
  161. injectGlobalImport(url, moduleName) {
  162. cache.storeAnonymous(prog, url, moduleName, (isScript, source) => {
  163. return isScript ? template.statement.ast`require(${source})` : t.importDeclaration([], source);
  164. });
  165. },
  166. injectNamedImport(url, name, hint = name, moduleName) {
  167. return cache.storeNamed(prog, url, name, moduleName, (isScript, source, name) => {
  168. const id = prog.scope.generateUidIdentifier(hint);
  169. return {
  170. node: isScript ? hoist(template.statement.ast`
  171. var ${id} = require(${source}).${name}
  172. `) : t.importDeclaration([t.importSpecifier(id, name)], source),
  173. name: id.name
  174. };
  175. });
  176. },
  177. injectDefaultImport(url, hint = url, moduleName) {
  178. return cache.storeNamed(prog, url, "default", moduleName, (isScript, source) => {
  179. const id = prog.scope.generateUidIdentifier(hint);
  180. return {
  181. node: isScript ? hoist(template.statement.ast`var ${id} = require(${source})`) : t.importDeclaration([t.importDefaultSpecifier(id)], source),
  182. name: id.name
  183. };
  184. });
  185. }
  186. };
  187. };
  188. }