imports-cache.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. var babel = _interopRequireWildcard(require("@babel/core"));
  5. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  6. 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; }
  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. let lastImport = this._lastImports.get(programPath);
  41. if (lastImport && lastImport.node && // Sometimes the AST is modified and the "last import"
  42. // we have has been replaced
  43. lastImport.parent === programPath.node && lastImport.container === programPath.node.body) {
  44. lastImport = lastImport.insertAfter(node);
  45. } else {
  46. lastImport = programPath.unshiftContainer("body", node);
  47. }
  48. lastImport = lastImport[lastImport.length - 1];
  49. this._lastImports.set(programPath, lastImport);
  50. /*
  51. let lastImport;
  52. programPath.get("body").forEach(path => {
  53. if (path.isImportDeclaration()) lastImport = path;
  54. if (
  55. path.isExpressionStatement() &&
  56. isRequireCall(path.get("expression"))
  57. ) {
  58. lastImport = path;
  59. }
  60. if (
  61. path.isVariableDeclaration() &&
  62. path.get("declarations").length === 1 &&
  63. (isRequireCall(path.get("declarations.0.init")) ||
  64. (path.get("declarations.0.init").isMemberExpression() &&
  65. isRequireCall(path.get("declarations.0.init.object"))))
  66. ) {
  67. lastImport = path;
  68. }
  69. });*/
  70. }
  71. _ensure(map, programPath, Collection) {
  72. let collection = map.get(programPath);
  73. if (!collection) {
  74. collection = new Collection();
  75. map.set(programPath, collection);
  76. }
  77. return collection;
  78. }
  79. _normalizeKey(programPath, url, name = "") {
  80. const {
  81. sourceType
  82. } = programPath.node; // If we rely on the imported binding (the "name" parameter), we also need to cache
  83. // based on the sourceType. This is because the module transforms change the names
  84. // of the import variables.
  85. return `${name && sourceType}::${url}::${name}`;
  86. }
  87. }
  88. exports.default = ImportsCache;