Skip to content

Commit

Permalink
feat(Grunt): add source maps to all min files
Browse files Browse the repository at this point in the history
Generate source map files when build step is ran and adds source map
headers to all min files.

Source map url must be appended to the min file otherwise the line
offsets will be off.

Inspired by Ryan Seddon (PR angular#2858)

Closes angular#1714
  • Loading branch information
IgorMinar committed Jun 15, 2013
1 parent cd3dd13 commit 908071a
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions lib/grunt/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,27 +112,44 @@ module.exports = {
},


singleStrict: function(src, insert, newline){
var useStrict = newline ? "$1\n'use strict';" : "$1'use strict';";
singleStrict: function(src, insert){
return src
.replace(/\s*("|')use strict("|');\s*/g, insert) // remove all file-specific strict mode flags
.replace(/(\(function\([^)]*\)\s*\{)/, useStrict); // add single strict mode flag
.replace(/(\(function\([^)]*\)\s*\{)/, "$1'use strict';"); // add single strict mode flag
},


sourceMap: function(mapFile, fileContents) {
// use the following once Chrome beta or stable supports the //# pragma
// var sourceMapLine = '//# sourceMappingURL=' + mapFile + '\n';
var sourceMapLine = '/*\n//@ sourceMappingURL=' + mapFile + '\n*/\n';
return fileContents + sourceMapLine;
},


min: function(file, done) {
var minFile = file.replace(/\.js$/, '.min.js');
var mapFile = minFile + '.map';
var mapFileName = mapFile.match(/[^\/]+$/)[0];
shell.exec(
'java ' +
this.java32flags() + ' ' +
'-jar components/closure-compiler/compiler.jar ' +
'--compilation_level SIMPLE_OPTIMIZATIONS ' +
'--language_in ECMASCRIPT5_STRICT ' +
'--source_map_format=V3 ' +
'--create_source_map ' + mapFile + ' ' +
'--js ' + file + ' ' +
'--js_output_file ' + minFile,
function(code) {
if (code !== 0) grunt.fail.warn('Error minifying ' + file);
grunt.file.write(minFile, this.singleStrict(grunt.file.read(minFile), '\n'));

// closure creates the source map relative to build/ folder, we need to strip those references
grunt.file.write(mapFile, grunt.file.read(mapFile).replace('"file":"build/', '"file":"').
replace('"sources":["build/','"sources":["'));

// move add use strict into the closure + add source map pragma
grunt.file.write(minFile, this.sourceMap(mapFileName, this.singleStrict(grunt.file.read(minFile), '\n')));
grunt.log.ok(file + ' minified into ' + minFile);
done();
}.bind(this));
Expand Down

0 comments on commit 908071a

Please sign in to comment.