@@ -33,10 +33,7 @@ const getCurrentScriptUrl = function (moduleId) {
3333 }
3434 return fileMap . split ( ',' ) . map ( function ( mapRule ) {
3535 const reg = new RegExp ( filename + '\\.js$' , 'g' ) ;
36- return normalizeUrl (
37- src . replace ( reg , mapRule . replace ( / { f i l e N a m e } / g, filename ) + '.css' ) ,
38- { stripWWW : false } ,
39- ) ;
36+ return normalizeUrl ( src . replace ( reg , mapRule . replace ( / { f i l e N a m e } / g, filename ) + '.css' ) , { stripWWW : false } ) ;
4037 } ) ;
4138 } ;
4239} ;
@@ -46,8 +43,8 @@ function updateCss(el, url) {
4643 url = el . href . split ( '?' ) [ 0 ] ;
4744 }
4845 if ( el . isLoaded === false ) {
49- // We seem to be about to replace a css link that hasn't loaded yet.
50- // We're probably changing the same file more than once.
46+ // We seem to be about to replace a css link that hasn't loaded yet.
47+ // We're probably changing the same file more than once.
5148 return ;
5249 }
5350 if ( ! url || ! ( url . indexOf ( '.css' ) > - 1 ) ) return ;
@@ -58,29 +55,18 @@ function updateCss(el, url) {
5855 newEl . isLoaded = false ;
5956 newEl . addEventListener ( 'load' , function ( ) {
6057 newEl . isLoaded = true ;
61- el . parentNode . removeChild ( el ) ;
58+ el . remove ( ) ;
6259 } ) ;
6360 newEl . addEventListener ( 'error' , function ( ) {
6461 newEl . isLoaded = true ;
65- el . parentNode . removeChild ( el ) ;
62+ el . remove ( ) ;
6663 } ) ;
6764
6865 newEl . href = url + '?' + Date . now ( ) ;
6966 el . parentNode . appendChild ( newEl ) ;
7067}
7168
72- function getReloadUrl ( href , src ) {
73- href = normalizeUrl ( href , { stripWWW : false } ) ;
74- let ret ;
75- src . some ( function ( url ) { // eslint-disable-line array-callback-return
76- if ( href . indexOf ( src ) > - 1 ) {
77- ret = url ;
78- }
79- } ) ;
80- return ret ;
81- }
82-
83- function reloadStyle ( src ) { // eslint-disable-line no-unused-vars
69+ function reloadStyle ( src ) {
8470 const elements = document . querySelectorAll ( 'link' ) ;
8571 let loaded = false ;
8672
@@ -97,6 +83,17 @@ function reloadStyle(src) { // eslint-disable-line no-unused-vars
9783 return loaded ;
9884}
9985
86+ function getReloadUrl ( href , src ) {
87+ href = normalizeUrl ( href , { stripWWW : false } ) ;
88+ let ret ;
89+ src . some ( function ( url ) {
90+ if ( href . indexOf ( src ) > - 1 ) {
91+ ret = url ;
92+ }
93+ } ) ;
94+ return ret ;
95+ }
96+
10097function reloadAll ( ) {
10198 const elements = document . querySelectorAll ( 'link' ) ;
10299 forEach . call ( elements , function ( el ) {
@@ -106,19 +103,21 @@ function reloadAll() {
106103}
107104
108105module . exports = function ( moduleId , options ) {
106+ let getScriptSrc ;
107+
109108 if ( noDocument ) {
110109 return noop ;
111110 }
112111
113- const getScriptSrc = getCurrentScriptUrl ( moduleId ) ;
112+ getScriptSrc = getCurrentScriptUrl ( moduleId ) ;
114113
115114 function update ( ) {
116115 const src = getScriptSrc ( options . fileMap ) ;
117- const reloaded = false ; // hack of all hacks...for now
118- if ( reloaded ) {
119- console . log ( '[HMR] css reload %s' , src . join ( ' ' ) ) ; // eslint-disable-line no-console
116+ const reloaded = reloadStyle ( src ) ;
117+ if ( reloaded && ! options . reloadAll ) {
118+ console . log ( '[HMR] css reload %s' , src . join ( ' ' ) ) ;
120119 } else {
121- console . log ( '[HMR] Reload all css' ) ; // eslint-disable-line no-console
120+ console . log ( '[HMR] Reload all css' ) ;
122121 reloadAll ( ) ;
123122 }
124123 }
0 commit comments