utils.js 5.5 KB

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