Skip to content

Commit 720590d

Browse files
committed
Breaking: Error in .add if file is stream, callback when null
1 parent da3ba78 commit 720590d

File tree

2 files changed

+51
-23
lines changed

2 files changed

+51
-23
lines changed

index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@ function add(file, callback) {
1313
return callback(new Error(PLUGIN_NAME + '-add: Not a vinyl file'));
1414
}
1515

16-
// Bail early successfully if file already has sourcemap
17-
if (file.sourceMap) {
16+
// Bail early with an error if file has streaming contents
17+
if (file.isStream()) {
18+
return callback(new Error(PLUGIN_NAME + '-add: Streaming not supported'));
19+
}
20+
21+
// Bail early successfully if file is null or already has a sourcemap
22+
if (file.isNull() || file.sourceMap) {
1823
return callback(null, file);
1924
}
2025

test/add.js

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ var File = require('vinyl');
55
var path = require('path');
66
var expect = require('expect');
77
var convert = require('convert-source-map');
8+
var miss = require('mississippi');
89

910
var sourcemaps = require('..');
1011

12+
var from = miss.from;
13+
1114
var sourceContent = fs.readFileSync(path.join(__dirname, 'assets/helloworld.js'));
1215

1316
function makeFile() {
@@ -64,14 +67,54 @@ describe('add', function() {
6467
});
6568
});
6669

67-
it('does not error if file argument is a vinyl object', function(done) {
70+
it('does not error if file argument is a Vinyl object with Buffer contents', function(done) {
6871
var file = makeFile();
6972
sourcemaps.add(file, function(err) {
7073
expect(err).toNotExist();
7174
done();
7275
});
7376
});
7477

78+
it('errors if file argument is a Vinyl object with Stream contents', function(done) {
79+
var file = makeFile();
80+
file.contents = from([]);
81+
sourcemaps.add(file, function(err) {
82+
expect(err instanceof Error && err.message === 'vinyl-sourcemap-add: Streaming not supported').toExist();
83+
done();
84+
});
85+
});
86+
87+
it('calls back with the untouched file if file already has a sourcemap', function(done) {
88+
var sourceMap = {
89+
version: 3,
90+
names: [],
91+
mappings: '',
92+
sources: ['test.js'],
93+
sourcesContent: ['testContent'],
94+
};
95+
96+
var file = makeFile();
97+
file.sourceMap = sourceMap;
98+
sourcemaps.add(file, function(err, data) {
99+
expect(data).toExist();
100+
expect(File.isVinyl(data)).toEqual(true);
101+
expect(data.sourceMap).toBe(sourceMap);
102+
expect(data).toBe(file);
103+
done(err);
104+
});
105+
});
106+
107+
it('calls back with the untouched file if file contents are null', function(done) {
108+
var file = makeFile();
109+
file.contents = null;
110+
sourcemaps.add(file, function(err, outFile) {
111+
expect(err).toNotExist();
112+
expect(file).toExist();
113+
expect(outFile).toEqual(file);
114+
done(err);
115+
});
116+
});
117+
75118
it('adds an empty sourceMap if none are found', function(done) {
76119
sourcemaps.add(makeFile(), function(err, data) {
77120
expect(data.sourceMap).toExist();
@@ -301,24 +344,4 @@ describe('add', function() {
301344
done(err);
302345
});
303346
});
304-
305-
it('passes file through when it already has a sourcemap', function(done) {
306-
var sourceMap = {
307-
version: 3,
308-
names: [],
309-
mappings: '',
310-
sources: ['test.js'],
311-
sourcesContent: ['testContent'],
312-
};
313-
314-
var file = makeFile();
315-
file.sourceMap = sourceMap;
316-
sourcemaps.add(file, function(err, data) {
317-
expect(data).toExist();
318-
expect(File.isVinyl(data)).toEqual(true);
319-
expect(data.sourceMap).toBe(sourceMap);
320-
expect(data).toBe(file);
321-
done(err);
322-
});
323-
});
324347
});

0 commit comments

Comments
 (0)