index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. 'use strict';
  2. var SourceMapGenerator = require('source-map').SourceMapGenerator;
  3. var SourceMapConsumer = require('source-map').SourceMapConsumer;
  4. function unixStylePath(filePath) {
  5. return filePath.replace(/\\/g, '/');
  6. }
  7. function Concat(generateSourceMap, fileName, separator) {
  8. this.lineOffset = 0;
  9. this.columnOffset = 0;
  10. this.sourceMapping = generateSourceMap;
  11. this.contentParts = [];
  12. if (separator === undefined) {
  13. this.separator = bufferFrom('');
  14. } else {
  15. this.separator = bufferFrom(separator);
  16. }
  17. if (this.sourceMapping) {
  18. this._sourceMap = new SourceMapGenerator({file: unixStylePath(fileName)});
  19. this.separatorLineOffset = 0;
  20. this.separatorColumnOffset = 0;
  21. var separatorString = this.separator.toString();
  22. for (var i = 0; i < separatorString.length; i++) {
  23. this.separatorColumnOffset++;
  24. if (separatorString[i] === '\n') {
  25. this.separatorLineOffset++;
  26. this.separatorColumnOffset = 0;
  27. }
  28. }
  29. }
  30. }
  31. Concat.prototype.add = function(filePath, content, sourceMap) {
  32. filePath = filePath && unixStylePath(filePath);
  33. if (!Buffer.isBuffer(content)) {
  34. content = bufferFrom(content);
  35. }
  36. if (this.contentParts.length !== 0) {
  37. this.contentParts.push(this.separator);
  38. }
  39. this.contentParts.push(content);
  40. if (this.sourceMapping) {
  41. var contentString = content.toString();
  42. var lines = contentString.split('\n').length;
  43. if (Object.prototype.toString.call(sourceMap) === '[object String]')
  44. sourceMap = JSON.parse(sourceMap);
  45. if (sourceMap && sourceMap.mappings && sourceMap.mappings.length > 0) {
  46. var upstreamSM = new SourceMapConsumer(sourceMap);
  47. var _this = this;
  48. upstreamSM.eachMapping(function(mapping) {
  49. if (mapping.source) {
  50. _this._sourceMap.addMapping({
  51. generated: {
  52. line: _this.lineOffset + mapping.generatedLine,
  53. column: (mapping.generatedLine === 1 ? _this.columnOffset : 0) + mapping.generatedColumn
  54. },
  55. original: mapping.originalLine == null ? null : {
  56. line: mapping.originalLine,
  57. column: mapping.originalColumn
  58. },
  59. source: mapping.originalLine != null ? mapping.source : null,
  60. name: mapping.name
  61. });
  62. }
  63. });
  64. if (upstreamSM.sourcesContent) {
  65. upstreamSM.sourcesContent.forEach(function(sourceContent, i) {
  66. _this._sourceMap.setSourceContent(upstreamSM.sources[i], sourceContent);
  67. });
  68. }
  69. } else {
  70. if (sourceMap && sourceMap.sources && sourceMap.sources.length > 0)
  71. filePath = sourceMap.sources[0];
  72. if (filePath) {
  73. for (var i = 1; i <= lines; i++) {
  74. this._sourceMap.addMapping({
  75. generated: {
  76. line: this.lineOffset + i,
  77. column: (i === 1 ? this.columnOffset : 0)
  78. },
  79. original: {
  80. line: i,
  81. column: 0
  82. },
  83. source: filePath
  84. });
  85. }
  86. if (sourceMap && sourceMap.sourcesContent)
  87. this._sourceMap.setSourceContent(filePath, sourceMap.sourcesContent[0]);
  88. }
  89. }
  90. if (lines > 1)
  91. this.columnOffset = 0;
  92. if (this.separatorLineOffset === 0)
  93. this.columnOffset += contentString.length - Math.max(0, contentString.lastIndexOf('\n')+1);
  94. this.columnOffset += this.separatorColumnOffset;
  95. this.lineOffset += lines - 1 + this.separatorLineOffset;
  96. }
  97. };
  98. Object.defineProperty(Concat.prototype, 'content', {
  99. get: function content() {
  100. return Buffer.concat(this.contentParts);
  101. }
  102. });
  103. Object.defineProperty(Concat.prototype, 'sourceMap', {
  104. get: function sourceMap() {
  105. return this._sourceMap ? this._sourceMap.toString() : undefined;
  106. }
  107. });
  108. function bufferFrom(content) {
  109. try {
  110. return Buffer.from(content);
  111. } catch(e) {
  112. if (Object.prototype.toString.call(content) !== '[object String]') {
  113. throw new TypeError("separator must be a string");
  114. }
  115. return new Buffer(content);
  116. }
  117. }
  118. Concat.bufferFrom = bufferFrom;
  119. Concat.default = Concat;
  120. module.exports = Concat;