Skip to content

Commit

Permalink
Breaking: Pass the sourcemap file as 3rd argument to write callback
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Jun 17, 2017
1 parent 2a94882 commit d74d061
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 82 deletions.
7 changes: 1 addition & 6 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,12 +369,7 @@ function writeSourceMaps(file, state, options, callback) {
return callback(err);
}

var result = [file];
if (state.sourceMapFile) {
result.push(state.sourceMapFile);
}

callback(null, result);
callback(null, file, state.sourceMapFile);
}
}

Expand Down
122 changes: 46 additions & 76 deletions test/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,10 @@ describe('write', function() {

it('should write an inline source map', function(done) {
var file = makeFile();
sourcemaps.write(file, function(err, data) {
var updatedFile = data[0];
expect(data && data.length === 1).toExist();
sourcemaps.write(file, function(err, updatedFile, sourceMapFile) {
expect(updatedFile).toExist();
expect(sourceMapFile).toNotExist();
// TODO: Vinyl.isVinyl
expect(updatedFile instanceof File).toExist();
expect(updatedFile).toEqual(file);
expect(String(updatedFile.contents)).toBe( sourceContent + '\n//# sourceMappingURL=' + base64JSON(updatedFile.sourceMap) + '\n');
Expand All @@ -162,8 +163,7 @@ describe('write', function() {
it('should use CSS comments if CSS file', function(done) {
var file = makeFile();
file.path = file.path.replace('.js', '.css');
sourcemaps.write(file, function(err, data) {
var updatedFile = data[0];
sourcemaps.write(file, function(err, updatedFile) {
expect(String(updatedFile.contents)).toBe(sourceContent + '\n/*# sourceMappingURL=' + base64JSON(updatedFile.sourceMap) + ' */\n');
done(err);
});
Expand All @@ -172,8 +172,7 @@ describe('write', function() {
it('should write no comment if not JS or CSS file', function(done) {
var file = makeFile();
file.path = file.path.replace('.js', '.txt');
sourcemaps.write(file, function(err, data) {
var updatedFile = data[0];
sourcemaps.write(file, function(err, updatedFile) {
expect(String(updatedFile.contents)).toBe(sourceContent);
done(err);
});
Expand All @@ -182,32 +181,29 @@ describe('write', function() {
it('should detect detect whether a file uses \\n or \\r\\n and follow the existing style', function(done) {
var file = makeFile();
file.contents = new Buffer(file.contents.toString().replace(/\n/g, '\r\n'));
sourcemaps.write(file, function(err, data) {
var updatedFile = data[0];
sourcemaps.write(file, function(err, updatedFile) {
expect(String(updatedFile.contents)).toBe(sourceContent.replace(/\n/g, '\r\n') + '\r\n//# sourceMappingURL=' + base64JSON(updatedFile.sourceMap) + '\r\n');
done(err);
});
});

it('should write external map files', function(done) {
var file = makeFile();
sourcemaps.write(file, '../maps', { destPath: 'dist' }, function(err, data) {
var updatedFile = data[0];
var sourceMap = data[1];
sourcemaps.write(file, '../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');
expect(updatedFile.sourceMap.file).toBe('../dist/helloworld.js');
expect(sourceMap instanceof File).toExist();
expect(sourceMap.path).toBe(path.join(__dirname, 'maps/helloworld.js.map'));
expect(JSON.parse(sourceMap.contents)).toEqual(updatedFile.sourceMap);
expect(sourceMap.stat.isFile()).toExist();
expect(sourceMap.stat.isDirectory()).toNotExist();
expect(sourceMap.stat.isBlockDevice()).toNotExist();
expect(sourceMap.stat.isCharacterDevice()).toNotExist();
expect(sourceMap.stat.isSymbolicLink()).toNotExist();
expect(sourceMap.stat.isFIFO()).toNotExist();
expect(sourceMap.stat.isSocket()).toNotExist();
expect(sourceMapFile instanceof File).toExist();
expect(sourceMapFile.path).toBe(path.join(__dirname, 'maps/helloworld.js.map'));
expect(JSON.parse(sourceMapFile.contents)).toEqual(updatedFile.sourceMap);
expect(sourceMapFile.stat.isFile()).toExist();
expect(sourceMapFile.stat.isDirectory()).toNotExist();
expect(sourceMapFile.stat.isBlockDevice()).toNotExist();
expect(sourceMapFile.stat.isCharacterDevice()).toNotExist();
expect(sourceMapFile.stat.isSymbolicLink()).toNotExist();
expect(sourceMapFile.stat.isFIFO()).toNotExist();
expect(sourceMapFile.stat.isSocket()).toNotExist();
done(err);
});
});
Expand All @@ -216,42 +212,37 @@ describe('write', function() {
var file = makeFile();
sourcemaps.write(file, '../maps', { mapFile: function(mapFile) {
return mapFile.replace('.js.map', '.map');
}, destPath: 'dist' }, function(err, data) {
var updatedFile = data[0];
var sourceMap = data[1];
}, 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.map\n');
expect(updatedFile.sourceMap.file).toBe('../dist/helloworld.js');
expect(sourceMap instanceof File).toExist();
expect(sourceMap.path).toBe(path.join(__dirname, 'maps/helloworld.map'));
expect(JSON.parse(sourceMap.contents)).toEqual(updatedFile.sourceMap);
expect(sourceMapFile instanceof File).toExist();
expect(sourceMapFile.path).toBe(path.join(__dirname, 'maps/helloworld.map'));
expect(JSON.parse(sourceMapFile.contents)).toEqual(updatedFile.sourceMap);
done(err);
});
});

it('should create shortest path to map in file comment', function(done) {
var file = makeNestedFile();
sourcemaps.write(file, 'dir1/maps', function(err, data) {
var updatedFile = data[0];
sourcemaps.write(file, 'dir1/maps', function(err, updatedFile) {
expect(String(updatedFile.contents)).toBe(sourceContent + '\n//# sourceMappingURL=../maps/dir1/dir2/helloworld.js.map\n');
done(err);
});
});

it('should write no comment with option addComment=false', function(done) {
var file = makeFile();
sourcemaps.write(file, { addComment: false }, function(err, data) {
var updatedFile = data[0];
sourcemaps.write(file, { addComment: false }, function(err, updatedFile) {
expect(String(updatedFile.contents)).toBe(sourceContent);
done(err);
});
});

it('should not include source content with option includeContent=false', function(done) {
var file = makeFile();
sourcemaps.write(file, { includeContent: false }, function(err, data) {
var updatedFile = data[0];
sourcemaps.write(file, { includeContent: false }, function(err, updatedFile) {
expect(updatedFile.sourceMap.sourcesContent).toBe(undefined);
done(err);
});
Expand All @@ -260,8 +251,7 @@ describe('write', function() {
it('should fetch missing sourceContent', function(done) {
var file = makeFile();
delete file.sourceMap.sourcesContent;
sourcemaps.write(file, function(err, data) {
var updatedFile = data[0];
sourcemaps.write(file, function(err, updatedFile) {
expect(updatedFile.sourceMap.sourcesContent).toNotBe(undefined);
expect(updatedFile.sourceMap.sourcesContent).toEqual([sourceContent]);
done(err);
Expand All @@ -272,8 +262,7 @@ describe('write', function() {
var file = makeFile();
file.sourceMap.sources[0] += '.invalid';
delete file.sourceMap.sourcesContent;
sourcemaps.write(file, function(err, data) {
var updatedFile = data[0];
sourcemaps.write(file, function(err, updatedFile) {
expect(updatedFile.sourceMap.sourcesContent).toNotBe(undefined);
expect(updatedFile.sourceMap.sourcesContent).toEqual([]);
done(err);
Expand All @@ -282,8 +271,7 @@ describe('write', function() {

it('should set the sourceRoot by option sourceRoot', function(done) {
var file = makeFile();
sourcemaps.write(file, { sourceRoot: '/testSourceRoot' }, function(err, data) {
var updatedFile = data[0];
sourcemaps.write(file, { sourceRoot: '/testSourceRoot' }, function(err, updatedFile) {
expect(updatedFile.sourceMap.sourceRoot).toBe('/testSourceRoot');
done(err);
});
Expand All @@ -295,69 +283,58 @@ describe('write', function() {
sourceRoot: function() {
return '/testSourceRoot';
}
}, function(err, data) {
var updatedFile = data[0];
}, function(err, updatedFile) {
expect(updatedFile.sourceMap.sourceRoot).toBe('/testSourceRoot');
done(err);
});
});

it('should automatically determine sourceRoot if destPath is set', function(done) {
var file = makeNestedFile();
sourcemaps.write(file, '.', { destPath: 'dist', includeContent: false }, function(err, data) {
var updatedFile = data[0];
var sourceMap = data[1];
sourcemaps.write(file, '.', { destPath: 'dist', includeContent: false }, function(err, updatedFile, sourceMapFile) {
expect(updatedFile.sourceMap.sourceRoot).toBe('../../../assets');
expect(updatedFile.sourceMap.file).toBe('helloworld.js');
expect(sourceMap.path).toBe(path.join(__dirname, 'assets/dir1/dir2/helloworld.js.map'));
expect(sourceMapFile.path).toBe(path.join(__dirname, 'assets/dir1/dir2/helloworld.js.map'));
done(err);
});
});

it('should interpret relative path in sourceRoot as relative to destination', function(done) {
var file = makeNestedFile();
sourcemaps.write(file, '.', { sourceRoot: '../src' }, function(err, data) {
var updatedFile = data[0];
var sourceMap = data[1];
sourcemaps.write(file, '.', { sourceRoot: '../src' }, function(err, updatedFile, sourceMapFile) {
expect(updatedFile.sourceMap.sourceRoot).toBe('../../../src');
expect(updatedFile.sourceMap.file).toBe('helloworld.js');
expect(sourceMap.path).toBe(path.join(__dirname, 'assets/dir1/dir2/helloworld.js.map'));
expect(sourceMapFile.path).toBe(path.join(__dirname, 'assets/dir1/dir2/helloworld.js.map'));
done(err);
});
});

it('should interpret relative path in sourceRoot as relative to destination (part 2)', function(done) {
var file = makeNestedFile();
sourcemaps.write(file, '.', { sourceRoot: '' }, function(err, data) {
var updatedFile = data[0];
var sourceMap = data[1];
sourcemaps.write(file, '.', { sourceRoot: '' }, function(err, updatedFile, sourceMapFile) {
expect(updatedFile.sourceMap.sourceRoot).toBe('../..');
expect(updatedFile.sourceMap.file).toBe('helloworld.js');
expect(sourceMap.path).toBe(path.join(__dirname, 'assets/dir1/dir2/helloworld.js.map'));
expect(sourceMapFile.path).toBe(path.join(__dirname, 'assets/dir1/dir2/helloworld.js.map'));
done(err);
});
});

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, data) {
var updatedFile = data[0];
var sourceMap = data[1];
sourcemaps.write(file, 'maps', { sourceRoot: '../src' }, function(err, updatedFile, sourceMapFile) {
expect(updatedFile.sourceMap.sourceRoot).toBe('../../../../src');
expect(updatedFile.sourceMap.file).toBe('../../../dir1/dir2/helloworld.js');
expect(sourceMap.path).toBe(path.join(__dirname, 'assets/maps/dir1/dir2/helloworld.js.map'));
expect(sourceMapFile.path).toBe(path.join(__dirname, 'assets/maps/dir1/dir2/helloworld.js.map'));
done(err);
});
});

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, data) {
var updatedFile = data[0];
var sourceMap = data[1];
sourcemaps.write(file, '../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(sourceMap.path).toBe(path.join(__dirname, 'maps/dir1/dir2/helloworld.js.map'));
expect(sourceMapFile.path).toBe(path.join(__dirname, 'maps/dir1/dir2/helloworld.js.map'));
done(err);
});
});
Expand All @@ -366,8 +343,7 @@ describe('write', function() {
var file = makeFile();
sourcemaps.write(file, '../maps', {
sourceMappingURLPrefix: 'https://asset-host.example.com'
}, function(err, data) {
var updatedFile = data[0];
}, function(err, updatedFile) {
if (/helloworld\.js$/.test(updatedFile.path)) {
expect(/sourceMappingURL.*\n$/.exec(String(updatedFile.contents))[0]).toEqual('sourceMappingURL=https://asset-host.example.com/maps/helloworld.js.map\n');
done(err);
Expand All @@ -381,8 +357,7 @@ describe('write', function() {
sourceMappingURLPrefix: function() {
return 'https://asset-host.example.com';
}
}, function(err, data) {
var updatedFile = data[0];
}, function(err, updatedFile) {
if (/helloworld\.js$/.test(updatedFile.path)) {
expect(/sourceMappingURL.*\n$/.exec(String(updatedFile.contents))[0]).toEqual('sourceMappingURL=https://asset-host.example.com/maps/helloworld.js.map\n');
done(err);
Expand All @@ -404,8 +379,7 @@ describe('write', function() {

it('null as sourceRoot, should not set the sourceRoot', function(done) {
var file = makeFile();
sourcemaps.write(file, { sourceRoot: null }, function(err, data) {
var updatedFile = data[0];
sourcemaps.write(file, { sourceRoot: null }, function(err, updatedFile) {
expect(updatedFile.sourceMap.sourceRoot).toBe(undefined);
done(err);
});
Expand All @@ -417,17 +391,15 @@ describe('write', function() {
sourceRoot: function() {
return null;
}
}, function(err, data) {
var updatedFile = data[0];
}, function(err, updatedFile) {
expect(updatedFile.sourceMap.sourceRoot).toBe(undefined);
done(err);
});
});

it('empty string as sourceRoot should be kept', function(done) {
var file = makeFile();
sourcemaps.write(file, { sourceRoot: '' }, function(err, data) {
var updatedFile = data[0];
sourcemaps.write(file, { sourceRoot: '' }, function(err, updatedFile) {
expect(updatedFile.sourceMap.sourceRoot).toBe('');
done(err);
});
Expand All @@ -439,8 +411,7 @@ describe('write', function() {
sourceMappingURL: function(file) {
return 'http://maps.example.com/' + file.relative + '.map';
}
}, function(err, data) {
var updatedFile = data[0];
}, function(err, updatedFile) {
if (/helloworld\.js$/.test(updatedFile.path)) {
expect(String(updatedFile.contents)).toBe(sourceContent + '\n//# sourceMappingURL=http://maps.example.com/dir1/dir2/helloworld.js.map\n');
done(err);
Expand All @@ -454,8 +425,7 @@ describe('write', function() {
mapSources: function(sourcePath) {
return '../src/' + sourcePath;
}
}, function(err, data) {
var updatedFile = data[0];
}, function(err, updatedFile) {
expect(updatedFile.sourceMap.sources).toEqual(['../src/helloworld.js']);
done(err);
});
Expand Down

0 comments on commit d74d061

Please sign in to comment.