SourceMapSource.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 {(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;
  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. buffer() {
  121. if (this._valueAsBuffer === undefined) {
  122. const value = Buffer.from(
  123. /** @type {string} */ (this._valueAsString),
  124. "utf8",
  125. );
  126. if (isDualStringBufferCachingEnabled()) {
  127. this._valueAsBuffer = value;
  128. }
  129. return value;
  130. }
  131. return this._valueAsBuffer;
  132. }
  133. /**
  134. * @returns {SourceValue} source
  135. */
  136. source() {
  137. if (this._valueAsString === undefined) {
  138. const value =
  139. /** @type {Buffer} */
  140. (this._valueAsBuffer).toString("utf8");
  141. if (isDualStringBufferCachingEnabled()) {
  142. this._valueAsString = value;
  143. }
  144. return value;
  145. }
  146. return this._valueAsString;
  147. }
  148. /**
  149. * @private
  150. * @returns {undefined | Buffer} buffer
  151. */
  152. _originalSourceBuffer() {
  153. if (this._originalSourceAsBuffer === undefined && this._hasOriginalSource) {
  154. const value = Buffer.from(
  155. /** @type {string} */
  156. (this._originalSourceAsString),
  157. "utf8",
  158. );
  159. if (isDualStringBufferCachingEnabled()) {
  160. this._originalSourceAsBuffer = value;
  161. }
  162. return value;
  163. }
  164. return this._originalSourceAsBuffer;
  165. }
  166. _originalSourceString() {
  167. if (this._originalSourceAsString === undefined && this._hasOriginalSource) {
  168. const value =
  169. /** @type {Buffer} */
  170. (this._originalSourceAsBuffer).toString("utf8");
  171. if (isDualStringBufferCachingEnabled()) {
  172. this._originalSourceAsString = value;
  173. }
  174. return value;
  175. }
  176. return this._originalSourceAsString;
  177. }
  178. _innerSourceMapObject() {
  179. if (this._innerSourceMapAsObject === undefined && this._hasInnerSourceMap) {
  180. const value = JSON.parse(this._innerSourceMapString());
  181. if (isDualStringBufferCachingEnabled()) {
  182. this._innerSourceMapAsObject = value;
  183. }
  184. return value;
  185. }
  186. return this._innerSourceMapAsObject;
  187. }
  188. _innerSourceMapBuffer() {
  189. if (this._innerSourceMapAsBuffer === undefined && this._hasInnerSourceMap) {
  190. const value = Buffer.from(this._innerSourceMapString(), "utf8");
  191. if (isDualStringBufferCachingEnabled()) {
  192. this._innerSourceMapAsBuffer = value;
  193. }
  194. return value;
  195. }
  196. return this._innerSourceMapAsBuffer;
  197. }
  198. /**
  199. * @private
  200. * @returns {string} result
  201. */
  202. _innerSourceMapString() {
  203. if (this._innerSourceMapAsString === undefined && this._hasInnerSourceMap) {
  204. if (this._innerSourceMapAsBuffer !== undefined) {
  205. const value = this._innerSourceMapAsBuffer.toString("utf8");
  206. if (isDualStringBufferCachingEnabled()) {
  207. this._innerSourceMapAsString = value;
  208. }
  209. return value;
  210. }
  211. const value = JSON.stringify(this._innerSourceMapAsObject);
  212. if (isDualStringBufferCachingEnabled()) {
  213. this._innerSourceMapAsString = value;
  214. }
  215. return value;
  216. }
  217. return /** @type {string} */ (this._innerSourceMapAsString);
  218. }
  219. _sourceMapObject() {
  220. if (this._sourceMapAsObject === undefined) {
  221. const value = JSON.parse(this._sourceMapString());
  222. if (isDualStringBufferCachingEnabled()) {
  223. this._sourceMapAsObject = value;
  224. }
  225. return value;
  226. }
  227. return this._sourceMapAsObject;
  228. }
  229. _sourceMapBuffer() {
  230. if (this._sourceMapAsBuffer === undefined) {
  231. const value = Buffer.from(this._sourceMapString(), "utf8");
  232. if (isDualStringBufferCachingEnabled()) {
  233. this._sourceMapAsBuffer = value;
  234. }
  235. return value;
  236. }
  237. return this._sourceMapAsBuffer;
  238. }
  239. _sourceMapString() {
  240. if (this._sourceMapAsString === undefined) {
  241. if (this._sourceMapAsBuffer !== undefined) {
  242. const value = this._sourceMapAsBuffer.toString("utf8");
  243. if (isDualStringBufferCachingEnabled()) {
  244. this._sourceMapAsString = value;
  245. }
  246. return value;
  247. }
  248. const value = JSON.stringify(this._sourceMapAsObject);
  249. if (isDualStringBufferCachingEnabled()) {
  250. this._sourceMapAsString = value;
  251. }
  252. return value;
  253. }
  254. return this._sourceMapAsString;
  255. }
  256. /**
  257. * @param {MapOptions=} options map options
  258. * @returns {RawSourceMap | null} map
  259. */
  260. map(options) {
  261. if (!this._hasInnerSourceMap) {
  262. return this._sourceMapObject();
  263. }
  264. return getMap(this, options);
  265. }
  266. /**
  267. * @param {MapOptions=} options map options
  268. * @returns {SourceAndMap} source and map
  269. */
  270. sourceAndMap(options) {
  271. if (!this._hasInnerSourceMap) {
  272. return {
  273. source: this.source(),
  274. map: this._sourceMapObject(),
  275. };
  276. }
  277. return getSourceAndMap(this, options);
  278. }
  279. /**
  280. * @param {Options} options options
  281. * @param {OnChunk} onChunk called for each chunk of code
  282. * @param {OnSource} onSource called for each source
  283. * @param {OnName} onName called for each name
  284. * @returns {GeneratedSourceInfo} generated source info
  285. */
  286. streamChunks(options, onChunk, onSource, onName) {
  287. if (this._hasInnerSourceMap) {
  288. return streamChunksOfCombinedSourceMap(
  289. /** @type {string} */
  290. (this.source()),
  291. this._sourceMapObject(),
  292. this._name,
  293. /** @type {string} */
  294. (this._originalSourceString()),
  295. this._innerSourceMapObject(),
  296. this._removeOriginalSource,
  297. onChunk,
  298. onSource,
  299. onName,
  300. Boolean(options && options.finalSource),
  301. Boolean(options && options.columns !== false),
  302. );
  303. }
  304. return streamChunksOfSourceMap(
  305. /** @type {string} */
  306. (this.source()),
  307. this._sourceMapObject(),
  308. onChunk,
  309. onSource,
  310. onName,
  311. Boolean(options && options.finalSource),
  312. Boolean(options && options.columns !== false),
  313. );
  314. }
  315. /**
  316. * @param {HashLike} hash hash
  317. * @returns {void}
  318. */
  319. updateHash(hash) {
  320. hash.update("SourceMapSource");
  321. hash.update(this.buffer());
  322. hash.update(this._sourceMapBuffer());
  323. if (this._hasOriginalSource) {
  324. hash.update(
  325. /** @type {Buffer} */
  326. (this._originalSourceBuffer()),
  327. );
  328. }
  329. if (this._hasInnerSourceMap) {
  330. hash.update(
  331. /** @type {Buffer} */
  332. (this._innerSourceMapBuffer()),
  333. );
  334. }
  335. hash.update(this._removeOriginalSource ? "true" : "false");
  336. }
  337. }
  338. module.exports = SourceMapSource;