SourceMapSource.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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").ClearCacheOptions} ClearCacheOptions */
  14. /** @typedef {import("./Source").HashLike} HashLike */
  15. /** @typedef {import("./Source").MapOptions} MapOptions */
  16. /** @typedef {import("./Source").RawSourceMap} RawSourceMap */
  17. /** @typedef {import("./Source").SourceAndMap} SourceAndMap */
  18. /** @typedef {import("./Source").SourceValue} SourceValue */
  19. /** @typedef {import("./helpers/getGeneratedSourceInfo").GeneratedSourceInfo} GeneratedSourceInfo */
  20. /** @typedef {import("./helpers/streamChunks").OnChunk} OnChunk */
  21. /** @typedef {import("./helpers/streamChunks").OnName} OnName */
  22. /** @typedef {import("./helpers/streamChunks").OnSource} OnSource */
  23. /** @typedef {import("./helpers/streamChunks").Options} Options */
  24. class SourceMapSource extends Source {
  25. /**
  26. * @param {string | Buffer} value value
  27. * @param {string} name name
  28. * @param {string | Buffer | RawSourceMap=} sourceMap source map
  29. * @param {SourceValue=} originalSource original source
  30. * @param {(null | string | Buffer | RawSourceMap)=} innerSourceMap inner source map
  31. * @param {boolean=} removeOriginalSource do remove original source
  32. */
  33. constructor(
  34. value,
  35. name,
  36. sourceMap,
  37. originalSource,
  38. innerSourceMap,
  39. removeOriginalSource,
  40. ) {
  41. super();
  42. const valueIsBuffer = Buffer.isBuffer(value);
  43. /**
  44. * @private
  45. * @type {undefined | string}
  46. */
  47. this._valueAsString = valueIsBuffer ? undefined : value;
  48. /**
  49. * @private
  50. * @type {undefined | Buffer}
  51. */
  52. this._valueAsBuffer = valueIsBuffer ? value : undefined;
  53. this._name = name;
  54. this._hasSourceMap = Boolean(sourceMap);
  55. const sourceMapIsBuffer = Buffer.isBuffer(sourceMap);
  56. const sourceMapIsString = typeof sourceMap === "string";
  57. /**
  58. * @private
  59. * @type {undefined | RawSourceMap}
  60. */
  61. this._sourceMapAsObject =
  62. sourceMapIsBuffer || sourceMapIsString ? undefined : sourceMap;
  63. /**
  64. * @private
  65. * @type {undefined | string}
  66. */
  67. this._sourceMapAsString = sourceMapIsString ? sourceMap : undefined;
  68. /**
  69. * @private
  70. * @type {undefined | Buffer}
  71. */
  72. this._sourceMapAsBuffer = sourceMapIsBuffer ? sourceMap : undefined;
  73. this._hasOriginalSource = Boolean(originalSource);
  74. const originalSourceIsBuffer = Buffer.isBuffer(originalSource);
  75. this._originalSourceAsString = originalSourceIsBuffer
  76. ? undefined
  77. : originalSource;
  78. this._originalSourceAsBuffer = originalSourceIsBuffer
  79. ? originalSource
  80. : undefined;
  81. this._hasInnerSourceMap = Boolean(innerSourceMap);
  82. const innerSourceMapIsBuffer = Buffer.isBuffer(innerSourceMap);
  83. const innerSourceMapIsString = typeof innerSourceMap === "string";
  84. /**
  85. * @private
  86. * @type {undefined | RawSourceMap}
  87. */
  88. this._innerSourceMapAsObject =
  89. innerSourceMapIsBuffer || innerSourceMapIsString
  90. ? undefined
  91. : innerSourceMap || undefined;
  92. /**
  93. * @private
  94. * @type {undefined | string}
  95. */
  96. this._innerSourceMapAsString = innerSourceMapIsString
  97. ? innerSourceMap
  98. : undefined;
  99. /**
  100. * @private
  101. * @type {undefined | Buffer}
  102. */
  103. this._innerSourceMapAsBuffer = innerSourceMapIsBuffer
  104. ? innerSourceMap
  105. : undefined;
  106. this._removeOriginalSource = removeOriginalSource;
  107. }
  108. /**
  109. * @returns {[Buffer, string, Buffer, Buffer | undefined, Buffer | undefined, boolean | undefined]} args
  110. */
  111. getArgsAsBuffers() {
  112. return [
  113. this.buffer(),
  114. this._name,
  115. this._sourceMapBuffer(),
  116. this._originalSourceBuffer(),
  117. this._innerSourceMapBuffer(),
  118. this._removeOriginalSource,
  119. ];
  120. }
  121. /**
  122. * @returns {Buffer} buffer
  123. */
  124. buffer() {
  125. if (this._valueAsBuffer === undefined) {
  126. const value = Buffer.from(
  127. /** @type {string} */ (this._valueAsString),
  128. "utf8",
  129. );
  130. if (isDualStringBufferCachingEnabled()) {
  131. this._valueAsBuffer = value;
  132. }
  133. return value;
  134. }
  135. return this._valueAsBuffer;
  136. }
  137. /**
  138. * @returns {number} size
  139. */
  140. size() {
  141. if (this._cachedSize !== undefined) return this._cachedSize;
  142. if (this._valueAsBuffer !== undefined) {
  143. return (this._cachedSize = this._valueAsBuffer.length);
  144. }
  145. return (this._cachedSize = Buffer.byteLength(
  146. /** @type {string} */ (this._valueAsString),
  147. "utf8",
  148. ));
  149. }
  150. /**
  151. * @returns {SourceValue} source
  152. */
  153. source() {
  154. if (this._valueAsString === undefined) {
  155. const value =
  156. /** @type {Buffer} */
  157. (this._valueAsBuffer).toString("utf8");
  158. if (isDualStringBufferCachingEnabled()) {
  159. this._valueAsString = value;
  160. }
  161. return value;
  162. }
  163. return this._valueAsString;
  164. }
  165. /**
  166. * @private
  167. * @returns {undefined | Buffer} buffer
  168. */
  169. _originalSourceBuffer() {
  170. if (this._originalSourceAsBuffer === undefined && this._hasOriginalSource) {
  171. const value = Buffer.from(
  172. /** @type {string} */
  173. (this._originalSourceAsString),
  174. "utf8",
  175. );
  176. if (isDualStringBufferCachingEnabled()) {
  177. this._originalSourceAsBuffer = value;
  178. }
  179. return value;
  180. }
  181. return this._originalSourceAsBuffer;
  182. }
  183. _originalSourceString() {
  184. if (this._originalSourceAsString === undefined && this._hasOriginalSource) {
  185. const value =
  186. /** @type {Buffer} */
  187. (this._originalSourceAsBuffer).toString("utf8");
  188. if (isDualStringBufferCachingEnabled()) {
  189. this._originalSourceAsString = value;
  190. }
  191. return value;
  192. }
  193. return this._originalSourceAsString;
  194. }
  195. _innerSourceMapObject() {
  196. if (this._innerSourceMapAsObject === undefined && this._hasInnerSourceMap) {
  197. const value = JSON.parse(this._innerSourceMapString());
  198. if (isDualStringBufferCachingEnabled()) {
  199. this._innerSourceMapAsObject = value;
  200. }
  201. return value;
  202. }
  203. return this._innerSourceMapAsObject;
  204. }
  205. _innerSourceMapBuffer() {
  206. if (this._innerSourceMapAsBuffer === undefined && this._hasInnerSourceMap) {
  207. const value = Buffer.from(this._innerSourceMapString(), "utf8");
  208. if (isDualStringBufferCachingEnabled()) {
  209. this._innerSourceMapAsBuffer = value;
  210. }
  211. return value;
  212. }
  213. return this._innerSourceMapAsBuffer;
  214. }
  215. /**
  216. * @private
  217. * @returns {string} result
  218. */
  219. _innerSourceMapString() {
  220. if (this._innerSourceMapAsString === undefined && this._hasInnerSourceMap) {
  221. if (this._innerSourceMapAsBuffer !== undefined) {
  222. const value = this._innerSourceMapAsBuffer.toString("utf8");
  223. if (isDualStringBufferCachingEnabled()) {
  224. this._innerSourceMapAsString = value;
  225. }
  226. return value;
  227. }
  228. const value = JSON.stringify(this._innerSourceMapAsObject);
  229. if (isDualStringBufferCachingEnabled()) {
  230. this._innerSourceMapAsString = value;
  231. }
  232. return value;
  233. }
  234. return /** @type {string} */ (this._innerSourceMapAsString);
  235. }
  236. _sourceMapObject() {
  237. if (this._sourceMapAsObject === undefined) {
  238. const value = JSON.parse(this._sourceMapString());
  239. if (isDualStringBufferCachingEnabled()) {
  240. this._sourceMapAsObject = value;
  241. }
  242. return value;
  243. }
  244. return this._sourceMapAsObject;
  245. }
  246. _sourceMapBuffer() {
  247. if (this._sourceMapAsBuffer === undefined) {
  248. const value = Buffer.from(this._sourceMapString(), "utf8");
  249. if (isDualStringBufferCachingEnabled()) {
  250. this._sourceMapAsBuffer = value;
  251. }
  252. return value;
  253. }
  254. return this._sourceMapAsBuffer;
  255. }
  256. _sourceMapString() {
  257. if (this._sourceMapAsString === undefined) {
  258. if (this._sourceMapAsBuffer !== undefined) {
  259. const value = this._sourceMapAsBuffer.toString("utf8");
  260. if (isDualStringBufferCachingEnabled()) {
  261. this._sourceMapAsString = value;
  262. }
  263. return value;
  264. }
  265. const value = JSON.stringify(this._sourceMapAsObject);
  266. if (isDualStringBufferCachingEnabled()) {
  267. this._sourceMapAsString = value;
  268. }
  269. return value;
  270. }
  271. return this._sourceMapAsString;
  272. }
  273. /**
  274. * @param {MapOptions=} options map options
  275. * @returns {RawSourceMap | null} map
  276. */
  277. map(options) {
  278. if (!this._hasInnerSourceMap) {
  279. return this._sourceMapObject();
  280. }
  281. return getMap(this, options);
  282. }
  283. /**
  284. * @param {MapOptions=} options map options
  285. * @returns {SourceAndMap} source and map
  286. */
  287. sourceAndMap(options) {
  288. if (!this._hasInnerSourceMap) {
  289. return {
  290. source: this.source(),
  291. map: this._sourceMapObject(),
  292. };
  293. }
  294. return getSourceAndMap(this, options);
  295. }
  296. /**
  297. * @param {Options} options options
  298. * @param {OnChunk} onChunk called for each chunk of code
  299. * @param {OnSource} onSource called for each source
  300. * @param {OnName} onName called for each name
  301. * @returns {GeneratedSourceInfo} generated source info
  302. */
  303. streamChunks(options, onChunk, onSource, onName) {
  304. if (this._hasInnerSourceMap) {
  305. return streamChunksOfCombinedSourceMap(
  306. /** @type {string} */
  307. (this.source()),
  308. this._sourceMapObject(),
  309. this._name,
  310. /** @type {string} */
  311. (this._originalSourceString()),
  312. this._innerSourceMapObject(),
  313. this._removeOriginalSource,
  314. onChunk,
  315. onSource,
  316. onName,
  317. Boolean(options && options.finalSource),
  318. Boolean(options && options.columns !== false),
  319. );
  320. }
  321. return streamChunksOfSourceMap(
  322. /** @type {string} */
  323. (this.source()),
  324. this._sourceMapObject(),
  325. onChunk,
  326. onSource,
  327. onName,
  328. Boolean(options && options.finalSource),
  329. Boolean(options && options.columns !== false),
  330. );
  331. }
  332. /**
  333. * Release cached data held by this source. clearCache is a memory
  334. * hint: it never affects correctness or output, only how expensive
  335. * the next read is. Subclasses override; the base is a no-op so
  336. * every Source supports the call. Composite sources always recurse
  337. * into wrapped sources. When the same child is reachable via several
  338. * parents (e.g. modules shared across webpack chunks), pass a shared
  339. * `visited` WeakSet so each subtree is walked at most once.
  340. * Not safe to call concurrently with source/map/sourceAndMap/
  341. * streamChunks/updateHash on the same instance.
  342. * @param {ClearCacheOptions=} options selectors
  343. * @param {WeakSet<Source>=} visited de-duplication set shared across calls
  344. * @returns {void}
  345. */
  346. clearCache(options, visited) {
  347. if (visited !== undefined) {
  348. if (visited.has(this)) return;
  349. visited.add(this);
  350. }
  351. const clearSource = !options || options.source !== false;
  352. const clearMaps = !options || options.maps !== false;
  353. const clearParsedMap = options !== undefined && options.parsedMap === true;
  354. // For every dual-cached pair, drop the string form when the
  355. // buffer is also held — buffer is more compact in V8 (1 byte/char
  356. // vs 2) and the string rehydrates cheaply. Parsed object forms
  357. // of source maps are heavier but only dropped when
  358. // `parsedMap: true` is set, because re-parsing JSON is much more
  359. // expensive than `toString` from a buffer.
  360. if (clearSource) {
  361. if (
  362. this._valueAsString !== undefined &&
  363. this._valueAsBuffer !== undefined
  364. ) {
  365. this._valueAsString = undefined;
  366. }
  367. if (
  368. this._originalSourceAsString !== undefined &&
  369. this._originalSourceAsBuffer !== undefined
  370. ) {
  371. this._originalSourceAsString = undefined;
  372. }
  373. }
  374. if (clearMaps) {
  375. if (
  376. this._sourceMapAsString !== undefined &&
  377. this._sourceMapAsBuffer !== undefined
  378. ) {
  379. this._sourceMapAsString = undefined;
  380. }
  381. if (
  382. clearParsedMap &&
  383. this._sourceMapAsObject !== undefined &&
  384. (this._sourceMapAsBuffer !== undefined ||
  385. this._sourceMapAsString !== undefined)
  386. ) {
  387. this._sourceMapAsObject = undefined;
  388. }
  389. if (
  390. this._innerSourceMapAsString !== undefined &&
  391. this._innerSourceMapAsBuffer !== undefined
  392. ) {
  393. this._innerSourceMapAsString = undefined;
  394. }
  395. if (
  396. clearParsedMap &&
  397. this._innerSourceMapAsObject !== undefined &&
  398. (this._innerSourceMapAsBuffer !== undefined ||
  399. this._innerSourceMapAsString !== undefined)
  400. ) {
  401. this._innerSourceMapAsObject = undefined;
  402. }
  403. }
  404. }
  405. /**
  406. * @param {HashLike} hash hash
  407. * @returns {void}
  408. */
  409. updateHash(hash) {
  410. hash.update("SourceMapSource");
  411. hash.update(this.buffer());
  412. hash.update(this._sourceMapBuffer());
  413. if (this._hasOriginalSource) {
  414. hash.update(
  415. /** @type {Buffer} */
  416. (this._originalSourceBuffer()),
  417. );
  418. }
  419. if (this._hasInnerSourceMap) {
  420. hash.update(
  421. /** @type {Buffer} */
  422. (this._innerSourceMapBuffer()),
  423. );
  424. }
  425. hash.update(this._removeOriginalSource ? "true" : "false");
  426. }
  427. }
  428. module.exports = SourceMapSource;