imports-injector.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 ImportsCachedInjector {
  11. constructor(resolver, getPreferredIndex) {
  12. this._imports = new WeakMap();
  13. this._anonymousImports = new WeakMap();
  14. this._lastImports = new WeakMap();
  15. this._resolver = resolver;
  16. this._getPreferredIndex = getPreferredIndex;
  17. }
  18. storeAnonymous(programPath, url, moduleName, 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, moduleName);
  25. }
  26. storeNamed(programPath, url, name, moduleName, 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, moduleName);
  36. }
  37. return t.identifier(imports.get(key));
  38. }
  39. _injectImport(programPath, node, moduleName) {
  40. var _this$_lastImports$ge;
  41. const newIndex = this._getPreferredIndex(moduleName);
  42. const lastImports = (_this$_lastImports$ge = this._lastImports.get(programPath)) != null ? _this$_lastImports$ge : [];
  43. const isPathStillValid = path => path.node &&
  44. // Sometimes the AST is modified and the "last import"
  45. // we have has been replaced
  46. path.parent === programPath.node && path.container === programPath.node.body;
  47. let last;
  48. if (newIndex === Infinity) {
  49. // Fast path: we can always just insert at the end if newIndex is `Infinity`
  50. if (lastImports.length > 0) {
  51. last = lastImports[lastImports.length - 1].path;
  52. if (!isPathStillValid(last)) last = undefined;
  53. }
  54. } else {
  55. for (const [i, data] of lastImports.entries()) {
  56. const {
  57. path,
  58. index
  59. } = data;
  60. if (isPathStillValid(path)) {
  61. if (newIndex < index) {
  62. const [newPath] = path.insertBefore(node);
  63. lastImports.splice(i, 0, {
  64. path: newPath,
  65. index: newIndex
  66. });
  67. return;
  68. }
  69. last = path;
  70. }
  71. }
  72. }
  73. if (last) {
  74. const [newPath] = last.insertAfter(node);
  75. lastImports.push({
  76. path: newPath,
  77. index: newIndex
  78. });
  79. } else {
  80. const [newPath] = programPath.unshiftContainer("body", node);
  81. this._lastImports.set(programPath, [{
  82. path: newPath,
  83. index: newIndex
  84. }]);
  85. }
  86. }
  87. _ensure(map, programPath, Collection) {
  88. let collection = map.get(programPath);
  89. if (!collection) {
  90. collection = new Collection();
  91. map.set(programPath, collection);
  92. }
  93. return collection;
  94. }
  95. _normalizeKey(programPath, url, name = "") {
  96. const {
  97. sourceType
  98. } = programPath.node;
  99. // If we rely on the imported binding (the "name" parameter), we also need to cache
  100. // based on the sourceType. This is because the module transforms change the names
  101. // of the import variables.
  102. return `${name && sourceType}::${url}::${name}`;
  103. }
  104. }
  105. exports.default = ImportsCachedInjector;