SourceMapSource.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 streamChunksOfSourceMap = require("./helpers/streamChunksOfSourceMap");
  8. const streamChunksOfCombinedSourceMap = require("./helpers/streamChunksOfCombinedSourceMap");
  9. const { getMap, getSourceAndMap } = require("./helpers/getFromStreamChunks");
  10. class SourceMapSource extends Source {
  11. constructor(
  12. value,
  13. name,
  14. sourceMap,
  15. originalSource,
  16. innerSourceMap,
  17. removeOriginalSource
  18. ) {
  19. super();
  20. const valueIsBuffer = Buffer.isBuffer(value);
  21. this._valueAsString = valueIsBuffer ? undefined : value;
  22. this._valueAsBuffer = valueIsBuffer ? value : undefined;
  23. this._name = name;
  24. this._hasSourceMap = !!sourceMap;
  25. const sourceMapIsBuffer = Buffer.isBuffer(sourceMap);
  26. const sourceMapIsString = typeof sourceMap === "string";
  27. this._sourceMapAsObject =
  28. sourceMapIsBuffer || sourceMapIsString ? undefined : sourceMap;
  29. this._sourceMapAsString = sourceMapIsString ? sourceMap : undefined;
  30. this._sourceMapAsBuffer = sourceMapIsBuffer ? sourceMap : undefined;
  31. this._hasOriginalSource = !!originalSource;
  32. const originalSourceIsBuffer = Buffer.isBuffer(originalSource);
  33. this._originalSourceAsString = originalSourceIsBuffer
  34. ? undefined
  35. : originalSource;
  36. this._originalSourceAsBuffer = originalSourceIsBuffer
  37. ? originalSource
  38. : undefined;
  39. this._hasInnerSourceMap = !!innerSourceMap;
  40. const innerSourceMapIsBuffer = Buffer.isBuffer(innerSourceMap);
  41. const innerSourceMapIsString = typeof innerSourceMap === "string";
  42. this._innerSourceMapAsObject =
  43. innerSourceMapIsBuffer || innerSourceMapIsString
  44. ? undefined
  45. : innerSourceMap;
  46. this._innerSourceMapAsString = innerSourceMapIsString
  47. ? innerSourceMap
  48. : undefined;
  49. this._innerSourceMapAsBuffer = innerSourceMapIsBuffer
  50. ? innerSourceMap
  51. : undefined;
  52. this._removeOriginalSource = removeOriginalSource;
  53. }
  54. _ensureValueBuffer() {
  55. if (this._valueAsBuffer === undefined) {
  56. this._valueAsBuffer = Buffer.from(this._valueAsString, "utf-8");
  57. }
  58. }
  59. _ensureValueString() {
  60. if (this._valueAsString === undefined) {
  61. this._valueAsString = this._valueAsBuffer.toString("utf-8");
  62. }
  63. }
  64. _ensureOriginalSourceBuffer() {
  65. if (this._originalSourceAsBuffer === undefined && this._hasOriginalSource) {
  66. this._originalSourceAsBuffer = Buffer.from(
  67. this._originalSourceAsString,
  68. "utf-8"
  69. );
  70. }
  71. }
  72. _ensureOriginalSourceString() {
  73. if (this._originalSourceAsString === undefined && this._hasOriginalSource) {
  74. this._originalSourceAsString = this._originalSourceAsBuffer.toString(
  75. "utf-8"
  76. );
  77. }
  78. }
  79. _ensureInnerSourceMapObject() {
  80. if (this._innerSourceMapAsObject === undefined && this._hasInnerSourceMap) {
  81. this._ensureInnerSourceMapString();
  82. this._innerSourceMapAsObject = JSON.parse(this._innerSourceMapAsString);
  83. }
  84. }
  85. _ensureInnerSourceMapBuffer() {
  86. if (this._innerSourceMapAsBuffer === undefined && this._hasInnerSourceMap) {
  87. this._ensureInnerSourceMapString();
  88. this._innerSourceMapAsBuffer = Buffer.from(
  89. this._innerSourceMapAsString,
  90. "utf-8"
  91. );
  92. }
  93. }
  94. _ensureInnerSourceMapString() {
  95. if (this._innerSourceMapAsString === undefined && this._hasInnerSourceMap) {
  96. if (this._innerSourceMapAsBuffer !== undefined) {
  97. this._innerSourceMapAsString = this._innerSourceMapAsBuffer.toString(
  98. "utf-8"
  99. );
  100. } else {
  101. this._innerSourceMapAsString = JSON.stringify(
  102. this._innerSourceMapAsObject
  103. );
  104. }
  105. }
  106. }
  107. _ensureSourceMapObject() {
  108. if (this._sourceMapAsObject === undefined) {
  109. this._ensureSourceMapString();
  110. this._sourceMapAsObject = JSON.parse(this._sourceMapAsString);
  111. }
  112. }
  113. _ensureSourceMapBuffer() {
  114. if (this._sourceMapAsBuffer === undefined) {
  115. this._ensureSourceMapString();
  116. this._sourceMapAsBuffer = Buffer.from(this._sourceMapAsString, "utf-8");
  117. }
  118. }
  119. _ensureSourceMapString() {
  120. if (this._sourceMapAsString === undefined) {
  121. if (this._sourceMapAsBuffer !== undefined) {
  122. this._sourceMapAsString = this._sourceMapAsBuffer.toString("utf-8");
  123. } else {
  124. this._sourceMapAsString = JSON.stringify(this._sourceMapAsObject);
  125. }
  126. }
  127. }
  128. getArgsAsBuffers() {
  129. this._ensureValueBuffer();
  130. this._ensureSourceMapBuffer();
  131. this._ensureOriginalSourceBuffer();
  132. this._ensureInnerSourceMapBuffer();
  133. return [
  134. this._valueAsBuffer,
  135. this._name,
  136. this._sourceMapAsBuffer,
  137. this._originalSourceAsBuffer,
  138. this._innerSourceMapAsBuffer,
  139. this._removeOriginalSource
  140. ];
  141. }
  142. buffer() {
  143. this._ensureValueBuffer();
  144. return this._valueAsBuffer;
  145. }
  146. source() {
  147. this._ensureValueString();
  148. return this._valueAsString;
  149. }
  150. map(options) {
  151. if (!this._hasInnerSourceMap) {
  152. this._ensureSourceMapObject();
  153. return this._sourceMapAsObject;
  154. }
  155. return getMap(this, options);
  156. }
  157. sourceAndMap(options) {
  158. if (!this._hasInnerSourceMap) {
  159. this._ensureValueString();
  160. this._ensureSourceMapObject();
  161. return {
  162. source: this._valueAsString,
  163. map: this._sourceMapAsObject
  164. };
  165. }
  166. return getSourceAndMap(this, options);
  167. }
  168. streamChunks(options, onChunk, onSource, onName) {
  169. this._ensureValueString();
  170. this._ensureSourceMapObject();
  171. this._ensureOriginalSourceString();
  172. if (this._hasInnerSourceMap) {
  173. this._ensureInnerSourceMapObject();
  174. return streamChunksOfCombinedSourceMap(
  175. this._valueAsString,
  176. this._sourceMapAsObject,
  177. this._name,
  178. this._originalSourceAsString,
  179. this._innerSourceMapAsObject,
  180. this._removeOriginalSource,
  181. onChunk,
  182. onSource,
  183. onName,
  184. !!(options && options.finalSource),
  185. !!(options && options.columns !== false)
  186. );
  187. } else {
  188. return streamChunksOfSourceMap(
  189. this._valueAsString,
  190. this._sourceMapAsObject,
  191. onChunk,
  192. onSource,
  193. onName,
  194. !!(options && options.finalSource),
  195. !!(options && options.columns !== false)
  196. );
  197. }
  198. }
  199. updateHash(hash) {
  200. this._ensureValueBuffer();
  201. this._ensureSourceMapBuffer();
  202. this._ensureOriginalSourceBuffer();
  203. this._ensureInnerSourceMapBuffer();
  204. hash.update("SourceMapSource");
  205. hash.update(this._valueAsBuffer);
  206. hash.update(this._sourceMapAsBuffer);
  207. if (this._hasOriginalSource) {
  208. hash.update(this._originalSourceAsBuffer);
  209. }
  210. if (this._hasInnerSourceMap) {
  211. hash.update(this._innerSourceMapAsBuffer);
  212. }
  213. hash.update(this._removeOriginalSource ? "true" : "false");
  214. }
  215. }
  216. module.exports = SourceMapSource;