imports-cache.js 3.0 KB

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