index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /** @typedef {import("./CachedSource").CachedData} CachedData */
  7. /** @typedef {import("./CompatSource").SourceLike} SourceLike */
  8. /** @typedef {import("./ConcatSource").Child} ConcatSourceChild */
  9. /** @typedef {import("./ReplaceSource").Replacement} Replacement */
  10. /** @typedef {import("./Source").HashLike} HashLike */
  11. /** @typedef {import("./Source").MapOptions} MapOptions */
  12. /** @typedef {import("./Source").RawSourceMap} RawSourceMap */
  13. /** @typedef {import("./Source").SourceAndMap} SourceAndMap */
  14. /** @typedef {import("./Source").SourceValue} SourceValue */
  15. /** @typedef {import("./helpers/getGeneratedSourceInfo").GeneratedSourceInfo} GeneratedSourceInfo */
  16. /** @typedef {import("./helpers/streamChunks").OnChunk} OnChunk */
  17. /** @typedef {import("./helpers/streamChunks").OnName} OnName */
  18. /** @typedef {import("./helpers/streamChunks").OnSource} OnSource */
  19. /** @typedef {import("./helpers/streamChunks").Options} StreamChunksOptions */
  20. /**
  21. * @template T
  22. * @param {() => T} fn memorized function
  23. * @returns {() => T} new function
  24. */
  25. const memoize = (fn) => {
  26. let cache = false;
  27. /** @type {T | undefined} */
  28. let result;
  29. return () => {
  30. if (cache) {
  31. return /** @type {T} */ (result);
  32. }
  33. result = fn();
  34. cache = true;
  35. // Allow to clean up memory for fn
  36. // and all dependent resources
  37. /** @type {(() => T) | undefined} */
  38. (fn) = undefined;
  39. return /** @type {T} */ (result);
  40. };
  41. };
  42. /**
  43. * @template A
  44. * @template B
  45. * @param {A} obj input a
  46. * @param {B} exports input b
  47. * @returns {A & B} merged
  48. */
  49. const mergeExports = (obj, exports) => {
  50. const descriptors = Object.getOwnPropertyDescriptors(exports);
  51. for (const name of Object.keys(descriptors)) {
  52. const descriptor = descriptors[name];
  53. if (descriptor.get) {
  54. const fn = descriptor.get;
  55. Object.defineProperty(obj, name, {
  56. configurable: false,
  57. enumerable: true,
  58. get: memoize(fn),
  59. });
  60. } else if (typeof descriptor.value === "object") {
  61. Object.defineProperty(obj, name, {
  62. configurable: false,
  63. enumerable: true,
  64. writable: false,
  65. value: mergeExports({}, descriptor.value),
  66. });
  67. } else {
  68. throw new Error(
  69. "Exposed values must be either a getter or an nested object",
  70. );
  71. }
  72. }
  73. return /** @type {A & B} */ (Object.freeze(obj));
  74. };
  75. module.exports = mergeExports(
  76. {},
  77. {
  78. get Source() {
  79. return require("./Source");
  80. },
  81. get RawSource() {
  82. return require("./RawSource");
  83. },
  84. get OriginalSource() {
  85. return require("./OriginalSource");
  86. },
  87. get SourceMapSource() {
  88. return require("./SourceMapSource");
  89. },
  90. get CachedSource() {
  91. return require("./CachedSource");
  92. },
  93. get ConcatSource() {
  94. return require("./ConcatSource");
  95. },
  96. get ReplaceSource() {
  97. return require("./ReplaceSource");
  98. },
  99. get PrefixSource() {
  100. return require("./PrefixSource");
  101. },
  102. get SizeOnlySource() {
  103. return require("./SizeOnlySource");
  104. },
  105. get CompatSource() {
  106. return require("./CompatSource");
  107. },
  108. util: {
  109. get stringBufferUtils() {
  110. return require("./helpers/stringBufferUtils");
  111. },
  112. },
  113. },
  114. );