@@ -42,6 +42,7 @@ function loader(mappings, entryPoints, options) {
4242
4343 var scope = {
4444 mappings : mappings ,
45+ revNums : { } ,
4546 cache : { } ,
4647 reloading : false ,
4748 reloadHooks : { } ,
@@ -80,23 +81,22 @@ function loader(mappings, entryPoints, options) {
8081 }
8182 }
8283
83- function compile ( mapping ) {
84+ function compile ( mapping , revision ) {
8485 var body = mapping [ 0 ] ;
8586 if ( typeof body !== "function" ) {
86- debug ( "Compiling module" , mapping [ 2 ] )
87+ debug ( "Compiling module" , mapping [ 2 ] , "[revision " + revision + " ]" )
8788 var compiled = compileModule ( body , mapping [ 2 ] . sourcemap ) ;
8889 mapping [ 0 ] = compiled ;
8990 mapping [ 2 ] . source = body ;
9091 }
9192 }
9293
9394 function compileModule ( source , sourcemap ) {
94- return eval (
95- "function __livereactload_module(require, module, exports){\n" +
96- source +
97- "\n}; __livereactload_module;" +
98- ( sourcemap || "" )
95+ var toModule = new Function (
96+ "__livereactload_source" , "__livereactload_sourcemap" ,
97+ "return eval('function __livereactload_module(require, module, exports){\\n' + __livereactload_source + '\\n}; __livereactload_module;' + (__livereactload_sourcemap || ''));"
9998 ) ;
99+ return toModule ( source , sourcemap )
100100 }
101101
102102 function unknownUseCase ( ) {
@@ -163,12 +163,34 @@ function loader(mappings, entryPoints, options) {
163163 var mapping = mappings [ id ] ;
164164 var meta = mapping [ 2 ] ;
165165 if ( ! old || old [ 2 ] . hash !== meta . hash ) {
166- compile ( mapping ) ;
166+ var rev = scope . revNums [ id ] ? ++ scope . revNums [ id ] : ( scope . revNums [ id ] = 1 ) ;
167+ if ( old && meta . sourcemap ) {
168+ addVersionToSourceMap ( meta , rev ) ;
169+ }
170+ compile ( mapping , rev ) ;
167171 scope . mappings [ id ] = mapping ;
168172 changes . push ( [ id , old ] ) ;
169173 }
170174 } ) ;
171175 return changes ;
176+
177+ // Updates the source map by adding a revision parameter to the filename.
178+ // Without this new filename, browsers will ignore the updated source map.
179+ function addVersionToSourceMap ( meta , revision ) {
180+ var comment = meta . sourcemap
181+ . replace ( / ^ \/ \* / g, '//' )
182+ . replace ( / \* \/ $ / g, '' ) ;
183+ // decode sourcemap comment and add hash param
184+ comment = comment . split ( ',' ) . pop ( ) ;
185+ var sourcemap = JSON . parse ( atob ( comment ) ) ;
186+ for ( var i = 0 ; i < sourcemap . sources . length ; i ++ ) {
187+ sourcemap . sources [ i ] += "?rev=" + revision ;
188+ }
189+ // re-encode to sourcemap comment
190+ comment = btoa ( JSON . stringify ( sourcemap ) ) ;
191+ comment = '//# sourceMappingURL=data:application/json;base64,' + comment ;
192+ meta . sourcemap = comment ;
193+ }
172194 }
173195
174196 /**
0 commit comments