OriginalSource.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 getGeneratedSourceInfo = require("./helpers/getGeneratedSourceInfo");
  9. const splitIntoLines = require("./helpers/splitIntoLines");
  10. const splitIntoPotentialTokens = require("./helpers/splitIntoPotentialTokens");
  11. const {
  12. isDualStringBufferCachingEnabled,
  13. } = require("./helpers/stringBufferUtils");
  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 OriginalSource extends Source {
  25. /**
  26. * @param {string | Buffer} value value
  27. * @param {string} name name
  28. */
  29. constructor(value, name) {
  30. super();
  31. const isBuffer = Buffer.isBuffer(value);
  32. /**
  33. * @private
  34. * @type {undefined | string}
  35. */
  36. this._value = isBuffer ? undefined : value;
  37. /**
  38. * @private
  39. * @type {undefined | Buffer}
  40. */
  41. this._valueAsBuffer = isBuffer ? value : undefined;
  42. this._name = name;
  43. }
  44. getName() {
  45. return this._name;
  46. }
  47. /**
  48. * @returns {SourceValue} source
  49. */
  50. source() {
  51. if (this._value === undefined) {
  52. const value =
  53. /** @type {Buffer} */
  54. (this._valueAsBuffer).toString("utf8");
  55. if (isDualStringBufferCachingEnabled()) {
  56. this._value = value;
  57. }
  58. return value;
  59. }
  60. return this._value;
  61. }
  62. buffer() {
  63. if (this._valueAsBuffer === undefined) {
  64. const value = Buffer.from(/** @type {string} */ (this._value), "utf8");
  65. if (isDualStringBufferCachingEnabled()) {
  66. this._valueAsBuffer = value;
  67. }
  68. return value;
  69. }
  70. return this._valueAsBuffer;
  71. }
  72. /**
  73. * @param {MapOptions=} options map options
  74. * @returns {RawSourceMap | null} map
  75. */
  76. map(options) {
  77. return getMap(this, options);
  78. }
  79. /**
  80. * @param {MapOptions=} options map options
  81. * @returns {SourceAndMap} source and map
  82. */
  83. sourceAndMap(options) {
  84. return getSourceAndMap(this, options);
  85. }
  86. /**
  87. * @param {Options} options options
  88. * @param {OnChunk} onChunk called for each chunk of code
  89. * @param {OnSource} onSource called for each source
  90. * @param {OnName} _onName called for each name
  91. * @returns {GeneratedSourceInfo} generated source info
  92. */
  93. streamChunks(options, onChunk, onSource, _onName) {
  94. if (this._value === undefined) {
  95. this._value =
  96. /** @type {Buffer} */
  97. (this._valueAsBuffer).toString("utf8");
  98. }
  99. onSource(0, this._name, this._value);
  100. const finalSource = Boolean(options && options.finalSource);
  101. if (!options || options.columns !== false) {
  102. // With column info we need to read all lines and split them
  103. const matches = splitIntoPotentialTokens(this._value);
  104. let line = 1;
  105. let column = 0;
  106. if (matches !== null) {
  107. for (const match of matches) {
  108. const isEndOfLine = match.endsWith("\n");
  109. if (isEndOfLine && match.length === 1) {
  110. if (!finalSource) onChunk(match, line, column, -1, -1, -1, -1);
  111. } else {
  112. const chunk = finalSource ? undefined : match;
  113. onChunk(chunk, line, column, 0, line, column, -1);
  114. }
  115. if (isEndOfLine) {
  116. line++;
  117. column = 0;
  118. } else {
  119. column += match.length;
  120. }
  121. }
  122. }
  123. return {
  124. generatedLine: line,
  125. generatedColumn: column,
  126. source: finalSource ? this._value : undefined,
  127. };
  128. } else if (finalSource) {
  129. // Without column info and with final source we only
  130. // need meta info to generate mapping
  131. const result = getGeneratedSourceInfo(this._value);
  132. const { generatedLine, generatedColumn } = result;
  133. if (generatedColumn === 0) {
  134. for (
  135. let line = 1;
  136. line < /** @type {number} */ (generatedLine);
  137. line++
  138. ) {
  139. onChunk(undefined, line, 0, 0, line, 0, -1);
  140. }
  141. } else {
  142. for (
  143. let line = 1;
  144. line <= /** @type {number} */ (generatedLine);
  145. line++
  146. ) {
  147. onChunk(undefined, line, 0, 0, line, 0, -1);
  148. }
  149. }
  150. return result;
  151. }
  152. // Without column info, but also without final source
  153. // we need to split source by lines
  154. let line = 1;
  155. const matches = splitIntoLines(this._value);
  156. /** @type {string | undefined} */
  157. let match;
  158. for (match of matches) {
  159. onChunk(finalSource ? undefined : match, line, 0, 0, line, 0, -1);
  160. line++;
  161. }
  162. return matches.length === 0 || /** @type {string} */ (match).endsWith("\n")
  163. ? {
  164. generatedLine: matches.length + 1,
  165. generatedColumn: 0,
  166. source: finalSource ? this._value : undefined,
  167. }
  168. : {
  169. generatedLine: matches.length,
  170. generatedColumn: /** @type {string} */ (match).length,
  171. source: finalSource ? this._value : undefined,
  172. };
  173. }
  174. /**
  175. * @param {HashLike} hash hash
  176. * @returns {void}
  177. */
  178. updateHash(hash) {
  179. hash.update("OriginalSource");
  180. hash.update(this.buffer());
  181. hash.update(this._name || "");
  182. }
  183. }
  184. module.exports = OriginalSource;