imports-injector.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. var _babel = _interopRequireWildcard(require("@babel/core"));
  5. function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
  6. const {
  7. types: t
  8. } = _babel.default || _babel;
  9. class ImportsCachedInjector {
  10. constructor(resolver, getPreferredIndex) {
  11. this._imports = new WeakMap();
  12. this._anonymousImports = new WeakMap();
  13. this._lastImports = new WeakMap();
  14. this._resolver = resolver;
  15. this._getPreferredIndex = getPreferredIndex;
  16. }
  17. storeAnonymous(programPath, url, moduleName, getVal) {
  18. const key = this._normalizeKey(programPath, url);
  19. const imports = this._ensure(this._anonymousImports, programPath, Set);
  20. if (imports.has(key)) return;
  21. const node = getVal(programPath.node.sourceType === "script", t.stringLiteral(this._resolver(url)));
  22. imports.add(key);
  23. this._injectImport(programPath, node, moduleName);
  24. }
  25. storeNamed(programPath, url, name, moduleName, getVal) {
  26. const key = this._normalizeKey(programPath, url, name);
  27. const imports = this._ensure(this._imports, programPath, Map);
  28. if (!imports.has(key)) {
  29. const {
  30. node,
  31. name: id
  32. } = getVal(programPath.node.sourceType === "script", t.stringLiteral(this._resolver(url)), t.identifier(name));
  33. imports.set(key, id);
  34. this._injectImport(programPath, node, moduleName);
  35. }
  36. return t.identifier(imports.get(key));
  37. }
  38. _injectImport(programPath, node, moduleName) {
  39. var _this$_lastImports$ge;
  40. const newIndex = this._getPreferredIndex(moduleName);
  41. const lastImports = (_this$_lastImports$ge = this._lastImports.get(programPath)) != null ? _this$_lastImports$ge : [];
  42. const isPathStillValid = path => path.node &&
  43. // Sometimes the AST is modified and the "last import"
  44. // we have has been replaced
  45. path.parent === programPath.node && path.container === programPath.node.body;
  46. let last;
  47. if (newIndex === Infinity) {
  48. // Fast path: we can always just insert at the end if newIndex is `Infinity`
  49. if (lastImports.length > 0) {
  50. last = lastImports[lastImports.length - 1].path;
  51. if (!isPathStillValid(last)) last = undefined;
  52. }
  53. } else {
  54. for (const [i, data] of lastImports.entries()) {
  55. const {
  56. path,
  57. index
  58. } = data;
  59. if (isPathStillValid(path)) {
  60. if (newIndex < index) {
  61. const [newPath] = path.insertBefore(node);
  62. lastImports.splice(i, 0, {
  63. path: newPath,
  64. index: newIndex
  65. });
  66. return;
  67. }
  68. last = path;
  69. }
  70. }
  71. }
  72. if (last) {
  73. const [newPath] = last.insertAfter(node);
  74. lastImports.push({
  75. path: newPath,
  76. index: newIndex
  77. });
  78. } else {
  79. const [newPath] = programPath.unshiftContainer("body", [node]);
  80. this._lastImports.set(programPath, [{
  81. path: newPath,
  82. index: newIndex
  83. }]);
  84. }
  85. }
  86. _ensure(map, programPath, Collection) {
  87. let collection = map.get(programPath);
  88. if (!collection) {
  89. collection = new Collection();
  90. map.set(programPath, collection);
  91. }
  92. return collection;
  93. }
  94. _normalizeKey(programPath, url, name = "") {
  95. const {
  96. sourceType
  97. } = programPath.node;
  98. // If we rely on the imported binding (the "name" parameter), we also need to cache
  99. // based on the sourceType. This is because the module transforms change the names
  100. // of the import variables.
  101. return `${name && sourceType}::${url}::${name}`;
  102. }
  103. }
  104. exports.default = ImportsCachedInjector;