@@ -6,6 +6,7 @@ var Template = require("./Template");
66var BasicEvaluatedExpression = require ( "./BasicEvaluatedExpression" ) ;
77var ModuleHotAcceptDependency = require ( "./dependencies/ModuleHotAcceptDependency" ) ;
88var ModuleHotDeclineDependency = require ( "./dependencies/ModuleHotDeclineDependency" ) ;
9+ var RawSource = require ( "webpack-core/lib/RawSource" ) ;
910
1011function HotModuleReplacementPlugin ( outputOptions ) {
1112 this . outputOptions = outputOptions ;
@@ -14,6 +15,7 @@ module.exports = HotModuleReplacementPlugin;
1415
1516HotModuleReplacementPlugin . prototype . apply = function ( compiler ) {
1617 var hotUpdateChunkFilename = this . outputOptions . hotUpdateChunkFilename || "[id].[hash].hot-update.js" ;
18+ var hotUpdateMainFilename = this . outputOptions . hotUpdateMainFilename || "[hash].hot-update.json" ;
1719 var hotUpdateFunction = this . outputOptions . hotUpdateFunction || ( "webpackHotUpdate" + ( this . outputOptions . library || "" ) ) ;
1820 compiler . plugin ( "compilation" , function ( compilation , params ) {
1921 var hotUpdateChunkTemplate = compilation . compiler . hotUpdateChunkTemplate ;
@@ -47,7 +49,24 @@ HotModuleReplacementPlugin.prototype.apply = function(compiler) {
4749 records . chunkModuleIds [ chunk . id ] = chunk . modules . map ( function ( m ) { return m . id ; } ) ;
4850 } ) ;
4951 } ) ;
50- compilation . plugin ( "after-chunk-assets" , function ( ) {
52+ compilation . plugin ( "after-hash" , function ( ) {
53+ var records = this . records ;
54+ if ( ! records ) return ;
55+ if ( ! records . hash ) return ;
56+ if ( ! records . preHash ) {
57+ records . preHash = this . hash ;
58+ return ;
59+ }
60+ if ( records . preHash === this . hash ) {
61+ if ( records . prepreHash )
62+ this . modifyHash ( records . prepreHash ) ;
63+ return ;
64+ }
65+ records . preHash = this . hash ;
66+ records . prepreHash = records . preHash ;
67+ this . modifyHash ( records . preHash ) ;
68+ } ) ;
69+ compilation . plugin ( "additional-chunk-assets" , function ( ) {
5170 var records = this . records ;
5271 if ( records . hash === this . hash ) return ;
5372 if ( ! records . moduleHashs || ! records . chunkHashs || ! records . chunkModuleIds ) return ;
@@ -59,6 +78,10 @@ HotModuleReplacementPlugin.prototype.apply = function(compiler) {
5978 hash = hash . digest ( "hex" ) ;
6079 module . hotUpdate = records . moduleHashs [ identifier ] !== hash ;
6180 } ) ;
81+ var hotUpdateMainContent = {
82+ h : this . hash ,
83+ c : [ ]
84+ } ;
6285 Object . keys ( records . chunkHashs ) . forEach ( function ( chunkId ) {
6386 chunkId = + chunkId ;
6487 var newModules = [ ] ;
@@ -77,28 +100,34 @@ HotModuleReplacementPlugin.prototype.apply = function(compiler) {
77100 } ) ;
78101 }
79102 newModules = removedModules . concat ( newModules ) ;
80- var source = hotUpdateChunkTemplate . render ( chunkId , newModules , this . hash , this . moduleTemplate , this . dependencyTemplates ) ;
81- var filename = hotUpdateChunkFilename
82- . replace ( Template . REGEXP_HASH , records . hash )
83- . replace ( Template . REGEXP_CHUNKHASH , records . chunkHashs [ chunkId ] )
84- . replace ( Template . REGEXP_ID , chunkId ) ;
85- this . assets [ filename ] = source ;
86- if ( currentChunk ) {
87- currentChunk . files . push ( filename ) ;
88- this . applyPlugins ( "chunk-asset" , currentChunk , filename ) ;
89- } else if ( this . chunks [ 0 ] ) {
90- // TODO: good place for this? (minimizing is only on chunk assets)
91- this . chunks [ 0 ] . files . push ( filename ) ;
92- this . applyPlugins ( "chunk-asset" , this . chunks [ 0 ] , filename ) ;
103+ if ( newModules . length > 0 ) {
104+ var source = hotUpdateChunkTemplate . render ( chunkId , newModules , this . hash , this . moduleTemplate , this . dependencyTemplates ) ;
105+ var filename = hotUpdateChunkFilename
106+ . replace ( Template . REGEXP_HASH , records . hash )
107+ . replace ( Template . REGEXP_ID , chunkId ) ;
108+ this . assets [ filename ] = source ;
109+ hotUpdateMainContent . c . push ( chunkId ) ;
110+ if ( currentChunk ) {
111+ currentChunk . files . push ( filename ) ;
112+ this . applyPlugins ( "chunk-asset" , currentChunk , filename ) ;
113+ } else {
114+ this . additionalChunkAssets . push ( filename ) ;
115+ this . applyPlugins ( "chunk-asset" , null , filename ) ;
116+ }
93117 }
94118 } , this ) ;
119+ var source = new RawSource ( JSON . stringify ( hotUpdateMainContent ) ) ;
120+ var filename = hotUpdateMainFilename . replace ( Template . REGEXP_HASH , records . hash ) ;
121+ this . assets [ filename ] = source ;
122+ this . additionalChunkAssets . push ( filename ) ;
123+ this . applyPlugins ( "chunk-asset" , null , filename ) ;
95124 } ) ;
96125
97126 var mainTemplate = compilation . mainTemplate ;
98127 compilation . mainTemplate = Object . create ( mainTemplate ) ;
99128
100129 compilation . mainTemplate . updateHash = function ( hash ) {
101- hash . update ( compilation . records . hash + " ") ;
130+ hash . update ( "HotMainTemplateDecorator ") ;
102131 mainTemplate . updateHash ( hash ) ;
103132 } ;
104133
@@ -111,9 +140,12 @@ HotModuleReplacementPlugin.prototype.apply = function(compiler) {
111140 var currentHotUpdateChunkFilename = JSON . stringify ( hotUpdateChunkFilename )
112141 . replace ( Template . REGEXP_HASH , "\" + " + this . renderCurrentHashCode ( hash ) + " + \"" )
113142 . replace ( Template . REGEXP_ID , "\" + chunkId + \"" ) ;
143+ var currentHotUpdateMainFilename = JSON . stringify ( hotUpdateMainFilename )
144+ . replace ( Template . REGEXP_HASH , "\" + " + this . renderCurrentHashCode ( hash ) + " + \"" ) ;
114145 buf . push ( "this[" + JSON . stringify ( hotUpdateFunction ) + "] = " +
115146 ( hotInitCode
116147 . replace ( / \$ r e q u i r e \$ / g, this . requireFn )
148+ . replace ( / \$ h o t M a i n F i l e n a m e \$ / g, currentHotUpdateMainFilename )
117149 . replace ( / \$ h o t C h u n k F i l e n a m e \$ / g, currentHotUpdateChunkFilename )
118150 . replace ( / \$ h a s h \$ / g, JSON . stringify ( hash ) )
119151 . 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;" ) ) ) ;
@@ -168,14 +200,13 @@ HotModuleReplacementPlugin.prototype.apply = function(compiler) {
168200} ;
169201
170202var hotInitCode = function ( ) {
171- function webpackHotUpdateCallback ( newHash , chunkId , moreModules ) {
203+ function webpackHotUpdateCallback ( chunkId , moreModules ) {
172204 for ( var moduleId in moreModules ) {
173205 if ( moreModules [ moduleId ] )
174206 hotUpdate [ moduleId ] = moreModules [ moduleId ] ;
175207 else if ( ! hotUpdate [ moduleId ] )
176208 hotUpdate [ moduleId ] = false ;
177209 }
178- hotUpdateNewHash = newHash ;
179210 if ( -- hotWaitingFiles === 0 && hotChunksLoading === 0 ) {
180211 hotUpdateDownloaded ( ) ;
181212 }
@@ -298,6 +329,7 @@ var hotInitCode = function() {
298329 var hotWaitingFiles = 0 ;
299330 var hotChunksLoading = 0 ;
300331 var hotWaitingFilesMap = { } ;
332+ var hotAvailibleFilesMap = { } ;
301333 var hotCallback ;
302334
303335 // The update info
@@ -312,8 +344,7 @@ var hotInitCode = function() {
312344
313345 try {
314346 var request = new XMLHttpRequest ( ) ;
315- var chunkId = 0 ;
316- request . open ( "HEAD" , modules . c + $hotChunkFilename$ , true ) ;
347+ request . open ( "GET" , modules . c + $hotMainFilename$ , true ) ;
317348 request . send ( null ) ;
318349 } catch ( err ) {
319350 return callback ( err ) ;
@@ -327,26 +358,43 @@ var hotInitCode = function() {
327358
328359 } else {
329360
361+ try {
362+ var update = JSON . parse ( request . responseText ) ;
363+ } catch ( e ) {
364+ hotSetStatus ( "idle" ) ;
365+ callback ( null , null ) ;
366+ return ;
367+ }
368+
369+ hotAvailibleFilesMap = { } ;
330370 hotWaitingFilesMap = { } ;
371+ for ( var i = 0 ; i < update . c . length ; i ++ )
372+ hotAvailibleFilesMap [ update . c [ i ] ] = true ;
373+ hotUpdateNewHash = update . h ;
374+
331375 hotSetStatus ( "prepare" ) ;
332376 hotCallback = callback ;
333377 hotUpdate = { } ;
334378 /*foreachInstalledChunks*/ {
335379 hotDownloadUpdateChunk ( chunkId ) ;
336380 }
337-
381+ if ( hotChunksLoading === 0 && hotWaitingFiles === 0 ) {
382+ hotUpdateDownloaded ( ) ;
383+ }
338384 }
339385 } ;
340386 }
341387
342388 function hotDownloadUpdateChunk ( chunkId ) {
343- hotWaitingFiles ++ ;
344- var head = document . getElementsByTagName ( 'head' ) [ 0 ] ;
345- var script = document . createElement ( 'script' ) ;
346- script . type = 'text/javascript' ;
347- script . charset = 'utf-8' ;
348- script . src = modules . c + $hotChunkFilename$ ;
349- head . appendChild ( script ) ;
389+ if ( hotAvailibleFilesMap [ chunkId ] ) {
390+ hotWaitingFiles ++ ;
391+ var head = document . getElementsByTagName ( 'head' ) [ 0 ] ;
392+ var script = document . createElement ( 'script' ) ;
393+ script . type = 'text/javascript' ;
394+ script . charset = 'utf-8' ;
395+ script . src = modules . c + $hotChunkFilename$ ;
396+ head . appendChild ( script ) ;
397+ }
350398 hotWaitingFilesMap [ chunkId ] = true ;
351399 }
352400
0 commit comments