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