Skip to content

Commit dcfe52f

Browse files
author
Florian Reiterer
committed
Fix: support for Node < v4
1 parent bd21d92 commit dcfe52f

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

.travis.yml

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ node_js:
55
- "9"
66
- "8"
77
- "6"
8+
- "4"
9+
- "0.10"
810
after_script:
911
- npm run coveralls
1012
branches:

index.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ function Concat(generateSourceMap, fileName, separator) {
1313
this.contentParts = [];
1414

1515
if (separator === undefined) {
16-
this.separator = Buffer.from('');
16+
this.separator = bufferFrom('');
1717
} else {
18-
this.separator = Buffer.from(separator);
18+
this.separator = bufferFrom(separator);
1919
}
2020

2121
if (this.sourceMapping) {
@@ -37,7 +37,7 @@ Concat.prototype.add = function(filePath, content, sourceMap) {
3737
filePath = filePath && unixStylePath(filePath);
3838

3939
if (!Buffer.isBuffer(content)) {
40-
content = Buffer.from(content);
40+
content = bufferFrom(content);
4141
}
4242

4343
if (this.contentParts.length !== 0) {
@@ -118,4 +118,16 @@ Object.defineProperty(Concat.prototype, 'sourceMap', {
118118
}
119119
});
120120

121+
function bufferFrom(content) {
122+
try {
123+
return Buffer.from(content);
124+
} catch(e) {
125+
if (Object.prototype.toString.call(content) !== '[object String]') {
126+
throw new TypeError("separator must be a string");
127+
}
128+
return new Buffer(content);
129+
}
130+
}
131+
Concat.bufferFrom = bufferFrom;
132+
121133
module.exports = Concat;

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "concat-with-sourcemaps",
3-
"version": "1.0.6",
3+
"version": "1.0.7",
44
"description": "Concatenate file contents with a custom separator and generate a source map",
55
"homepage": "http://github.com/floridoo/concat-with-sourcemaps",
66
"repository": "git://github.com/floridoo/concat-with-sourcemaps.git",

test/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function testCase(description, options) {
88
// content as Buffer
99
var concat = new Concat(options.sourceMapping, options.outFile, options.separator);
1010
options.input.forEach(function(input, i) {
11-
concat.add((input.fileName !== undefined ? input.fileName : 'test'+(i+1)), Buffer.from(input.content), input.sourceMap);
11+
concat.add((input.fileName !== undefined ? input.fileName : 'test'+(i+1)), Concat.bufferFrom(input.content), input.sourceMap);
1212
});
1313
t.equal(concat.content.toString(), options.output.content, 'should produce the right output');
1414
if (options.output.sourceMap)

0 commit comments

Comments
 (0)