File tree Expand file tree Collapse file tree 3 files changed +49
-2
lines changed Expand file tree Collapse file tree 3 files changed +49
-2
lines changed Original file line number Diff line number Diff line change
1
+ function forceRedraw ( element ) {
2
+ var n = document . createTextNode ( ' ' ) ;
3
+ var visibility = element . style . visibility ;
4
+ element . appendChild ( n ) ;
5
+ element . style . visibility = 'hidden' ;
6
+ setTimeout ( function ( ) {
7
+ element . style . visibility = visibility ;
8
+ n . parentNode . removeChild ( n ) ;
9
+ } , 20 ) ;
10
+ }
11
+
12
+ function replaceStylesheet ( styleSheet , url ) {
13
+ // Wait until the extract module is complete
14
+ styleSheet . href = url ;
15
+ setTimeout ( function ( ) {
16
+ console . log ( '[HMR]' , 'Reload css: ' , url ) ;
17
+ forceRedraw ( document . body ) ;
18
+ } , 100 ) ;
19
+ }
20
+
21
+ module . exports = function ( compilationHash , outputFilename ) {
22
+ if ( document ) {
23
+ var styleSheets = document . getElementsByTagName ( 'link' ) ;
24
+ for ( var i = 0 ; i < styleSheets . length ; i ++ ) {
25
+ if ( styleSheets [ i ] . href ) {
26
+ var hrefUrl = styleSheets [ i ] . href . split ( '?' ) ;
27
+ var href = hrefUrl [ 0 ] ;
28
+ var hash = hrefUrl [ 1 ] ;
29
+ if ( hash !== compilationHash && href === document . location . origin + '/' + outputFilename ) {
30
+ replaceStylesheet ( styleSheets [ i ] , href + '?' + compilationHash ) ;
31
+ break ;
32
+ }
33
+ }
34
+ }
35
+ }
36
+ }
Original file line number Diff line number Diff line change @@ -304,6 +304,13 @@ ExtractTextPlugin.prototype.apply = function(compiler) {
304
304
} ) ;
305
305
compilation . assets [ file ] = source ;
306
306
chunk . files . push ( file ) ;
307
+
308
+ // Hot module replacement
309
+ extractedChunk . modules . forEach ( function ( module ) {
310
+ var originalModule = module . getOriginalModule ( ) ;
311
+ originalModule . _source . _value = originalModule . _source . _value . replace ( '%%extracted-file%%' , file ) ;
312
+ } ) ;
313
+
307
314
}
308
315
} , this ) ;
309
316
callback ( ) ;
Original file line number Diff line number Diff line change @@ -105,8 +105,12 @@ module.exports.pitch = function(request) {
105
105
} ) ;
106
106
} ) ;
107
107
this [ __dirname ] ( text , query ) ;
108
- if ( text . locals && typeof resultSource !== "undefined" ) {
109
- resultSource += "\nmodule.exports = " + JSON . stringify ( text . locals ) + ";" ;
108
+ if ( resultSource !== "undefined" ) {
109
+ if ( text . locals ) {
110
+ resultSource += "\nmodule.exports = " + JSON . stringify ( text . locals ) + ";" ;
111
+ }
112
+ resultSource += '\nif(module.hot){require("' + require . resolve ( './hotModuleReplacement.js' ) + '")' +
113
+ '("' + compilation . hash + '", "%%extracted-file%%");}'
110
114
}
111
115
} catch ( e ) {
112
116
return callback ( e ) ;
You can’t perform that action at this time.
0 commit comments