descriptorCache.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
  5. }) : (function(o, m, k, k2) {
  6. if (k2 === undefined) k2 = k;
  7. o[k2] = m[k];
  8. }));
  9. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  10. Object.defineProperty(o, "default", { enumerable: true, value: v });
  11. }) : function(o, v) {
  12. o["default"] = v;
  13. });
  14. var __importStar = (this && this.__importStar) || function (mod) {
  15. if (mod && mod.__esModule) return mod;
  16. var result = {};
  17. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  18. __setModuleDefault(result, mod);
  19. return result;
  20. };
  21. Object.defineProperty(exports, "__esModule", { value: true });
  22. exports.getDescriptor = exports.setDescriptor = exports.descriptorCache = void 0;
  23. const fs = __importStar(require("fs"));
  24. const compiler_1 = require("./compiler");
  25. const { parse } = compiler_1.compiler;
  26. exports.descriptorCache = new Map();
  27. function setDescriptor(filename, entry) {
  28. exports.descriptorCache.set(cleanQuery(filename), entry);
  29. }
  30. exports.setDescriptor = setDescriptor;
  31. function getDescriptor(filename, compilerOptions) {
  32. filename = cleanQuery(filename);
  33. if (exports.descriptorCache.has(filename)) {
  34. return exports.descriptorCache.get(filename);
  35. }
  36. // This function should only be called after the descriptor has been
  37. // cached by the main loader.
  38. // If this is somehow called without a cache hit, it's probably due to sub
  39. // loaders being run in separate threads. The only way to deal with this is to
  40. // read from disk directly...
  41. const source = fs.readFileSync(filename, 'utf-8');
  42. const { descriptor } = parse(source, {
  43. filename,
  44. sourceMap: true,
  45. templateParseOptions: compilerOptions,
  46. });
  47. exports.descriptorCache.set(filename, descriptor);
  48. return descriptor;
  49. }
  50. exports.getDescriptor = getDescriptor;
  51. function cleanQuery(str) {
  52. const i = str.indexOf('?');
  53. return i > 0 ? str.slice(0, i) : str;
  54. }