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
9
2
var map = Object . create ( null )
10
3
var shimmed = false
11
4
5
+ /**
6
+ * Determine compatibility and apply patch.
7
+ *
8
+ * @param {Function } vue
9
+ */
10
+
12
11
exports . install = function ( vue ) {
13
12
if ( shimmed ) return
14
13
shimmed = true
@@ -23,25 +22,26 @@ exports.install = function (vue) {
23
22
return
24
23
}
25
24
26
- // shim component
27
- shimComponent ( Vue . internalDirectives . component )
25
+ // patch view directive
26
+ patchView ( Vue . internalDirectives . component )
28
27
console . log ( '[HMR] vue component hot reload shim applied.' )
29
28
// shim router-view if present
30
29
var routerView = Vue . elementDirective ( 'router-view' )
31
30
if ( routerView ) {
32
- shimComponent ( routerView )
31
+ patchView ( routerView )
33
32
console . log ( '[HMR] vue-router <router-view> hot reload shim applied.' )
34
33
}
35
34
}
36
35
37
36
/**
38
- * Shim the component directive.
37
+ * Shim the view directive (component or router-view) .
39
38
*
40
- * @param {Object } dir
39
+ * @param {Object } View
41
40
*/
42
41
43
- function shimComponent ( dir ) {
44
- shimMethod ( dir , 'unbuild' , function ( defer ) {
42
+ function patchView ( View ) {
43
+ var unbuild = View . unbuild
44
+ View . unbuild = function ( defer ) {
45
45
if ( ! this . hotUpdating ) {
46
46
var prevComponent = this . childVM && this . childVM . constructor
47
47
removeComponent ( prevComponent , this )
@@ -51,57 +51,43 @@ function shimComponent (dir) {
51
51
addComponent ( this . Component , this )
52
52
}
53
53
}
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 )
70
56
}
71
57
}
72
58
73
59
/**
74
- * Remove a component view from a Component's hot list
60
+ * Add a component view to a Component's hot list
75
61
*
76
62
* @param {Function } Component
77
63
* @param {Directive } view - view directive instance
78
64
*/
79
65
80
- function removeComponent ( Component , view ) {
66
+ function addComponent ( Component , view ) {
81
67
var id = Component && Component . options . hotID
82
68
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 )
84
77
}
85
78
}
86
79
87
80
/**
88
- * Add a component view to a Component's hot list
81
+ * Remove a component view from a Component's hot list
89
82
*
90
83
* @param {Function } Component
91
84
* @param {Directive } view - view directive instance
92
85
*/
93
86
94
- function addComponent ( Component , view ) {
87
+ function removeComponent ( Component , view ) {
95
88
var id = Component && Component . options . hotID
96
89
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 )
105
91
}
106
92
}
107
93
0 commit comments