@@ -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 * ( " | ' ) 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 ;
120127 } ,
121128
122129
123130 min : function ( file , done ) {
124131 var minFile = file . replace ( / \. j s $ / , '.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