utils.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 resolve(path, resolved = new Set()) {
  26. if (resolved.has(path)) return;
  27. resolved.add(path);
  28. if (path.isVariableDeclarator()) {
  29. if (path.get("id").isIdentifier()) {
  30. return resolve(path.get("init"), resolved);
  31. }
  32. } else if (path.isReferencedIdentifier()) {
  33. const binding = path.scope.getBinding(path.node.name);
  34. if (!binding) return path;
  35. if (!binding.constant) return;
  36. return resolve(binding.path, resolved);
  37. }
  38. return path;
  39. }
  40. function resolveId(path) {
  41. if (path.isIdentifier() && !path.scope.hasBinding(path.node.name, /* noGlobals */true)) {
  42. return path.node.name;
  43. }
  44. const resolved = resolve(path);
  45. if (resolved != null && resolved.isIdentifier()) {
  46. return resolved.node.name;
  47. }
  48. }
  49. function resolveKey(path, computed = false) {
  50. const {
  51. scope
  52. } = path;
  53. if (path.isStringLiteral()) return path.node.value;
  54. const isIdentifier = path.isIdentifier();
  55. if (isIdentifier && !(computed || path.parent.computed)) {
  56. return path.node.name;
  57. }
  58. if (computed && path.isMemberExpression() && path.get("object").isIdentifier({
  59. name: "Symbol"
  60. }) && !scope.hasBinding("Symbol", /* noGlobals */true)) {
  61. const sym = resolveKey(path.get("property"), path.node.computed);
  62. if (sym) return "Symbol." + sym;
  63. }
  64. if (isIdentifier ? scope.hasBinding(path.node.name, /* noGlobals */true) : path.isPure()) {
  65. const {
  66. value
  67. } = path.evaluate();
  68. if (typeof value === "string") return value;
  69. }
  70. }
  71. function resolveSource(obj) {
  72. if (obj.isMemberExpression() && obj.get("property").isIdentifier({
  73. name: "prototype"
  74. })) {
  75. const id = resolveId(obj.get("object"));
  76. if (id) {
  77. return {
  78. id,
  79. placement: "prototype"
  80. };
  81. }
  82. return {
  83. id: null,
  84. placement: null
  85. };
  86. }
  87. const id = resolveId(obj);
  88. if (id) {
  89. return {
  90. id,
  91. placement: "static"
  92. };
  93. }
  94. const path = resolve(obj);
  95. switch (path == null ? void 0 : path.type) {
  96. case "RegExpLiteral":
  97. return {
  98. id: "RegExp",
  99. placement: "prototype"
  100. };
  101. case "FunctionExpression":
  102. return {
  103. id: "Function",
  104. placement: "prototype"
  105. };
  106. case "StringLiteral":
  107. return {
  108. id: "String",
  109. placement: "prototype"
  110. };
  111. case "NumberLiteral":
  112. return {
  113. id: "Number",
  114. placement: "prototype"
  115. };
  116. case "BooleanLiteral":
  117. return {
  118. id: "Boolean",
  119. placement: "prototype"
  120. };
  121. case "ObjectExpression":
  122. return {
  123. id: "Object",
  124. placement: "prototype"
  125. };
  126. case "ArrayExpression":
  127. return {
  128. id: "Array",
  129. placement: "prototype"
  130. };
  131. }
  132. return {
  133. id: null,
  134. placement: null
  135. };
  136. }
  137. function getImportSource({
  138. node
  139. }) {
  140. if (node.specifiers.length === 0) return node.source.value;
  141. }
  142. function getRequireSource({
  143. node
  144. }) {
  145. if (!t.isExpressionStatement(node)) return;
  146. const {
  147. expression
  148. } = node;
  149. if (t.isCallExpression(expression) && t.isIdentifier(expression.callee) && expression.callee.name === "require" && expression.arguments.length === 1 && t.isStringLiteral(expression.arguments[0])) {
  150. return expression.arguments[0].value;
  151. }
  152. }
  153. function hoist(node) {
  154. // @ts-expect-error
  155. node._blockHoist = 3;
  156. return node;
  157. }
  158. function createUtilsGetter(cache) {
  159. return path => {
  160. const prog = path.findParent(p => p.isProgram());
  161. return {
  162. injectGlobalImport(url, moduleName) {
  163. cache.storeAnonymous(prog, url, moduleName, (isScript, source) => {
  164. return isScript ? template.statement.ast`require(${source})` : t.importDeclaration([], source);
  165. });
  166. },
  167. injectNamedImport(url, name, hint = name, moduleName) {
  168. return cache.storeNamed(prog, url, name, moduleName, (isScript, source, name) => {
  169. const id = prog.scope.generateUidIdentifier(hint);
  170. return {
  171. node: isScript ? hoist(template.statement.ast`
  172. var ${id} = require(${source}).${name}
  173. `) : t.importDeclaration([t.importSpecifier(id, name)], source),
  174. name: id.name
  175. };
  176. });
  177. },
  178. injectDefaultImport(url, hint = url, moduleName) {
  179. return cache.storeNamed(prog, url, "default", moduleName, (isScript, source) => {
  180. const id = prog.scope.generateUidIdentifier(hint);
  181. return {
  182. node: isScript ? hoist(template.statement.ast`var ${id} = require(${source})`) : t.importDeclaration([t.importDefaultSpecifier(id)], source),
  183. name: id.name
  184. };
  185. });
  186. }
  187. };
  188. };
  189. }