Skip to content

Commit

Permalink
Breaking: Simplify writeSourceMaps & add comment to non-JS/CSS files
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Jun 17, 2017
1 parent 11265af commit 11e9e00
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 37 deletions.
46 changes: 13 additions & 33 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,27 +201,23 @@ function createSourceMapFile(opts) {
});
}

function commentFormatter(extname, url) {
// TODO: Not sure I agree with this
if (extname !== '.js' && extname !== '.css') {
return '';
}
var needsMultiline = ['.css'];

function getCommentOptions(extname) {
var opts = {
multiline: (extname === '.css')
multiline: (needsMultiline.indexOf(extname) !== -1)
};

return convert.generateMapFileComment(url, opts);
return opts;
}

function contentIncluded(file, state, callback) {
function writeSourceMaps(file, state, callback) {
var commentOpts = getCommentOptions(file.extname);

var comment;
if (state.destPath == null) {
// encode source map into comment
var base64Map = convert.fromObject(state.sourceMap).toBase64();
// TODO: use convert-source-map .toComment() when we upgrade and have charset support
comment = commentFormatter(file.extname, 'data:application/json;charset=utf8;base64,' + base64Map);
comment = convert.fromObject(state.sourceMap).toComment(commentOpts);
} else {
var mapFile = path.join(state.destPath, file.relative) + '.map';

Expand All @@ -235,35 +231,19 @@ function contentIncluded(file, state, callback) {
content: state.sourceMap,
});

var sourceMapPathRelative = path.relative(file.dirname, sourceMapPath);
var sourcemapLocation = path.relative(file.dirname, sourceMapPath);

if (!isRemoteSource(sourceMapPathRelative)) {
comment = commentFormatter(file.extname, normalizePath(sourceMapPathRelative));
} else {
comment = commentFormatter(file.extname, sourceMapPathRelative);
if (!isRemoteSource(sourcemapLocation)) {
sourcemapLocation = normalizePath(sourcemapLocation);
}

comment = convert.generateMapFileComment(sourcemapLocation, commentOpts);
}

// append source map comment
file.contents = appendBuffer(file.contents, comment);

callback();
}

function writeSourceMaps(file, state, callback) {
var tasks = [
contentIncluded
];

async.applyEachSeries(tasks, file, state, done);

function done(err) {
if (err) {
return callback(err);
}

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

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"dependencies": {
"async": "^2.1.4",
"convert-source-map": "1.3.0",
"convert-source-map": "^1.5.0",
"file-normalize": "^1.1.0",
"graceful-fs": "^4.1.6",
"object.defaults": "^1.0.0",
Expand Down
6 changes: 3 additions & 3 deletions test/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function makeNestedFile() {
}

function base64JSON(object) {
return 'data:application/json;charset=utf8;base64,' + new Buffer(JSON.stringify(object)).toString('base64');
return 'data:application/json;charset=utf-8;base64,' + new Buffer(JSON.stringify(object)).toString('base64');
}

describe('write', function() {
Expand Down Expand Up @@ -197,11 +197,11 @@ describe('write', function() {
});
});

it('should write no comment if not JS or CSS file', function(done) {
it('should write \/\/# comment if any non-.css extension', function(done) {
var file = makeFile();
file.path = file.path.replace('.js', '.txt');
sourcemaps.write(file, function(err, updatedFile) {
expect(String(updatedFile.contents)).toBe(sourceContent);
expect(String(updatedFile.contents)).toEqual(sourceContent + '//# sourceMappingURL=' + base64JSON(updatedFile.sourceMap) + '\n');
done(err);
});
});
Expand Down

0 comments on commit 11e9e00

Please sign in to comment.