imports-cache.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. var _babel = _interopRequireWildcard(require("@babel/core"));
  5. 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); }
  6. 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; }
  7. const {
  8. types: t
  9. } = _babel.default || _babel;
  10. class ImportsCache {
  11. constructor(resolver) {
  12. this._imports = new WeakMap();
  13. this._anonymousImports = new WeakMap();
  14. this._lastImports = new WeakMap();
  15. this._resolver = resolver;
  16. }
  17. storeAnonymous(programPath, url, // eslint-disable-next-line no-undef
  18. getVal) {
  19. const key = this._normalizeKey(programPath, url);
  20. const imports = this._ensure(this._anonymousImports, programPath, Set);
  21. if (imports.has(key)) return;
  22. const node = getVal(programPath.node.sourceType === "script", t.stringLiteral(this._resolver(url)));
  23. imports.add(key);
  24. this._injectImport(programPath, node);
  25. }
  26. storeNamed(programPath, url, name, getVal) {
  27. const key = this._normalizeKey(programPath, url, name);
  28. const imports = this._ensure(this._imports, programPath, Map);
  29. if (!imports.has(key)) {
  30. const {
  31. node,
  32. name: id
  33. } = getVal(programPath.node.sourceType === "script", t.stringLiteral(this._resolver(url)), t.identifier(name));
  34. imports.set(key, id);
  35. this._injectImport(programPath, node);
  36. }
  37. return t.identifier(imports.get(key));
  38. }
  39. _injectImport(programPath, node) {
  40. const lastImport = this._lastImports.get(programPath);
  41. let newNodes;
  42. if (lastImport && lastImport.node && // Sometimes the AST is modified and the "last import"
  43. // we have has been replaced
  44. lastImport.parent === programPath.node && lastImport.container === programPath.node.body) {
  45. newNodes = lastImport.insertAfter(node);
  46. } else {
  47. newNodes = programPath.unshiftContainer("body", node);
  48. }
  49. const newNode = newNodes[newNodes.length - 1];
  50. this._lastImports.set(programPath, newNode);
  51. /*
  52. let lastImport;
  53. programPath.get("body").forEach(path => {
  54. if (path.isImportDeclaration()) lastImport = path;
  55. if (
  56. path.isExpressionStatement() &&
  57. isRequireCall(path.get("expression"))
  58. ) {
  59. lastImport = path;
  60. }
  61. if (
  62. path.isVariableDeclaration() &&
  63. path.get("declarations").length === 1 &&
  64. (isRequireCall(path.get("declarations.0.init")) ||
  65. (path.get("declarations.0.init").isMemberExpression() &&
  66. isRequireCall(path.get("declarations.0.init.object"))))
  67. ) {
  68. lastImport = path;
  69. }
  70. });*/
  71. }
  72. _ensure(map, programPath, Collection) {
  73. let collection = map.get(programPath);
  74. if (!collection) {
  75. collection = new Collection();
  76. map.set(programPath, collection);
  77. }
  78. return collection;
  79. }
  80. _normalizeKey(programPath, url, name = "") {
  81. const {
  82. sourceType
  83. } = programPath.node; // If we rely on the imported binding (the "name" parameter), we also need to cache
  84. // based on the sourceType. This is because the module transforms change the names
  85. // of the import variables.
  86. return `${name && sourceType}::${url}::${name}`;
  87. }
  88. }
  89. exports.default = ImportsCache;