File: index.js

package info (click to toggle)
node-concat-with-sourcemaps 1.0.7-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 104 kB
  • sloc: makefile: 2
file content (133 lines) | stat: -rw-r--r-- 4,118 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
'use strict';
var SourceMapGenerator = require('source-map').SourceMapGenerator;
var SourceMapConsumer = require('source-map').SourceMapConsumer;

function unixStylePath(filePath) {
  return filePath.replace(/\\/g, '/');
}

function Concat(generateSourceMap, fileName, separator) {
  this.lineOffset = 0;
  this.columnOffset = 0;
  this.sourceMapping = generateSourceMap;
  this.contentParts = [];

  if (separator === undefined) {
    this.separator = bufferFrom('');
  } else {
    this.separator = bufferFrom(separator);
  }

  if (this.sourceMapping) {
    this._sourceMap = new SourceMapGenerator({file: unixStylePath(fileName)});
    this.separatorLineOffset = 0;
    this.separatorColumnOffset = 0;
    var separatorString = this.separator.toString();
    for (var i = 0; i < separatorString.length; i++) {
      this.separatorColumnOffset++;
      if (separatorString[i] === '\n') {
        this.separatorLineOffset++;
        this.separatorColumnOffset = 0;
      }
    }
  }
}

Concat.prototype.add = function(filePath, content, sourceMap) {
  filePath = filePath && unixStylePath(filePath);

  if (!Buffer.isBuffer(content)) {
    content = bufferFrom(content);
  }

  if (this.contentParts.length !== 0) {
    this.contentParts.push(this.separator);
  }
  this.contentParts.push(content);

  if (this.sourceMapping) {
    var contentString = content.toString();
    var lines = contentString.split('\n').length;

    if (Object.prototype.toString.call(sourceMap) === '[object String]')
      sourceMap = JSON.parse(sourceMap);

    if (sourceMap && sourceMap.mappings && sourceMap.mappings.length > 0) {
      var upstreamSM = new SourceMapConsumer(sourceMap);
      var _this = this;
      upstreamSM.eachMapping(function(mapping) {
        if (mapping.source) {
          _this._sourceMap.addMapping({
            generated: {
              line: _this.lineOffset + mapping.generatedLine,
              column: (mapping.generatedLine === 1 ? _this.columnOffset : 0) + mapping.generatedColumn
            },
            original: mapping.originalLine == null ? null : {
              line: mapping.originalLine,
              column: mapping.originalColumn
            },
            source: mapping.originalLine != null ? mapping.source : null,
            name: mapping.name
          });
        }
      });
      if (upstreamSM.sourcesContent) {
        upstreamSM.sourcesContent.forEach(function(sourceContent, i) {
          _this._sourceMap.setSourceContent(upstreamSM.sources[i], sourceContent);
        });
      }
    } else {
      if (sourceMap && sourceMap.sources && sourceMap.sources.length > 0)
        filePath = sourceMap.sources[0];
      if (filePath) {
        for (var i = 1; i <= lines; i++) {
          this._sourceMap.addMapping({
            generated: {
              line: this.lineOffset + i,
              column: (i === 1 ? this.columnOffset : 0)
            },
            original: {
              line: i,
              column: 0
            },
            source: filePath
          });
        }
        if (sourceMap && sourceMap.sourcesContent)
          this._sourceMap.setSourceContent(filePath, sourceMap.sourcesContent[0]);
      }
    }
    if (lines > 1)
      this.columnOffset = 0;
    if (this.separatorLineOffset === 0)
      this.columnOffset += contentString.length - Math.max(0, contentString.lastIndexOf('\n')+1);
    this.columnOffset += this.separatorColumnOffset;
    this.lineOffset += lines - 1 + this.separatorLineOffset;
  }
};

Object.defineProperty(Concat.prototype, 'content', {
  get: function content() {
    return Buffer.concat(this.contentParts);
  }
});

Object.defineProperty(Concat.prototype, 'sourceMap', {
  get: function sourceMap() {
    return this._sourceMap ? this._sourceMap.toString() : undefined;
  }
});

function bufferFrom(content) {
  try {
    return Buffer.from(content);
  } catch(e) {
    if (Object.prototype.toString.call(content) !== '[object String]') {
      throw new TypeError("separator must be a string");
    }
    return new Buffer(content);
  }
}
Concat.bufferFrom = bufferFrom;

module.exports = Concat;