SourceMapSource.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Source = require("./Source");
  7. const { getMap, getSourceAndMap } = require("./helpers/getFromStreamChunks");
  8. const streamChunksOfCombinedSourceMap = require("./helpers/streamChunksOfCombinedSourceMap");
  9. const streamChunksOfSourceMap = require("./helpers/streamChunksOfSourceMap");
  10. const {
  11. isDualStringBufferCachingEnabled,
  12. } = require("./helpers/stringBufferUtils");
  13. /** @typedef {import("./Source").HashLike} HashLike */
  14. /** @typedef {import("./Source").MapOptions} MapOptions */
  15. /** @typedef {import("./Source").RawSourceMap} RawSourceMap */
  16. /** @typedef {import("./Source").SourceAndMap} SourceAndMap */
  17. /** @typedef {import("./Source").SourceValue} SourceValue */
  18. /** @typedef {import("./helpers/getGeneratedSourceInfo").GeneratedSourceInfo} GeneratedSourceInfo */
  19. /** @typedef {import("./helpers/streamChunks").OnChunk} OnChunk */
  20. /** @typedef {import("./helpers/streamChunks").OnName} OnName */
  21. /** @typedef {import("./helpers/streamChunks").OnSource} OnSource */
  22. /** @typedef {import("./helpers/streamChunks").Options} Options */
  23. class SourceMapSource extends Source {
  24. /**
  25. * @param {string | Buffer} value value
  26. * @param {string} name name
  27. * @param {string | Buffer | RawSourceMap=} sourceMap source map
  28. * @param {SourceValue=} originalSource original source
  29. * @param {(null | string | Buffer | RawSourceMap)=} innerSourceMap inner source map
  30. * @param {boolean=} removeOriginalSource do remove original source
  31. */
  32. constructor(
  33. value,
  34. name,
  35. sourceMap,
  36. originalSource,
  37. innerSourceMap,
  38. removeOriginalSource,
  39. ) {
  40. super();
  41. const valueIsBuffer = Buffer.isBuffer(value);
  42. /**
  43. * @private
  44. * @type {undefined | string}
  45. */
  46. this._valueAsString = valueIsBuffer ? undefined : value;
  47. /**
  48. * @private
  49. * @type {undefined | Buffer}
  50. */
  51. this._valueAsBuffer = valueIsBuffer ? value : undefined;
  52. this._name = name;
  53. this._hasSourceMap = Boolean(sourceMap);
  54. const sourceMapIsBuffer = Buffer.isBuffer(sourceMap);
  55. const sourceMapIsString = typeof sourceMap === "string";
  56. /**
  57. * @private
  58. * @type {undefined | RawSourceMap}
  59. */
  60. this._sourceMapAsObject =
  61. sourceMapIsBuffer || sourceMapIsString ? undefined : sourceMap;
  62. /**
  63. * @private
  64. * @type {undefined | string}
  65. */
  66. this._sourceMapAsString = sourceMapIsString ? sourceMap : undefined;
  67. /**
  68. * @private
  69. * @type {undefined | Buffer}
  70. */
  71. this._sourceMapAsBuffer = sourceMapIsBuffer ? sourceMap : undefined;
  72. this._hasOriginalSource = Boolean(originalSource);
  73. const originalSourceIsBuffer = Buffer.isBuffer(originalSource);
  74. this._originalSourceAsString = originalSourceIsBuffer
  75. ? undefined
  76. : originalSource;
  77. this._originalSourceAsBuffer = originalSourceIsBuffer
  78. ? originalSource
  79. : undefined;
  80. this._hasInnerSourceMap = Boolean(innerSourceMap);
  81. const innerSourceMapIsBuffer = Buffer.isBuffer(innerSourceMap);
  82. const innerSourceMapIsString = typeof innerSourceMap === "string";
  83. /**
  84. * @private
  85. * @type {undefined | RawSourceMap}
  86. */
  87. this._innerSourceMapAsObject =
  88. innerSourceMapIsBuffer || innerSourceMapIsString
  89. ? undefined
  90. : innerSourceMap || undefined;
  91. /**
  92. * @private
  93. * @type {undefined | string}
  94. */
  95. this._innerSourceMapAsString = innerSourceMapIsString
  96. ? innerSourceMap
  97. : undefined;
  98. /**
  99. * @private
  100. * @type {undefined | Buffer}
  101. */
  102. this._innerSourceMapAsBuffer = innerSourceMapIsBuffer
  103. ? innerSourceMap
  104. : undefined;
  105. this._removeOriginalSource = removeOriginalSource;
  106. }
  107. /**
  108. * @returns {[Buffer, string, Buffer, Buffer | undefined, Buffer | undefined, boolean | undefined]} args
  109. */
  110. getArgsAsBuffers() {
  111. return [
  112. this.buffer(),
  113. this._name,
  114. this._sourceMapBuffer(),
  115. this._originalSourceBuffer(),
  116. this._innerSourceMapBuffer(),
  117. this._removeOriginalSource,
  118. ];
  119. }
  120. /**
  121. * @returns {Buffer} buffer
  122. */
  123. buffer() {
  124. if (this._valueAsBuffer === undefined) {
  125. const value = Buffer.from(
  126. /** @type {string} */ (this._valueAsString),
  127. "utf8",
  128. );
  129. if (isDualStringBufferCachingEnabled()) {
  130. this._valueAsBuffer = value;
  131. }
  132. return value;
  133. }
  134. return this._valueAsBuffer;
  135. }
  136. /**
  137. * @returns {number} size
  138. */
  139. size() {
  140. if (this._cachedSize !== undefined) return this._cachedSize;
  141. if (this._valueAsBuffer !== undefined) {
  142. return (this._cachedSize = this._valueAsBuffer.length);
  143. }
  144. return (this._cachedSize = Buffer.byteLength(
  145. /** @type {string} */ (this._valueAsString),
  146. "utf8",
  147. ));
  148. }
  149. /**
  150. * @returns {SourceValue} source
  151. */
  152. source() {
  153. if (this._valueAsString === undefined) {
  154. const value =
  155. /** @type {Buffer} */
  156. (this._valueAsBuffer).toString("utf8");
  157. if (isDualStringBufferCachingEnabled()) {
  158. this._valueAsString = value;
  159. }
  160. return value;
  161. }
  162. return this._valueAsString;
  163. }
  164. /**
  165. * @private
  166. * @returns {undefined | Buffer} buffer
  167. */
  168. _originalSourceBuffer() {
  169. if (this._originalSourceAsBuffer === undefined && this._hasOriginalSource) {
  170. const value = Buffer.from(
  171. /** @type {string} */
  172. (this._originalSourceAsString),
  173. "utf8",
  174. );
  175. if (isDualStringBufferCachingEnabled()) {
  176. this._originalSourceAsBuffer = value;
  177. }
  178. return value;
  179. }
  180. return this._originalSourceAsBuffer;
  181. }
  182. _originalSourceString() {
  183. if (this._originalSourceAsString === undefined && this._hasOriginalSource) {
  184. const value =
  185. /** @type {Buffer} */
  186. (this._originalSourceAsBuffer).toString("utf8");
  187. if (isDualStringBufferCachingEnabled()) {
  188. this._originalSourceAsString = value;
  189. }
  190. return value;
  191. }
  192. return this._originalSourceAsString;
  193. }
  194. _innerSourceMapObject() {
  195. if (this._innerSourceMapAsObject === undefined && this._hasInnerSourceMap) {
  196. const value = JSON.parse(this._innerSourceMapString());
  197. if (isDualStringBufferCachingEnabled()) {
  198. this._innerSourceMapAsObject = value;
  199. }
  200. return value;
  201. }
  202. return this._innerSourceMapAsObject;
  203. }
  204. _innerSourceMapBuffer() {
  205. if (this._innerSourceMapAsBuffer === undefined && this._hasInnerSourceMap) {
  206. const value = Buffer.from(this._innerSourceMapString(), "utf8");
  207. if (isDualStringBufferCachingEnabled()) {
  208. this._innerSourceMapAsBuffer = value;
  209. }
  210. return value;
  211. }
  212. return this._innerSourceMapAsBuffer;
  213. }
  214. /**
  215. * @private
  216. * @returns {string} result
  217. */
  218. _innerSourceMapString() {
  219. if (this._innerSourceMapAsString === undefined && this._hasInnerSourceMap) {
  220. if (this._innerSourceMapAsBuffer !== undefined) {
  221. const value = this._innerSourceMapAsBuffer.toString("utf8");
  222. if (isDualStringBufferCachingEnabled()) {
  223. this._innerSourceMapAsString = value;
  224. }
  225. return value;
  226. }
  227. const value = JSON.stringify(this._innerSourceMapAsObject);
  228. if (isDualStringBufferCachingEnabled()) {
  229. this._innerSourceMapAsString = value;
  230. }
  231. return value;
  232. }
  233. return /** @type {string} */ (this._innerSourceMapAsString);
  234. }
  235. _sourceMapObject() {
  236. if (this._sourceMapAsObject === undefined) {
  237. const value = JSON.parse(this._sourceMapString());
  238. if (isDualStringBufferCachingEnabled()) {
  239. this._sourceMapAsObject = value;
  240. }
  241. return value;
  242. }
  243. return this._sourceMapAsObject;
  244. }
  245. _sourceMapBuffer() {
  246. if (this._sourceMapAsBuffer === undefined) {
  247. const value = Buffer.from(this._sourceMapString(), "utf8");
  248. if (isDualStringBufferCachingEnabled()) {
  249. this._sourceMapAsBuffer = value;
  250. }
  251. return value;
  252. }
  253. return this._sourceMapAsBuffer;
  254. }
  255. _sourceMapString() {
  256. if (this._sourceMapAsString === undefined) {
  257. if (this._sourceMapAsBuffer !== undefined) {
  258. const value = this._sourceMapAsBuffer.toString("utf8");
  259. if (isDualStringBufferCachingEnabled()) {
  260. this._sourceMapAsString = value;
  261. }
  262. return value;
  263. }
  264. const value = JSON.stringify(this._sourceMapAsObject);
  265. if (isDualStringBufferCachingEnabled()) {
  266. this._sourceMapAsString = value;
  267. }
  268. return value;
  269. }
  270. return this._sourceMapAsString;
  271. }
  272. /**
  273. * @param {MapOptions=} options map options
  274. * @returns {RawSourceMap | null} map
  275. */
  276. map(options) {
  277. if (!this._hasInnerSourceMap) {
  278. return this._sourceMapObject();
  279. }
  280. return getMap(this, options);
  281. }
  282. /**
  283. * @param {MapOptions=} options map options
  284. * @returns {SourceAndMap} source and map
  285. */
  286. sourceAndMap(options) {
  287. if (!this._hasInnerSourceMap) {
  288. return {
  289. source: this.source(),
  290. map: this._sourceMapObject(),
  291. };
  292. }
  293. return getSourceAndMap(this, options);
  294. }
  295. /**
  296. * @param {Options} options options
  297. * @param {OnChunk} onChunk called for each chunk of code
  298. * @param {OnSource} onSource called for each source
  299. * @param {OnName} onName called for each name
  300. * @returns {GeneratedSourceInfo} generated source info
  301. */
  302. streamChunks(options, onChunk, onSource, onName) {
  303. if (this._hasInnerSourceMap) {
  304. return streamChunksOfCombinedSourceMap(
  305. /** @type {string} */
  306. (this.source()),
  307. this._sourceMapObject(),
  308. this._name,
  309. /** @type {string} */
  310. (this._originalSourceString()),
  311. this._innerSourceMapObject(),
  312. this._removeOriginalSource,
  313. onChunk,
  314. onSource,
  315. onName,
  316. Boolean(options && options.finalSource),
  317. Boolean(options && options.columns !== false),
  318. );
  319. }
  320. return streamChunksOfSourceMap(
  321. /** @type {string} */
  322. (this.source()),
  323. this._sourceMapObject(),
  324. onChunk,
  325. onSource,
  326. onName,
  327. Boolean(options && options.finalSource),
  328. Boolean(options && options.columns !== false),
  329. );
  330. }
  331. /**
  332. * @param {HashLike} hash hash
  333. * @returns {void}
  334. */
  335. updateHash(hash) {
  336. hash.update("SourceMapSource");
  337. hash.update(this.buffer());
  338. hash.update(this._sourceMapBuffer());
  339. if (this._hasOriginalSource) {
  340. hash.update(
  341. /** @type {Buffer} */
  342. (this._originalSourceBuffer()),
  343. );
  344. }
  345. if (this._hasInnerSourceMap) {
  346. hash.update(
  347. /** @type {Buffer} */
  348. (this._innerSourceMapBuffer()),
  349. );
  350. }
  351. hash.update(this._removeOriginalSource ? "true" : "false");
  352. }
  353. }
  354. module.exports = SourceMapSource;