Skip to content

Commit ea56f4c

Browse files
committed
Breaking: Remove options from the API
1 parent 06f26ac commit ea56f4c

File tree

3 files changed

+20
-155
lines changed

3 files changed

+20
-155
lines changed

index.js

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,13 @@
11
'use strict';
22

33
var File = require('vinyl');
4-
var defaults = require('object.defaults');
54
var normalizePath = require('normalize-path');
65

76
var helpers = require('./lib/helpers');
87

98
var PLUGIN_NAME = 'vinyl-sourcemap';
109

11-
function isObject(value) {
12-
return value && typeof value === 'object' && !Array.isArray(value);
13-
}
14-
15-
function add(file, options, callback) {
16-
17-
// Check if options or a callback are passed as second argument
18-
if (typeof options === 'function') {
19-
callback = options;
20-
options = {};
21-
}
22-
23-
// Default options if not an object
24-
if (!isObject(options)) {
25-
options = {};
26-
}
10+
function add(file, callback) {
2711

2812
// Bail early an error if the file argument is not a Vinyl file
2913
if (!File.isVinyl(file)) {
@@ -45,17 +29,12 @@ function add(file, options, callback) {
4529
helpers.addSourceMaps(file, state, callback);
4630
}
4731

48-
function write(file, options, callback) {
32+
function write(file, destPath, callback) {
4933

5034
// Check if options or a callback are passed as second argument
51-
if (typeof options === 'function') {
52-
callback = options;
53-
options = {};
54-
}
55-
56-
// Default options if not an object
57-
if (!isObject(options)) {
58-
options = {};
35+
if (typeof destPath === 'function') {
36+
callback = destPath;
37+
destPath = undefined;
5938
}
6039

6140
// Bail early with an error if the file argument is not a Vinyl file
@@ -75,9 +54,6 @@ function write(file, options, callback) {
7554
return callback(null, file);
7655
}
7756

78-
// Set defaults for options if unset
79-
var opts = defaults({}, options);
80-
8157
var sourceMap = file.sourceMap;
8258

8359
// fix paths if Windows style paths
@@ -97,7 +73,7 @@ function write(file, options, callback) {
9773
}
9874

9975
var state = {
100-
destPath: opts.path,
76+
destPath: destPath,
10177
sourceMap: sourceMap,
10278
sourceMapFile: null,
10379
};

test/add.js

Lines changed: 10 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -79,79 +79,6 @@ describe('add', function() {
7979
});
8080
});
8181

82-
describe('ensures options argument', function() {
83-
84-
// Currently no options are defaulted
85-
// TODO: Enable test if any options are defaulted
86-
it.skip('is not mutated', function(done) {
87-
var defaultedOpts = {};
88-
89-
var opts = {};
90-
91-
var file = makeFile();
92-
sourcemaps.add(file, opts, function(err) {
93-
expect(opts).toNotEqual(defaultedOpts);
94-
done(err);
95-
});
96-
});
97-
98-
it('is defaulted if undefined', function(done) {
99-
var file = makeFile();
100-
sourcemaps.add(file, undefined, function(err) {
101-
expect(err).toNotExist();
102-
done();
103-
});
104-
});
105-
106-
it('is defaulted if null', function(done) {
107-
var file = makeFile();
108-
sourcemaps.add(file, null, function(err) {
109-
expect(err).toNotExist();
110-
done();
111-
});
112-
});
113-
114-
it('is defaulted if empty string', function(done) {
115-
var file = makeFile();
116-
sourcemaps.add(file, '', function(err) {
117-
expect(err).toNotExist();
118-
done();
119-
});
120-
});
121-
122-
it('is defaulted if non-empty string', function(done) {
123-
var file = makeFile();
124-
sourcemaps.add(file, 'invalid', function(err) {
125-
expect(err).toNotExist();
126-
done();
127-
});
128-
});
129-
130-
it('is defaulted if boolean false', function(done) {
131-
var file = makeFile();
132-
sourcemaps.add(file, false, function(err) {
133-
expect(err).toNotExist();
134-
done();
135-
});
136-
});
137-
138-
it('is defaulted if boolean true', function(done) {
139-
var file = makeFile();
140-
sourcemaps.add(file, true, function(err) {
141-
expect(err).toNotExist();
142-
done();
143-
});
144-
});
145-
146-
it('is defaulted if array', function(done) {
147-
var file = makeFile();
148-
sourcemaps.add(file, [], function(err) {
149-
expect(err).toNotExist();
150-
done();
151-
});
152-
});
153-
});
154-
15582
it('should add an empty sourceMap', function(done) {
15683
sourcemaps.add(makeFile(), function(err, data) {
15784
expect(File.isVinyl(data)).toExist();
@@ -166,7 +93,7 @@ describe('add', function() {
16693
});
16794

16895
it('should import an existing inline source map', function(done) {
169-
sourcemaps.add(makeFileWithInlineSourceMap(), { loadMaps: true }, function(err, data) {
96+
sourcemaps.add(makeFileWithInlineSourceMap(), function(err, data) {
17097
expect(data).toExist();
17198
expect(data instanceof File).toExist();
17299
expect(data.sourceMap).toExist();
@@ -179,7 +106,7 @@ describe('add', function() {
179106
});
180107

181108
it('should remove inline source', function(done) {
182-
sourcemaps.add(makeFileWithInlineSourceMap(), { loadMaps: true }, function(err, data) {
109+
sourcemaps.add(makeFileWithInlineSourceMap(), function(err, data) {
183110
expect(/sourceMappingURL/.test(data.contents.toString())).toNotExist();
184111
done(err);
185112
});
@@ -188,7 +115,7 @@ describe('add', function() {
188115
it('should load external source map file reference in comment with \/\/# syntax', function(done) {
189116
var file = makeFile();
190117
file.contents = new Buffer(sourceContent + '\n//# sourceMappingURL=helloworld2.js.map');
191-
sourcemaps.add(file, { loadMaps: true }, function(err, data) {
118+
sourcemaps.add(file, function(err, data) {
192119
expect(data.sourceMap).toExist();
193120
expect(String(data.sourceMap.version)).toBe('3');
194121
expect(data.sourceMap.sources).toEqual(['helloworld2.js']);
@@ -201,7 +128,7 @@ describe('add', function() {
201128
it('should remove source map comment with the \/\/# syntax', function(done) {
202129
var file = makeFile();
203130
file.contents = new Buffer(sourceContent + '\n//# sourceMappingURL=helloworld2.js.map');
204-
sourcemaps.add(file, { loadMaps: true }, function(err, data) {
131+
sourcemaps.add(file, function(err, data) {
205132
expect(/sourceMappingURL/.test(data.contents.toString())).toNotExist();
206133
done(err);
207134
});
@@ -210,7 +137,7 @@ describe('add', function() {
210137
it('should load external source map if no source mapping comment', function (done) {
211138
var file = makeFile();
212139
file.path = file.path.replace('helloworld.js', 'helloworld2.js');
213-
sourcemaps.add(file, { loadMaps: true }, function(err, data) {
140+
sourcemaps.add(file, function(err, data) {
214141
expect(data.sourceMap).toExist();
215142
expect(String(data.sourceMap.version)).toBe('3');
216143
expect(data.sourceMap.sources).toEqual(['helloworld2.js']);
@@ -223,7 +150,7 @@ describe('add', function() {
223150
it('should load external source map and add sourceContent if missing', function(done) {
224151
var file = makeFile();
225152
file.contents = new Buffer(sourceContent + '\n//# sourceMappingURL=helloworld3.js.map');
226-
sourcemaps.add(file, { loadMaps: true }, function(err, data) {
153+
sourcemaps.add(file, function(err, data) {
227154
expect(data.sourceMap).toExist();
228155
expect(String(data.sourceMap.version)).toBe('3');
229156
expect(data.sourceMap.sources).toEqual(['helloworld.js', 'test1.js']);
@@ -236,7 +163,7 @@ describe('add', function() {
236163
it('should not throw when source file for sourceContent not found', function(done) {
237164
var file = makeFile();
238165
file.contents = new Buffer(sourceContent + '\n//# sourceMappingURL=helloworld4.js.map');
239-
sourcemaps.add(file, { loadMaps: true }, function(err, data) {
166+
sourcemaps.add(file, function(err, data) {
240167
expect(data.sourceMap).toExist();
241168
expect(String(data.sourceMap.version)).toBe('3');
242169
expect(data.sourceMap.sources).toEqual(['helloworld.js', 'missingfile']);
@@ -259,7 +186,7 @@ describe('add', function() {
259186
it('should use sourceRoot when resolving path to sources', function(done) {
260187
var file = makeFile();
261188
file.contents = new Buffer(sourceContent + '\n//# sourceMappingURL=helloworld5.js.map');
262-
sourcemaps.add(file, { loadMaps:true }, function(err, data) {
189+
sourcemaps.add(file, function(err, data) {
263190
expect(data.sourceMap).toExist([]);
264191
expect(String(data.sourceMap.version)).toBe('3');
265192
expect(data.sourceMap.sources).toEqual(['../helloworld.js', '../test1.js']);
@@ -273,7 +200,7 @@ describe('add', function() {
273200
it('should not load source conent if the path is a url', function(done) {
274201
var file = makeFile();
275202
file.contents = new Buffer(sourceContent + '\n//# sourceMappingURL=helloworld6.js.map');
276-
sourcemaps.add(file, { loadMaps: true }, function(err, data) {
203+
sourcemaps.add(file, function(err, data) {
277204
expect(data.sourceMap).toExist();
278205
expect(String(data.sourceMap.version)).toBe('3');
279206
expect(data.sourceMap.sources).toEqual(['helloworld.js', 'http://example2.com/test1.js']);
@@ -283,17 +210,6 @@ describe('add', function() {
283210
});
284211
});
285212

286-
it.skip('should output an error message if debug option is set and sourceContent is missing', function (done) {
287-
var file = makeFile();
288-
file.contents = new Buffer(sourceContent + '\n//# sourceMappingURL=helloworld4.js.map');
289-
var hConsole = ''; // Removed
290-
sourcemaps.add(file, { loadMaps: true, debug: true }, function(err) {
291-
expect(hConsole.history.log[0]).toEqual('vinyl-sourcemap-add: No source content for "missingfile". Loading from file.');
292-
expect(hConsole.history.warn[0].indexOf('vinyl-sourcemap-add: source file not found: ') === 0).toExist();
293-
done(err);
294-
});
295-
});
296-
297213
it('should pass through whe file already has a source map', function(done) {
298214
var sourceMap = {
299215
version: 3,
@@ -305,7 +221,7 @@ describe('add', function() {
305221

306222
var file = makeFile();
307223
file.sourceMap = sourceMap;
308-
sourcemaps.add(file, { loadMaps:true }, function(err, data) {
224+
sourcemaps.add(file, function(err, data) {
309225
expect(data).toExist();
310226
expect(data instanceof File).toExist();
311227
expect(data.sourceMap).toBe(sourceMap);

test/write.js

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ describe('write', function() {
9090
});
9191
});
9292

93-
describe('ensures options argument', function() {
93+
describe.skip('ensures destPath argument', function() {
9494

9595
it('is not mutated', function(done) {
9696
var defaultedOpts = {
@@ -217,7 +217,7 @@ describe('write', function() {
217217

218218
it.skip('should write external map files', function(done) {
219219
var file = makeFile();
220-
sourcemaps.write(file, { path: '../maps', destPath: 'dist' }, function(err, updatedFile, sourceMapFile) {
220+
sourcemaps.write(file, '../maps', function(err, updatedFile, sourceMapFile) {
221221
expect(updatedFile instanceof File).toExist();
222222
expect(updatedFile).toEqual(file);
223223
expect(String(updatedFile.contents)).toBe(sourceContent + '//# sourceMappingURL=../maps/helloworld.js.map\n');
@@ -238,7 +238,7 @@ describe('write', function() {
238238

239239
it('should create shortest path to map in file comment', function(done) {
240240
var file = makeNestedFile();
241-
sourcemaps.write(file, { path: 'dir1/maps' }, function(err, updatedFile) {
241+
sourcemaps.write(file, 'dir1/maps', function(err, updatedFile) {
242242
expect(String(updatedFile.contents)).toBe(sourceContent + '//# sourceMappingURL=../maps/dir1/dir2/helloworld.js.map\n');
243243
done(err);
244244
});
@@ -265,36 +265,9 @@ describe('write', function() {
265265
});
266266
});
267267

268-
it.skip('should output an error message if debug option is set and sourceContent is missing', function(done) {
269-
var file = makeFile();
270-
file.sourceMap.sources[0] += '.invalid';
271-
delete file.sourceMap.sourcesContent;
272-
var hConsole = ''; // removed
273-
sourcemaps.write(file, { debug: true }, function(err) {
274-
expect(hConsole.history.log[0]).toBe('vinyl-sourcemap-write: No source content for "helloworld.js.invalid". Loading from file.');
275-
expect(hConsole.history.warn[0].indexOf('vinyl-sourcemap-write: source file not found: ') === 0).toExist();
276-
done(err);
277-
});
278-
});
279-
280-
it.skip('should be able to fully control sourceMappingURL by the option sourceMappingURL', function(done) {
281-
var file = makeNestedFile();
282-
sourcemaps.write(file, {
283-
path: '../aaa/bbb/',
284-
sourceMappingURL: function(file) {
285-
return 'http://maps.example.com/' + file.relative + '.map';
286-
}
287-
}, function(err, updatedFile) {
288-
if (/helloworld\.js$/.test(updatedFile.path)) {
289-
expect(String(updatedFile.contents)).toBe(sourceContent + '//# sourceMappingURL=http://maps.example.com/dir1/dir2/helloworld.js.map\n');
290-
done(err);
291-
}
292-
});
293-
});
294-
295268
it('should create shortest path to file in sourceMap#file', function(done) {
296269
var file = makeNestedFile();
297-
sourcemaps.write(file, { path: 'dir1/maps' }, function(err, updatedFile) {
270+
sourcemaps.write(file, 'dir1/maps', function(err, updatedFile) {
298271
expect(updatedFile.sourceMap.file).toEqual('../../../dir2/helloworld.js');
299272
done(err);
300273
});

0 commit comments

Comments
 (0)