instance-cache.js 1.3 KB

123456789101112131415161718192021222324252627282930
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.setTSInstanceInCache = exports.getTSInstanceFromCache = void 0;
  4. // Some loaders (e.g. thread-loader) will set the _compiler property to undefined.
  5. // We can't use undefined as a WeakMap key as it will throw an error at runtime,
  6. // thus we keep a dummy "marker" object to use as key in those situations.
  7. const marker = {};
  8. // Each TypeScript instance is cached based on the webpack instance (key of the WeakMap)
  9. // and also the name that was generated or passed via the options (string key of the
  10. // internal Map)
  11. const cache = new WeakMap();
  12. function getTSInstanceFromCache(key, name) {
  13. const compiler = key !== null && key !== void 0 ? key : marker;
  14. let instances = cache.get(compiler);
  15. if (!instances) {
  16. instances = new Map();
  17. cache.set(compiler, instances);
  18. }
  19. return instances.get(name);
  20. }
  21. exports.getTSInstanceFromCache = getTSInstanceFromCache;
  22. function setTSInstanceInCache(key, name, instance) {
  23. var _a;
  24. const compiler = key !== null && key !== void 0 ? key : marker;
  25. const instances = (_a = cache.get(compiler)) !== null && _a !== void 0 ? _a : new Map();
  26. instances.set(name, instance);
  27. cache.set(compiler, instances);
  28. }
  29. exports.setTSInstanceInCache = setTSInstanceInCache;
  30. //# sourceMappingURL=instance-cache.js.map