Skip to content

Commit

Permalink
Breaking: Remove options from the API
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Jun 17, 2017
1 parent 06f26ac commit ea56f4c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 155 deletions.
36 changes: 6 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,13 @@
'use strict';

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

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

var PLUGIN_NAME = 'vinyl-sourcemap';

function isObject(value) {
return value && typeof value === 'object' && !Array.isArray(value);
}

function add(file, options, callback) {

// Check if options or a callback are passed as second argument
if (typeof options === 'function') {
callback = options;
options = {};
}

// Default options if not an object
if (!isObject(options)) {
options = {};
}
function add(file, callback) {

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

function write(file, options, callback) {
function write(file, destPath, callback) {

// Check if options or a callback are passed as second argument
if (typeof options === 'function') {
callback = options;
options = {};
}

// Default options if not an object
if (!isObject(options)) {
options = {};
if (typeof destPath === 'function') {
callback = destPath;
destPath = undefined;
}

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

// Set defaults for options if unset
var opts = defaults({}, options);

var sourceMap = file.sourceMap;

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

var state = {
destPath: opts.path,
destPath: destPath,
sourceMap: sourceMap,
sourceMapFile: null,
};
Expand Down
104 changes: 10 additions & 94 deletions test/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,79 +79,6 @@ describe('add', function() {
});
});

describe('ensures options argument', function() {

// Currently no options are defaulted
// TODO: Enable test if any options are defaulted
it.skip('is not mutated', function(done) {
var defaultedOpts = {};

var opts = {};

var file = makeFile();
sourcemaps.add(file, opts, function(err) {
expect(opts).toNotEqual(defaultedOpts);
done(err);
});
});

it('is defaulted if undefined', function(done) {
var file = makeFile();
sourcemaps.add(file, undefined, function(err) {
expect(err).toNotExist();
done();
});
});

it('is defaulted if null', function(done) {
var file = makeFile();
sourcemaps.add(file, null, function(err) {
expect(err).toNotExist();
done();
});
});

it('is defaulted if empty string', function(done) {
var file = makeFile();
sourcemaps.add(file, '', function(err) {
expect(err).toNotExist();
done();
});
});

it('is defaulted if non-empty string', function(done) {
var file = makeFile();
sourcemaps.add(file, 'invalid', function(err) {
expect(err).toNotExist();
done();
});
});

it('is defaulted if boolean false', function(done) {
var file = makeFile();
sourcemaps.add(file, false, function(err) {
expect(err).toNotExist();
done();
});
});

it('is defaulted if boolean true', function(done) {
var file = makeFile();
sourcemaps.add(file, true, function(err) {
expect(err).toNotExist();
done();
});
});

it('is defaulted if array', function(done) {
var file = makeFile();
sourcemaps.add(file, [], function(err) {
expect(err).toNotExist();
done();
});
});
});

it('should add an empty sourceMap', function(done) {
sourcemaps.add(makeFile(), function(err, data) {
expect(File.isVinyl(data)).toExist();
Expand All @@ -166,7 +93,7 @@ describe('add', function() {
});

it('should import an existing inline source map', function(done) {
sourcemaps.add(makeFileWithInlineSourceMap(), { loadMaps: true }, function(err, data) {
sourcemaps.add(makeFileWithInlineSourceMap(), function(err, data) {
expect(data).toExist();
expect(data instanceof File).toExist();
expect(data.sourceMap).toExist();
Expand All @@ -179,7 +106,7 @@ describe('add', function() {
});

it('should remove inline source', function(done) {
sourcemaps.add(makeFileWithInlineSourceMap(), { loadMaps: true }, function(err, data) {
sourcemaps.add(makeFileWithInlineSourceMap(), function(err, data) {
expect(/sourceMappingURL/.test(data.contents.toString())).toNotExist();
done(err);
});
Expand All @@ -188,7 +115,7 @@ describe('add', function() {
it('should load external source map file reference in comment with \/\/# syntax', function(done) {
var file = makeFile();
file.contents = new Buffer(sourceContent + '\n//# sourceMappingURL=helloworld2.js.map');
sourcemaps.add(file, { loadMaps: true }, function(err, data) {
sourcemaps.add(file, function(err, data) {
expect(data.sourceMap).toExist();
expect(String(data.sourceMap.version)).toBe('3');
expect(data.sourceMap.sources).toEqual(['helloworld2.js']);
Expand All @@ -201,7 +128,7 @@ describe('add', function() {
it('should remove source map comment with the \/\/# syntax', function(done) {
var file = makeFile();
file.contents = new Buffer(sourceContent + '\n//# sourceMappingURL=helloworld2.js.map');
sourcemaps.add(file, { loadMaps: true }, function(err, data) {
sourcemaps.add(file, function(err, data) {
expect(/sourceMappingURL/.test(data.contents.toString())).toNotExist();
done(err);
});
Expand All @@ -210,7 +137,7 @@ describe('add', function() {
it('should load external source map if no source mapping comment', function (done) {
var file = makeFile();
file.path = file.path.replace('helloworld.js', 'helloworld2.js');
sourcemaps.add(file, { loadMaps: true }, function(err, data) {
sourcemaps.add(file, function(err, data) {
expect(data.sourceMap).toExist();
expect(String(data.sourceMap.version)).toBe('3');
expect(data.sourceMap.sources).toEqual(['helloworld2.js']);
Expand All @@ -223,7 +150,7 @@ describe('add', function() {
it('should load external source map and add sourceContent if missing', function(done) {
var file = makeFile();
file.contents = new Buffer(sourceContent + '\n//# sourceMappingURL=helloworld3.js.map');
sourcemaps.add(file, { loadMaps: true }, function(err, data) {
sourcemaps.add(file, function(err, data) {
expect(data.sourceMap).toExist();
expect(String(data.sourceMap.version)).toBe('3');
expect(data.sourceMap.sources).toEqual(['helloworld.js', 'test1.js']);
Expand All @@ -236,7 +163,7 @@ describe('add', function() {
it('should not throw when source file for sourceContent not found', function(done) {
var file = makeFile();
file.contents = new Buffer(sourceContent + '\n//# sourceMappingURL=helloworld4.js.map');
sourcemaps.add(file, { loadMaps: true }, function(err, data) {
sourcemaps.add(file, function(err, data) {
expect(data.sourceMap).toExist();
expect(String(data.sourceMap.version)).toBe('3');
expect(data.sourceMap.sources).toEqual(['helloworld.js', 'missingfile']);
Expand All @@ -259,7 +186,7 @@ describe('add', function() {
it('should use sourceRoot when resolving path to sources', function(done) {
var file = makeFile();
file.contents = new Buffer(sourceContent + '\n//# sourceMappingURL=helloworld5.js.map');
sourcemaps.add(file, { loadMaps:true }, function(err, data) {
sourcemaps.add(file, function(err, data) {
expect(data.sourceMap).toExist([]);
expect(String(data.sourceMap.version)).toBe('3');
expect(data.sourceMap.sources).toEqual(['../helloworld.js', '../test1.js']);
Expand All @@ -273,7 +200,7 @@ describe('add', function() {
it('should not load source conent if the path is a url', function(done) {
var file = makeFile();
file.contents = new Buffer(sourceContent + '\n//# sourceMappingURL=helloworld6.js.map');
sourcemaps.add(file, { loadMaps: true }, function(err, data) {
sourcemaps.add(file, function(err, data) {
expect(data.sourceMap).toExist();
expect(String(data.sourceMap.version)).toBe('3');
expect(data.sourceMap.sources).toEqual(['helloworld.js', 'http://example2.com/test1.js']);
Expand All @@ -283,17 +210,6 @@ describe('add', function() {
});
});

it.skip('should output an error message if debug option is set and sourceContent is missing', function (done) {
var file = makeFile();
file.contents = new Buffer(sourceContent + '\n//# sourceMappingURL=helloworld4.js.map');
var hConsole = ''; // Removed
sourcemaps.add(file, { loadMaps: true, debug: true }, function(err) {
expect(hConsole.history.log[0]).toEqual('vinyl-sourcemap-add: No source content for "missingfile". Loading from file.');
expect(hConsole.history.warn[0].indexOf('vinyl-sourcemap-add: source file not found: ') === 0).toExist();
done(err);
});
});

it('should pass through whe file already has a source map', function(done) {
var sourceMap = {
version: 3,
Expand All @@ -305,7 +221,7 @@ describe('add', function() {

var file = makeFile();
file.sourceMap = sourceMap;
sourcemaps.add(file, { loadMaps:true }, function(err, data) {
sourcemaps.add(file, function(err, data) {
expect(data).toExist();
expect(data instanceof File).toExist();
expect(data.sourceMap).toBe(sourceMap);
Expand Down
35 changes: 4 additions & 31 deletions test/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe('write', function() {
});
});

describe('ensures options argument', function() {
describe.skip('ensures destPath argument', function() {

it('is not mutated', function(done) {
var defaultedOpts = {
Expand Down Expand Up @@ -217,7 +217,7 @@ describe('write', function() {

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

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

it.skip('should output an error message if debug option is set and sourceContent is missing', function(done) {
var file = makeFile();
file.sourceMap.sources[0] += '.invalid';
delete file.sourceMap.sourcesContent;
var hConsole = ''; // removed
sourcemaps.write(file, { debug: true }, function(err) {
expect(hConsole.history.log[0]).toBe('vinyl-sourcemap-write: No source content for "helloworld.js.invalid". Loading from file.');
expect(hConsole.history.warn[0].indexOf('vinyl-sourcemap-write: source file not found: ') === 0).toExist();
done(err);
});
});

it.skip('should be able to fully control sourceMappingURL by the option sourceMappingURL', function(done) {
var file = makeNestedFile();
sourcemaps.write(file, {
path: '../aaa/bbb/',
sourceMappingURL: function(file) {
return 'http://maps.example.com/' + file.relative + '.map';
}
}, function(err, updatedFile) {
if (/helloworld\.js$/.test(updatedFile.path)) {
expect(String(updatedFile.contents)).toBe(sourceContent + '//# sourceMappingURL=http://maps.example.com/dir1/dir2/helloworld.js.map\n');
done(err);
}
});
});

it('should create shortest path to file in sourceMap#file', function(done) {
var file = makeNestedFile();
sourcemaps.write(file, { path: 'dir1/maps' }, function(err, updatedFile) {
sourcemaps.write(file, 'dir1/maps', function(err, updatedFile) {
expect(updatedFile.sourceMap.file).toEqual('../../../dir2/helloworld.js');
done(err);
});
Expand Down

0 comments on commit ea56f4c

Please sign in to comment.