Skip to content

Commit

Permalink
Sourcemap support
Browse files Browse the repository at this point in the history
  • Loading branch information
frostney committed Feb 23, 2015
1 parent 65033d1 commit af76eb8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"dependencies": {
"coffee-formatter": "^0.1.1",
"grunt-concat-sourcemap": "~0.4.0",
"js-beautify": "^1.5.4"
"js-beautify": "^1.5.4",
"sourcemap-concat": "^1.0.2"
}
}
38 changes: 38 additions & 0 deletions tasks/commonjs_tamer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
var path = require('path');
var beautify = require('js-beautify').js_beautify;
var coffeeFormatter = require('coffee-formatter');
var SourceMapConcat = require('sourcemap-concat').SourceMapConcatenator;

module.exports = function(grunt) {

Expand Down Expand Up @@ -40,6 +41,8 @@ module.exports = function(grunt) {

// Iterate over all specified file groups.
this.files.forEach(function(f) {

var sourceMapConcat = new SourceMapConcat({file: f.dest});

var src = '';

Expand Down Expand Up @@ -124,6 +127,41 @@ module.exports = function(grunt) {

source = options.process(source, moduleName);

if (options.sourceMap) {
var prevSourceMap;
var sourceMapFile = filepath + '.map';
var file;

var sourceMapComment = '//# sourceMappingURL=';
var hasSourceMap = source.indexOf(sourceMapComment) >= 0;

if (hasSourceMap) {
var smMatch = source.match(/\/\# sourceMappingURL=(.*)/);
file = smMatch[1];

var basename = path.basename(filepath);
var sourceMapPath = filepath.split(basename)[0];

sourceMapFile = [sourceMapPath, file].join(path.sep);
}

if (grunt.file.exists(sourceMapFile)) {
prevSourceMap = grunt.file.readJSON(sourceMapFile);
}

if (hasSourceMap) {
source = source.split(sourceMapComment + file)[0];
}

var fileSource = source;

if (index !== filtered.length - 1 && !prevSourceMap) {
fileSource += grunt.util.normalizelf(options.separator);
}

sourceMapConcat.add(filepath, source, prevSourceMap);
}

return source;
}).join(grunt.util.normalizelf(options.separator));

Expand Down

0 comments on commit af76eb8

Please sign in to comment.