getFromStreamChunks.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const createMappingsSerializer = require("./createMappingsSerializer");
  7. exports.getSourceAndMap = (inputSource, options) => {
  8. let code = "";
  9. let mappings = "";
  10. let sources = [];
  11. let sourcesContent = [];
  12. let names = [];
  13. const addMapping = createMappingsSerializer(options);
  14. const { source } = inputSource.streamChunks(
  15. Object.assign({}, options, { finalSource: true }),
  16. (
  17. chunk,
  18. generatedLine,
  19. generatedColumn,
  20. sourceIndex,
  21. originalLine,
  22. originalColumn,
  23. nameIndex
  24. ) => {
  25. if (chunk !== undefined) code += chunk;
  26. mappings += addMapping(
  27. generatedLine,
  28. generatedColumn,
  29. sourceIndex,
  30. originalLine,
  31. originalColumn,
  32. nameIndex
  33. );
  34. },
  35. (sourceIndex, source, sourceContent) => {
  36. while (sources.length < sourceIndex) {
  37. sources.push(null);
  38. }
  39. sources[sourceIndex] = source;
  40. if (sourceContent !== undefined) {
  41. while (sourcesContent.length < sourceIndex) {
  42. sourcesContent.push(null);
  43. }
  44. sourcesContent[sourceIndex] = sourceContent;
  45. }
  46. },
  47. (nameIndex, name) => {
  48. while (names.length < nameIndex) {
  49. names.push(null);
  50. }
  51. names[nameIndex] = name;
  52. }
  53. );
  54. return {
  55. source: source !== undefined ? source : code,
  56. map:
  57. mappings.length > 0
  58. ? {
  59. version: 3,
  60. file: "x",
  61. mappings,
  62. sources,
  63. sourcesContent:
  64. sourcesContent.length > 0 ? sourcesContent : undefined,
  65. names
  66. }
  67. : null
  68. };
  69. };
  70. exports.getMap = (source, options) => {
  71. let mappings = "";
  72. let sources = [];
  73. let sourcesContent = [];
  74. let names = [];
  75. const addMapping = createMappingsSerializer(options);
  76. source.streamChunks(
  77. Object.assign({}, options, { source: false, finalSource: true }),
  78. (
  79. chunk,
  80. generatedLine,
  81. generatedColumn,
  82. sourceIndex,
  83. originalLine,
  84. originalColumn,
  85. nameIndex
  86. ) => {
  87. mappings += addMapping(
  88. generatedLine,
  89. generatedColumn,
  90. sourceIndex,
  91. originalLine,
  92. originalColumn,
  93. nameIndex
  94. );
  95. },
  96. (sourceIndex, source, sourceContent) => {
  97. while (sources.length < sourceIndex) {
  98. sources.push(null);
  99. }
  100. sources[sourceIndex] = source;
  101. if (sourceContent !== undefined) {
  102. while (sourcesContent.length < sourceIndex) {
  103. sourcesContent.push(null);
  104. }
  105. sourcesContent[sourceIndex] = sourceContent;
  106. }
  107. },
  108. (nameIndex, name) => {
  109. while (names.length < nameIndex) {
  110. names.push(null);
  111. }
  112. names[nameIndex] = name;
  113. }
  114. );
  115. return mappings.length > 0
  116. ? {
  117. version: 3,
  118. file: "x",
  119. mappings,
  120. sources,
  121. sourcesContent: sourcesContent.length > 0 ? sourcesContent : undefined,
  122. names
  123. }
  124. : null;
  125. };