Skip to content

Commit

Permalink
Breaking: Remove destPath argument in .write & attach to options as p…
Browse files Browse the repository at this point in the history
…ath property (closes #14)
  • Loading branch information
phated committed Mar 21, 2017
1 parent 1e136d8 commit dc9ff35
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 62 deletions.
20 changes: 4 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {};
Expand Down Expand Up @@ -128,7 +116,7 @@ function write(file, destPath, options, callback) {
}

var state = {
destPath: destPath,
destPath: options.path,
sourceMap: sourceMap,
sourceMapFile: null,
};
Expand Down
63 changes: 17 additions & 46 deletions test/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,55 +90,23 @@ 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();
});
});

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();
});
});

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();
});
});
Expand Down Expand Up @@ -185,7 +153,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');
Expand All @@ -206,7 +174,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();
Expand All @@ -222,7 +190,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);
});
Expand Down Expand Up @@ -287,7 +255,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'));
Expand All @@ -297,7 +265,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'));
Expand All @@ -307,7 +275,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'));
Expand All @@ -317,7 +285,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'));
Expand All @@ -327,7 +295,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'));
Expand All @@ -337,7 +305,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)) {
Expand All @@ -349,7 +318,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';
}
Expand Down Expand Up @@ -403,7 +373,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';
}
Expand Down

0 comments on commit dc9ff35

Please sign in to comment.