OriginalSource.js 5.4 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").Hash} Hash */
  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("utf-8");
  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), "utf-8");
  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("utf-8");
  98. }
  99. onSource(0, this._name, this._value);
  100. const finalSource = !!(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 (let line = 1; line < /** @type {number} */ (generatedLine); line++)
  135. onChunk(undefined, line, 0, 0, line, 0, -1);
  136. } else {
  137. for (
  138. let line = 1;
  139. line <= /** @type {number} */ (generatedLine);
  140. line++
  141. )
  142. onChunk(undefined, line, 0, 0, line, 0, -1);
  143. }
  144. return result;
  145. } else {
  146. // Without column info, but also without final source
  147. // we need to split source by lines
  148. let line = 1;
  149. const matches = splitIntoLines(this._value);
  150. /** @type {string | undefined} */
  151. let match;
  152. for (match of matches) {
  153. onChunk(finalSource ? undefined : match, line, 0, 0, line, 0, -1);
  154. line++;
  155. }
  156. return matches.length === 0 ||
  157. /** @type {string} */ (match).endsWith("\n")
  158. ? {
  159. generatedLine: matches.length + 1,
  160. generatedColumn: 0,
  161. source: finalSource ? this._value : undefined
  162. }
  163. : {
  164. generatedLine: matches.length,
  165. generatedColumn: /** @type {string} */ (match).length,
  166. source: finalSource ? this._value : undefined
  167. };
  168. }
  169. }
  170. /**
  171. * @param {Hash} hash hash
  172. * @returns {void}
  173. */
  174. updateHash(hash) {
  175. hash.update("OriginalSource");
  176. hash.update(
  177. this._valueAsBuffer
  178. ? /** @type {Buffer} */ (this._valueAsBuffer)
  179. : /** @type {string} */ (this._value)
  180. );
  181. hash.update(this._name || "");
  182. }
  183. }
  184. module.exports = OriginalSource;