@@ -14,12 +14,11 @@ function HotModuleReplacementPlugin(outputOptions) {
1414module . exports = HotModuleReplacementPlugin ;
1515
1616HotModuleReplacementPlugin . prototype . apply = function ( compiler ) {
17- var hotUpdateChunkFilename = this . outputOptions . hotUpdateChunkFilename || "[id].[hash].hot-update.js" ;
18- var hotUpdateMainFilename = this . outputOptions . hotUpdateMainFilename || "[hash].hot-update.json" ;
19- var hotUpdateFunction = this . outputOptions . hotUpdateFunction || ( "webpackHotUpdate" + ( this . outputOptions . library || "" ) ) ;
17+ var hotUpdateChunkFilename = this . outputOptions . hotUpdateChunkFilename ;
18+ var hotUpdateMainFilename = this . outputOptions . hotUpdateMainFilename ;
2019 compiler . plugin ( "compilation" , function ( compilation , params ) {
2120 var hotUpdateChunkTemplate = compilation . compiler . hotUpdateChunkTemplate ;
22- if ( ! hotUpdateChunkTemplate ) return ;
21+ if ( ! hotUpdateChunkTemplate && ! compilation . mainTemplate . renderHotModuleReplacementInit ) return ;
2322
2423 var normalModuleFactory = params . normalModuleFactory ;
2524 var contextModuleFactory = params . contextModuleFactory ;
@@ -120,18 +119,12 @@ HotModuleReplacementPlugin.prototype.apply = function(compiler) {
120119
121120 compilation . mainTemplate . renderInit = function ( hash , chunk ) {
122121 var buf = mainTemplate . renderInit ( hash , chunk ) ;
123- var currentHotUpdateChunkFilename = JSON . stringify ( hotUpdateChunkFilename )
124- . replace ( Template . REGEXP_HASH , "\" + " + this . renderCurrentHashCode ( hash ) + " + \"" )
125- . replace ( Template . REGEXP_ID , "\" + chunkId + \"" ) ;
126- var currentHotUpdateMainFilename = JSON . stringify ( hotUpdateMainFilename )
127- . replace ( Template . REGEXP_HASH , "\" + " + this . renderCurrentHashCode ( hash ) + " + \"" ) ;
128- buf . push ( "this[" + JSON . stringify ( hotUpdateFunction ) + "] = " +
129- ( hotInitCode
130- . replace ( / \$ r e q u i r e \$ / g, this . requireFn )
131- . replace ( / \$ h o t M a i n F i l e n a m e \$ / g, currentHotUpdateMainFilename )
132- . replace ( / \$ h o t C h u n k F i l e n a m e \$ / g, currentHotUpdateChunkFilename )
133- . replace ( / \$ h a s h \$ / g, JSON . stringify ( hash ) )
134- . replace ( / \/ \* f o r e a c h I n s t a l l e d C h u n k s \* \/ / g, chunk . chunks . length > 0 ? "for(var chunkId in installedChunks)" : "var chunkId = 0;" ) ) ) ;
122+ buf = buf . concat ( this . renderHotModuleReplacementInit ( hash , chunk ) ) ;
123+ buf . push ( "\n\n" ) ;
124+ buf . push ( hotInitCode
125+ . replace ( / \$ r e q u i r e \$ / g, this . requireFn )
126+ . replace ( / \$ h a s h \$ / g, JSON . stringify ( hash ) )
127+ . replace ( / \/ \* f o r e a c h I n s t a l l e d C h u n k s \* \/ / g, chunk . chunks . length > 0 ? "for(var chunkId in installedChunks)" : "var chunkId = 0;" ) ) ;
135128 return buf ;
136129 } ;
137130
@@ -182,15 +175,7 @@ HotModuleReplacementPlugin.prototype.apply = function(compiler) {
182175 } ) ;
183176} ;
184177
185- var hotInitCode = function ( ) {
186- function webpackHotUpdateCallback ( chunkId , moreModules ) {
187- for ( var moduleId in moreModules ) {
188- hotUpdate [ moduleId ] = moreModules [ moduleId ] ;
189- }
190- if ( -- hotWaitingFiles === 0 && hotChunksLoading === 0 ) {
191- hotUpdateDownloaded ( ) ;
192- }
193- }
178+ var hotInitCode = Template . getFunctionContent ( function ( ) {
194179
195180 var hotApplyOnUpdate = true ;
196181 var hotCurrentHash = $hash$ ;
@@ -221,7 +206,7 @@ var hotInitCode = function() {
221206 hotChunksLoading -- ;
222207 if ( hotStatus === "prepare" ) {
223208 if ( ! hotWaitingFilesMap [ chunkId ] ) {
224- hotDownloadUpdateChunk ( chunkId ) ;
209+ hotEnsureUpdateChunk ( chunkId ) ;
225210 }
226211 if ( hotChunksLoading === 0 && hotWaitingFiles === 0 ) {
227212 hotUpdateDownloaded ( ) ;
@@ -230,8 +215,8 @@ var hotInitCode = function() {
230215 }
231216 } ) ;
232217 }
233- fn . cache = $require$ . cache ;
234- fn . modules = $require$ . modules ;
218+ for ( var name in $require$ )
219+ fn [ name ] = $require$ [ name ] ;
235220 return fn ;
236221 }
237222
@@ -316,64 +301,49 @@ var hotInitCode = function() {
316301 function hotCheck ( callback ) {
317302 callback = callback || function ( err ) { if ( err ) throw err } ;
318303 if ( hotStatus !== "idle" ) throw new Error ( "check() is only allowed in idle status" ) ;
319- if ( typeof XMLHttpRequest === "undefined" )
320- return callback ( new Error ( "No browser support" ) ) ;
321304 hotSetStatus ( "check" ) ;
322-
323- try {
324- var request = new XMLHttpRequest ( ) ;
325- request . open ( "GET" , $require$ . p + $hotMainFilename$ , true ) ;
326- request . send ( null ) ;
327- } catch ( err ) {
328- return callback ( err ) ;
329- }
330- request . onreadystatechange = function ( ) {
331- if ( request . readyState !== 4 ) return ;
332- if ( request . status !== 200 && request . status !== 304 ) {
333-
305+ hotDownloadManifest ( function ( err , update ) {
306+ if ( err ) return callback ( err ) ;
307+ if ( ! update ) {
334308 hotSetStatus ( "idle" ) ;
335309 callback ( null , null ) ;
310+ return ;
311+ }
336312
337- } else {
338-
339- try {
340- var update = JSON . parse ( request . responseText ) ;
341- } catch ( e ) {
342- hotSetStatus ( "idle" ) ;
343- callback ( null , null ) ;
344- return ;
345- }
346-
347- hotAvailibleFilesMap = { } ;
348- hotWaitingFilesMap = { } ;
349- for ( var i = 0 ; i < update . c . length ; i ++ )
350- hotAvailibleFilesMap [ update . c [ i ] ] = true ;
351- hotUpdateNewHash = update . h ;
352-
353- hotSetStatus ( "prepare" ) ;
354- hotCallback = callback ;
355- hotUpdate = { } ;
356- /*foreachInstalledChunks*/ {
357- hotDownloadUpdateChunk ( chunkId ) ;
358- }
359- if ( hotChunksLoading === 0 && hotWaitingFiles === 0 ) {
360- hotUpdateDownloaded ( ) ;
361- }
313+ hotAvailibleFilesMap = { } ;
314+ hotWaitingFilesMap = { } ;
315+ for ( var i = 0 ; i < update . c . length ; i ++ )
316+ hotAvailibleFilesMap [ update . c [ i ] ] = true ;
317+ hotUpdateNewHash = update . h ;
318+
319+ hotSetStatus ( "prepare" ) ;
320+ hotCallback = callback ;
321+ hotUpdate = { } ;
322+ /*foreachInstalledChunks*/ {
323+ hotEnsureUpdateChunk ( chunkId ) ;
362324 }
363- } ;
325+ if ( hotChunksLoading === 0 && hotWaitingFiles === 0 ) {
326+ hotUpdateDownloaded ( ) ;
327+ }
328+ } ) ;
329+ }
330+
331+ function hotAddUpdateChunk ( chunkId , moreModules ) {
332+ for ( var moduleId in moreModules ) {
333+ hotUpdate [ moduleId ] = moreModules [ moduleId ] ;
334+ }
335+ if ( -- hotWaitingFiles === 0 && hotChunksLoading === 0 ) {
336+ hotUpdateDownloaded ( ) ;
337+ }
364338 }
365339
366- function hotDownloadUpdateChunk ( chunkId ) {
367- if ( hotAvailibleFilesMap [ chunkId ] ) {
340+ function hotEnsureUpdateChunk ( chunkId ) {
341+ if ( ! hotAvailibleFilesMap [ chunkId ] ) {
342+ hotWaitingFilesMap [ chunkId ] = true ;
343+ } else {
368344 hotWaitingFiles ++ ;
369- var head = document . getElementsByTagName ( 'head' ) [ 0 ] ;
370- var script = document . createElement ( 'script' ) ;
371- script . type = 'text/javascript' ;
372- script . charset = 'utf-8' ;
373- script . src = $require$ . p + $hotChunkFilename$ ;
374- head . appendChild ( script ) ;
345+ hotDownloadUpdateChunk ( chunkId ) ;
375346 }
376- hotWaitingFilesMap [ chunkId ] = true ;
377347 }
378348
379349 function hotUpdateDownloaded ( ) {
@@ -539,4 +509,4 @@ var hotInitCode = function() {
539509 hotSetStatus ( "idle" ) ;
540510 callback ( null , outdatedModules ) ;
541511 }
542- } . toString ( ) . replace ( / ^ f u n c t i o n \s ? \( \) \s ? \{ \n ? | \n ? \} $ / g , "" ) . replace ( / ^ \t / mg , "" ) ;
512+ } ) ;
0 commit comments