You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SourceListMap.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const CodeNode = require("./CodeNode");
  7. const SourceNode = require("./SourceNode");
  8. const MappingsContext = require("./MappingsContext");
  9. const getNumberOfLines = require("./helpers").getNumberOfLines;
  10. class SourceListMap {
  11. constructor(generatedCode, source, originalSource) {
  12. if(Array.isArray(generatedCode)) {
  13. this.children = generatedCode;
  14. } else {
  15. this.children = [];
  16. if(generatedCode || source)
  17. this.add(generatedCode, source, originalSource);
  18. }
  19. }
  20. add(generatedCode, source, originalSource) {
  21. if(typeof generatedCode === "string") {
  22. if(source) {
  23. this.children.push(new SourceNode(generatedCode, source, originalSource));
  24. } else if(this.children.length > 0 && this.children[this.children.length - 1] instanceof CodeNode) {
  25. this.children[this.children.length - 1].addGeneratedCode(generatedCode);
  26. } else {
  27. this.children.push(new CodeNode(generatedCode));
  28. }
  29. } else if(generatedCode.getMappings && generatedCode.getGeneratedCode) {
  30. this.children.push(generatedCode);
  31. } else if(generatedCode.children) {
  32. generatedCode.children.forEach(function(sln) {
  33. this.children.push(sln);
  34. }, this);
  35. } else {
  36. throw new Error("Invalid arguments to SourceListMap.protfotype.add: Expected string, Node or SourceListMap");
  37. }
  38. };
  39. preprend(generatedCode, source, originalSource) {
  40. if(typeof generatedCode === "string") {
  41. if(source) {
  42. this.children.unshift(new SourceNode(generatedCode, source, originalSource));
  43. } else if(this.children.length > 0 && this.children[this.children.length - 1].preprendGeneratedCode) {
  44. this.children[this.children.length - 1].preprendGeneratedCode(generatedCode);
  45. } else {
  46. this.children.unshift(new CodeNode(generatedCode));
  47. }
  48. } else if(generatedCode.getMappings && generatedCode.getGeneratedCode) {
  49. this.children.unshift(generatedCode);
  50. } else if(generatedCode.children) {
  51. generatedCode.children.slice().reverse().forEach(function(sln) {
  52. this.children.unshift(sln);
  53. }, this);
  54. } else {
  55. throw new Error("Invalid arguments to SourceListMap.protfotype.prerend: Expected string, Node or SourceListMap");
  56. }
  57. };
  58. mapGeneratedCode(fn) {
  59. const normalizedNodes = [];
  60. this.children.forEach(function(sln) {
  61. sln.getNormalizedNodes().forEach(function(newNode) {
  62. normalizedNodes.push(newNode);
  63. });
  64. });
  65. const optimizedNodes = [];
  66. normalizedNodes.forEach(function(sln) {
  67. sln = sln.mapGeneratedCode(fn);
  68. if(optimizedNodes.length === 0) {
  69. optimizedNodes.push(sln);
  70. } else {
  71. const last = optimizedNodes[optimizedNodes.length - 1];
  72. const mergedNode = last.merge(sln);
  73. if(mergedNode) {
  74. optimizedNodes[optimizedNodes.length - 1] = mergedNode;
  75. } else {
  76. optimizedNodes.push(sln);
  77. }
  78. }
  79. });
  80. return new SourceListMap(optimizedNodes);
  81. };
  82. toString() {
  83. return this.children.map(function(sln) {
  84. return sln.getGeneratedCode();
  85. }).join("");
  86. };
  87. toStringWithSourceMap(options) {
  88. const mappingsContext = new MappingsContext();
  89. const source = this.children.map(function(sln) {
  90. return sln.getGeneratedCode();
  91. }).join("");
  92. const mappings = this.children.map(function(sln) {
  93. return sln.getMappings(mappingsContext);
  94. }).join("");
  95. const arrays = mappingsContext.getArrays();
  96. return {
  97. source,
  98. map: {
  99. version: 3,
  100. file: options && options.file,
  101. sources: arrays.sources,
  102. sourcesContent: mappingsContext.hasSourceContent ? arrays.sourcesContent : undefined,
  103. mappings: mappings
  104. }
  105. };
  106. }
  107. }
  108. module.exports = SourceListMap;