diff --git a/index.js b/index.js index 6398b99..7187a8a 100644 --- a/index.js +++ b/index.js @@ -54,24 +54,12 @@ function add(file, options, callback) { * @param options * @param callback */ -function write(file, destPath, options, callback) { +function write(file, options, callback) { // Check arguments for optional destPath, options, or callback function - if (callback === undefined && typeof destPath === 'function') { - callback = destPath; - destPath = undefined; - } else if (callback === undefined && typeof options === 'function') { + if (typeof options === 'function') { callback = options; - if (Object.prototype.toString.call(destPath) === '[object Object]') { - options = destPath; - destPath = undefined; - } else if (typeof destPath === 'string') { - options = {}; - } else { - return callback(new Error(PLUGIN_NAME + '-write: Invalid arguments')); - } - } else if (Object.prototype.toString.call(options) !== '[object Object]') { - return callback(new Error(PLUGIN_NAME + '-write: Invalid argument: options')); + options = {}; } options = options || {}; @@ -128,7 +116,7 @@ function write(file, destPath, options, callback) { } var state = { - destPath: destPath, + destPath: options.path, sourceMap: sourceMap, sourceMapFile: null, }; diff --git a/test/write.js b/test/write.js index 2d0c4be..5993020 100644 --- a/test/write.js +++ b/test/write.js @@ -92,7 +92,7 @@ describe('write', function() { it('should return an error when invalid arguments are provided', function(done) { var file = makeFile(); sourcemaps.write(file, undefined, function(err) { - expect(err instanceof Error && err.message === 'vinyl-sourcemap-write: Invalid arguments').toExist(); + expect(err).toNotExist(); done(); }); }); @@ -100,7 +100,7 @@ describe('write', function() { it('should return an error when invalid arguments are provided', function(done) { var file = makeFile(); sourcemaps.write(file, null, function(err) { - expect(err instanceof Error && err.message === 'vinyl-sourcemap-write: Invalid arguments').toExist(); + expect(err).toNotExist(); done(); }); }); @@ -108,39 +108,7 @@ describe('write', function() { it('should return an error when invalid arguments are provided', function(done) { var file = makeFile(); sourcemaps.write(file, true, function(err) { - expect(err instanceof Error && err.message === 'vinyl-sourcemap-write: Invalid arguments').toExist(); - done(); - }); - }); - - it('should return an error when invalid options are provided', function(done) { - var file = makeFile(); - sourcemaps.write(file, 'test', undefined, function(err) { - expect(err instanceof Error && err.message === 'vinyl-sourcemap-write: Invalid argument: options').toExist(); - done(); - }); - }); - - it('should return an error when invalid options are provided', function(done) { - var file = makeFile(); - sourcemaps.write(file, 'test', null, function(err) { - expect(err instanceof Error && err.message === 'vinyl-sourcemap-write: Invalid argument: options').toExist(); - done(); - }); - }); - - it('should return an error when invalid options are provided', function(done) { - var file = makeFile(); - sourcemaps.write(file, 'test', '', function(err) { - expect(err instanceof Error && err.message === 'vinyl-sourcemap-write: Invalid argument: options').toExist(); - done(); - }); - }); - - it('should return an error when invalid options are provided', function(done) { - var file = makeFile(); - sourcemaps.write(file, 'test', true, function(err) { - expect(err instanceof Error && err.message === 'vinyl-sourcemap-write: Invalid argument: options').toExist(); + expect(err).toNotExist(); done(); }); }); @@ -187,7 +155,7 @@ describe('write', function() { it('should write external map files', function(done) { var file = makeFile(); - sourcemaps.write(file, '../maps', { destPath: 'dist' }, function(err, updatedFile, sourceMapFile) { + sourcemaps.write(file, { path: '../maps', destPath: 'dist' }, function(err, updatedFile, sourceMapFile) { expect(updatedFile instanceof File).toExist(); expect(updatedFile).toEqual(file); expect(String(updatedFile.contents)).toBe(sourceContent + '\n//# sourceMappingURL=../maps/helloworld.js.map\n'); @@ -208,7 +176,7 @@ describe('write', function() { it.skip('should allow to rename map file', function(done) { var file = makeFile(); - sourcemaps.write(file, '../maps', { mapFile: function(mapFile) { + sourcemaps.write(file, { path: '../maps', mapFile: function(mapFile) { return mapFile.replace('.js.map', '.map'); }, destPath: 'dist' }, function(err, updatedFile, sourceMapFile) { expect(updatedFile instanceof File).toExist(); @@ -224,7 +192,7 @@ describe('write', function() { it('should create shortest path to map in file comment', function(done) { var file = makeNestedFile(); - sourcemaps.write(file, 'dir1/maps', function(err, updatedFile) { + sourcemaps.write(file, { path: 'dir1/maps' }, function(err, updatedFile) { expect(String(updatedFile.contents)).toBe(sourceContent + '\n//# sourceMappingURL=../maps/dir1/dir2/helloworld.js.map\n'); done(err); }); @@ -289,7 +257,7 @@ describe('write', function() { it('should automatically determine sourceRoot if destPath is set', function(done) { var file = makeNestedFile(); - sourcemaps.write(file, '.', { destPath: 'dist', includeContent: false }, function(err, updatedFile, sourceMapFile) { + sourcemaps.write(file, { path: '.', destPath: 'dist', includeContent: false }, function(err, updatedFile, sourceMapFile) { expect(updatedFile.sourceMap.sourceRoot).toBe('../../../assets'); expect(updatedFile.sourceMap.file).toBe('helloworld.js'); expect(sourceMapFile.path).toBe(path.join(__dirname, 'assets/dir1/dir2/helloworld.js.map')); @@ -299,7 +267,7 @@ describe('write', function() { it('should interpret relative path in sourceRoot as relative to destination', function(done) { var file = makeNestedFile(); - sourcemaps.write(file, '.', { sourceRoot: '../src' }, function(err, updatedFile, sourceMapFile) { + sourcemaps.write(file, { path: '.', sourceRoot: '../src' }, function(err, updatedFile, sourceMapFile) { expect(updatedFile.sourceMap.sourceRoot).toBe('../../../src'); expect(updatedFile.sourceMap.file).toBe('helloworld.js'); expect(sourceMapFile.path).toBe(path.join(__dirname, 'assets/dir1/dir2/helloworld.js.map')); @@ -309,7 +277,7 @@ describe('write', function() { it('should interpret relative path in sourceRoot as relative to destination (part 2)', function(done) { var file = makeNestedFile(); - sourcemaps.write(file, '.', { sourceRoot: '' }, function(err, updatedFile, sourceMapFile) { + sourcemaps.write(file, { path: '.', sourceRoot: '' }, function(err, updatedFile, sourceMapFile) { expect(updatedFile.sourceMap.sourceRoot).toBe('../..'); expect(updatedFile.sourceMap.file).toBe('helloworld.js'); expect(sourceMapFile.path).toBe(path.join(__dirname, 'assets/dir1/dir2/helloworld.js.map')); @@ -319,7 +287,7 @@ describe('write', function() { it('should interpret relative path in sourceRoot as relative to destination (part 3)', function(done) { var file = makeNestedFile(); - sourcemaps.write(file, 'maps', { sourceRoot: '../src' }, function(err, updatedFile, sourceMapFile) { + sourcemaps.write(file, { path: 'maps', sourceRoot: '../src' }, function(err, updatedFile, sourceMapFile) { expect(updatedFile.sourceMap.sourceRoot).toBe('../../../../src'); expect(updatedFile.sourceMap.file).toBe('../../../dir1/dir2/helloworld.js'); expect(sourceMapFile.path).toBe(path.join(__dirname, 'assets/maps/dir1/dir2/helloworld.js.map')); @@ -329,7 +297,7 @@ describe('write', function() { it('should interpret relative path in sourceRoot as relative to destination (part 4)', function(done) { var file = makeNestedFile(); - sourcemaps.write(file, '../maps', { sourceRoot: '../src', destPath: 'dist' }, function(err, updatedFile, sourceMapFile) { + sourcemaps.write(file, { path: '../maps', sourceRoot: '../src', destPath: 'dist' }, function(err, updatedFile, sourceMapFile) { expect(updatedFile.sourceMap.sourceRoot).toBe('../../../src'); expect(updatedFile.sourceMap.file).toBe('../../../dist/dir1/dir2/helloworld.js'); expect(sourceMapFile.path).toBe(path.join(__dirname, 'maps/dir1/dir2/helloworld.js.map')); @@ -339,7 +307,8 @@ describe('write', function() { it('should accept a sourceMappingURLPrefix', function(done) { var file = makeFile(); - sourcemaps.write(file, '../maps', { + sourcemaps.write(file, { + path: '../maps', sourceMappingURLPrefix: 'https://asset-host.example.com' }, function(err, updatedFile) { if (/helloworld\.js$/.test(updatedFile.path)) { @@ -351,7 +320,8 @@ describe('write', function() { it.skip('should accept a sourceMappingURLPrefix, as a function', function(done) { var file = makeFile(); - sourcemaps.write(file, '../maps', { + sourcemaps.write(file, { + path: '../maps', sourceMappingURLPrefix: function() { return 'https://asset-host.example.com'; } @@ -405,7 +375,8 @@ describe('write', function() { it.skip('should be able to fully control sourceMappingURL by the option sourceMappingURL', function(done) { var file = makeNestedFile(); - sourcemaps.write(file, '../aaa/bbb/', { + sourcemaps.write(file, { + path: '../aaa/bbb/', sourceMappingURL: function(file) { return 'http://maps.example.com/' + file.relative + '.map'; }