descriptorCache.js 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getDescriptor = exports.setDescriptor = void 0;
  4. const fs = require("fs");
  5. const compiler_1 = require("./compiler");
  6. const cache = new Map();
  7. function setDescriptor(filename, entry) {
  8. cache.set(cleanQuery(filename), entry);
  9. }
  10. exports.setDescriptor = setDescriptor;
  11. function getDescriptor(filename) {
  12. filename = cleanQuery(filename);
  13. if (cache.has(filename)) {
  14. return cache.get(filename);
  15. }
  16. // This function should only be called after the descriptor has been
  17. // cached by the main loader.
  18. // If this is somehow called without a cache hit, it's probably due to sub
  19. // loaders being run in separate threads. The only way to deal with this is to
  20. // read from disk directly...
  21. const source = fs.readFileSync(filename, 'utf-8');
  22. const { descriptor } = compiler_1.compiler.parse(source, {
  23. filename,
  24. sourceMap: true,
  25. });
  26. cache.set(filename, descriptor);
  27. return descriptor;
  28. }
  29. exports.getDescriptor = getDescriptor;
  30. function cleanQuery(str) {
  31. const i = str.indexOf('?');
  32. return i > 0 ? str.slice(0, i) : str;
  33. }