Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 908071a

Browse files
committed
feat(Grunt): add source maps to all min files
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 #2858) Closes #1714
1 parent cd3dd13 commit 908071a

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

lib/grunt/utils.js

+21-4
Original file line numberDiff line numberDiff line change
@@ -112,27 +112,44 @@ module.exports = {
112112
},
113113

114114

115-
singleStrict: function(src, insert, newline){
116-
var useStrict = newline ? "$1\n'use strict';" : "$1'use strict';";
115+
singleStrict: function(src, insert){
117116
return src
118117
.replace(/\s*("|')use strict("|');\s*/g, insert) // remove all file-specific strict mode flags
119-
.replace(/(\(function\([^)]*\)\s*\{)/, useStrict); // add single strict mode flag
118+
.replace(/(\(function\([^)]*\)\s*\{)/, "$1'use strict';"); // add single strict mode flag
119+
},
120+
121+
122+
sourceMap: function(mapFile, fileContents) {
123+
// use the following once Chrome beta or stable supports the //# pragma
124+
// var sourceMapLine = '//# sourceMappingURL=' + mapFile + '\n';
125+
var sourceMapLine = '/*\n//@ sourceMappingURL=' + mapFile + '\n*/\n';
126+
return fileContents + sourceMapLine;
120127
},
121128

122129

123130
min: function(file, done) {
124131
var minFile = file.replace(/\.js$/, '.min.js');
132+
var mapFile = minFile + '.map';
133+
var mapFileName = mapFile.match(/[^\/]+$/)[0];
125134
shell.exec(
126135
'java ' +
127136
this.java32flags() + ' ' +
128137
'-jar components/closure-compiler/compiler.jar ' +
129138
'--compilation_level SIMPLE_OPTIMIZATIONS ' +
130139
'--language_in ECMASCRIPT5_STRICT ' +
140+
'--source_map_format=V3 ' +
141+
'--create_source_map ' + mapFile + ' ' +
131142
'--js ' + file + ' ' +
132143
'--js_output_file ' + minFile,
133144
function(code) {
134145
if (code !== 0) grunt.fail.warn('Error minifying ' + file);
135-
grunt.file.write(minFile, this.singleStrict(grunt.file.read(minFile), '\n'));
146+
147+
// closure creates the source map relative to build/ folder, we need to strip those references
148+
grunt.file.write(mapFile, grunt.file.read(mapFile).replace('"file":"build/', '"file":"').
149+
replace('"sources":["build/','"sources":["'));
150+
151+
// move add use strict into the closure + add source map pragma
152+
grunt.file.write(minFile, this.sourceMap(mapFileName, this.singleStrict(grunt.file.read(minFile), '\n')));
136153
grunt.log.ok(file + ' minified into ' + minFile);
137154
done();
138155
}.bind(this));

0 commit comments

Comments
 (0)