1- /**
2- * override component directive's resolveComponent function.
3- * When component is resolved:
4- * - remove self from previous component's list
5- * - add self to current component's list
6- */
7-
8- var Vue
1+ var Vue // late bind
92var map = Object . create ( null )
103var shimmed = false
114
5+ /**
6+ * Determine compatibility and apply patch.
7+ *
8+ * @param {Function } vue
9+ */
10+
1211exports . install = function ( vue ) {
1312 if ( shimmed ) return
1413 shimmed = true
@@ -23,25 +22,26 @@ exports.install = function (vue) {
2322 return
2423 }
2524
26- // shim component
27- shimComponent ( Vue . internalDirectives . component )
25+ // patch view directive
26+ patchView ( Vue . internalDirectives . component )
2827 console . log ( '[HMR] vue component hot reload shim applied.' )
2928 // shim router-view if present
3029 var routerView = Vue . elementDirective ( 'router-view' )
3130 if ( routerView ) {
32- shimComponent ( routerView )
31+ patchView ( routerView )
3332 console . log ( '[HMR] vue-router <router-view> hot reload shim applied.' )
3433 }
3534}
3635
3736/**
38- * Shim the component directive.
37+ * Shim the view directive (component or router-view) .
3938 *
40- * @param {Object } dir
39+ * @param {Object } View
4140 */
4241
43- function shimComponent ( dir ) {
44- shimMethod ( dir , 'unbuild' , function ( defer ) {
42+ function patchView ( View ) {
43+ var unbuild = View . unbuild
44+ View . unbuild = function ( defer ) {
4545 if ( ! this . hotUpdating ) {
4646 var prevComponent = this . childVM && this . childVM . constructor
4747 removeComponent ( prevComponent , this )
@@ -51,57 +51,43 @@ function shimComponent (dir) {
5151 addComponent ( this . Component , this )
5252 }
5353 }
54- } )
55- }
56-
57- /**
58- * Shim a directive method.
59- *
60- * @param {Object } dir
61- * @param {String } methodName
62- * @param {Function } fn
63- */
64-
65- function shimMethod ( dir , methodName , fn ) {
66- var original = dir [ methodName ]
67- dir [ methodName ] = function ( ) {
68- fn . apply ( this , arguments )
69- return original . apply ( this , arguments )
54+ // call original
55+ return unbuild . call ( this , defer )
7056 }
7157}
7258
7359/**
74- * Remove a component view from a Component's hot list
60+ * Add a component view to a Component's hot list
7561 *
7662 * @param {Function } Component
7763 * @param {Directive } view - view directive instance
7864 */
7965
80- function removeComponent ( Component , view ) {
66+ function addComponent ( Component , view ) {
8167 var id = Component && Component . options . hotID
8268 if ( id ) {
83- map [ id ] . views . $remove ( view )
69+ if ( ! map [ id ] ) {
70+ map [ id ] = {
71+ Component : Component ,
72+ views : [ ] ,
73+ instances : [ ]
74+ }
75+ }
76+ map [ id ] . views . push ( view )
8477 }
8578}
8679
8780/**
88- * Add a component view to a Component's hot list
81+ * Remove a component view from a Component's hot list
8982 *
9083 * @param {Function } Component
9184 * @param {Directive } view - view directive instance
9285 */
9386
94- function addComponent ( Component , view ) {
87+ function removeComponent ( Component , view ) {
9588 var id = Component && Component . options . hotID
9689 if ( id ) {
97- if ( ! map [ id ] ) {
98- map [ id ] = {
99- Component : Component ,
100- views : [ ] ,
101- instances : [ ]
102- }
103- }
104- map [ id ] . views . push ( view )
90+ map [ id ] . views . $remove ( view )
10591 }
10692}
10793
0 commit comments