Skip to content

Commit

Permalink
Merge pull request #884 from huochunpeng/fix-concat-with-sourcemaps
Browse files Browse the repository at this point in the history
fix(bundler): fix compatibility with source-map >= v0.6
  • Loading branch information
JeroenVinke committed Jul 24, 2018
2 parents 7668908 + 681a77d commit 4aa0fcc
Showing 1 changed file with 28 additions and 19 deletions.
47 changes: 28 additions & 19 deletions lib/build/concat-with-sourcemaps/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ function Concat(generateSourceMap, fileName, separator) {
this.contentParts = [];

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

if (this.sourceMapping) {
Expand All @@ -37,18 +37,17 @@ Concat.prototype.add = function(filePath, content, sourceMap) {
filePath = filePath && unixStylePath(filePath);

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

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

var contentString = content.toString();
var lines = contentString.split('\n').length;

if (sourceMap && this.sourceMapping) {
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);
Expand All @@ -63,11 +62,11 @@ Concat.prototype.add = function(filePath, content, sourceMap) {
line: _this.lineOffset + mapping.generatedLine,
column: (mapping.generatedLine === 1 ? _this.columnOffset : 0) + mapping.generatedColumn
},
original: {
original: mapping.originalLine == null ? null : {
line: mapping.originalLine,
column: mapping.originalColumn
},
source: mapping.source,
source: mapping.originalLine != null ? mapping.source : null,
name: mapping.name
});
}
Expand Down Expand Up @@ -98,16 +97,13 @@ Concat.prototype.add = function(filePath, content, sourceMap) {
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;
}

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', {
Expand All @@ -122,4 +118,17 @@ Object.defineProperty(Concat.prototype, 'sourceMap', {
}
});

module.exports = Concat;
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;
Concat.default = Concat;

module.exports = Concat;

0 comments on commit 4aa0fcc

Please sign in to comment.