From ea56f4ca21101a0f77dfe265c079638f6a9ac5b3 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Wed, 19 Apr 2017 17:04:36 -0500 Subject: [PATCH] Breaking: Remove options from the API --- index.js | 36 +++-------------- test/add.js | 104 +++++--------------------------------------------- test/write.js | 35 ++--------------- 3 files changed, 20 insertions(+), 155 deletions(-) diff --git a/index.js b/index.js index 82089d1..9b09ab5 100644 --- a/index.js +++ b/index.js @@ -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)) { @@ -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 @@ -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 @@ -97,7 +73,7 @@ function write(file, options, callback) { } var state = { - destPath: opts.path, + destPath: destPath, sourceMap: sourceMap, sourceMapFile: null, }; diff --git a/test/add.js b/test/add.js index aab6efa..749c7e4 100644 --- a/test/add.js +++ b/test/add.js @@ -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(); @@ -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(); @@ -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); }); @@ -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']); @@ -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); }); @@ -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']); @@ -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']); @@ -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']); @@ -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']); @@ -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']); @@ -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, @@ -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); diff --git a/test/write.js b/test/write.js index 78a2cb1..194f892 100644 --- a/test/write.js +++ b/test/write.js @@ -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 = { @@ -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'); @@ -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); }); @@ -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); });