@@ -112,27 +112,44 @@ module.exports = {
112
112
} ,
113
113
114
114
115
- singleStrict : function ( src , insert , newline ) {
116
- var useStrict = newline ? "$1\n'use strict';" : "$1'use strict';" ;
115
+ singleStrict : function ( src , insert ) {
117
116
return src
118
117
. replace ( / \s * ( " | ' ) u s e s t r i c t ( " | ' ) ; \s * / g, insert ) // remove all file-specific strict mode flags
119
- . replace ( / ( \( f u n c t i o n \( [ ^ ) ] * \) \s * \{ ) / , useStrict ) ; // add single strict mode flag
118
+ . replace ( / ( \( f u n c t i o n \( [ ^ ) ] * \) \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 ;
120
127
} ,
121
128
122
129
123
130
min : function ( file , done ) {
124
131
var minFile = file . replace ( / \. j s $ / , '.min.js' ) ;
132
+ var mapFile = minFile + '.map' ;
133
+ var mapFileName = mapFile . match ( / [ ^ \/ ] + $ / ) [ 0 ] ;
125
134
shell . exec (
126
135
'java ' +
127
136
this . java32flags ( ) + ' ' +
128
137
'-jar components/closure-compiler/compiler.jar ' +
129
138
'--compilation_level SIMPLE_OPTIMIZATIONS ' +
130
139
'--language_in ECMASCRIPT5_STRICT ' +
140
+ '--source_map_format=V3 ' +
141
+ '--create_source_map ' + mapFile + ' ' +
131
142
'--js ' + file + ' ' +
132
143
'--js_output_file ' + minFile ,
133
144
function ( code ) {
134
145
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' ) ) ) ;
136
153
grunt . log . ok ( file + ' minified into ' + minFile ) ;
137
154
done ( ) ;
138
155
} . bind ( this ) ) ;
0 commit comments