index.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. var Vue // late bind
  2. var version
  3. var map = (window.__VUE_HOT_MAP__ = Object.create(null))
  4. var installed = false
  5. var isBrowserify = false
  6. var initHookName = 'beforeCreate'
  7. exports.install = function (vue, browserify) {
  8. if (installed) { return }
  9. installed = true
  10. Vue = vue.__esModule ? vue.default : vue
  11. version = Vue.version.split('.').map(Number)
  12. isBrowserify = browserify
  13. // compat with < 2.0.0-alpha.7
  14. if (Vue.config._lifecycleHooks.indexOf('init') > -1) {
  15. initHookName = 'init'
  16. }
  17. exports.compatible = version[0] >= 2
  18. if (!exports.compatible) {
  19. console.warn(
  20. '[HMR] You are using a version of vue-hot-reload-api that is ' +
  21. 'only compatible with Vue.js core ^2.0.0.'
  22. )
  23. return
  24. }
  25. }
  26. /**
  27. * Create a record for a hot module, which keeps track of its constructor
  28. * and instances
  29. *
  30. * @param {String} id
  31. * @param {Object} options
  32. */
  33. exports.createRecord = function (id, options) {
  34. if(map[id]) { return }
  35. var Ctor = null
  36. if (typeof options === 'function') {
  37. Ctor = options
  38. options = Ctor.options
  39. }
  40. makeOptionsHot(id, options)
  41. map[id] = {
  42. Ctor: Ctor,
  43. options: options,
  44. instances: []
  45. }
  46. }
  47. /**
  48. * Check if module is recorded
  49. *
  50. * @param {String} id
  51. */
  52. exports.isRecorded = function (id) {
  53. return typeof map[id] !== 'undefined'
  54. }
  55. /**
  56. * Make a Component options object hot.
  57. *
  58. * @param {String} id
  59. * @param {Object} options
  60. */
  61. function makeOptionsHot(id, options) {
  62. if (options.functional) {
  63. var render = options.render
  64. options.render = function (h, ctx) {
  65. var instances = map[id].instances
  66. if (ctx && instances.indexOf(ctx.parent) < 0) {
  67. instances.push(ctx.parent)
  68. }
  69. return render(h, ctx)
  70. }
  71. } else {
  72. injectHook(options, initHookName, function() {
  73. var record = map[id]
  74. if (!record.Ctor) {
  75. record.Ctor = this.constructor
  76. }
  77. record.instances.push(this)
  78. })
  79. injectHook(options, 'beforeDestroy', function() {
  80. var instances = map[id].instances
  81. instances.splice(instances.indexOf(this), 1)
  82. })
  83. }
  84. }
  85. /**
  86. * Inject a hook to a hot reloadable component so that
  87. * we can keep track of it.
  88. *
  89. * @param {Object} options
  90. * @param {String} name
  91. * @param {Function} hook
  92. */
  93. function injectHook(options, name, hook) {
  94. var existing = options[name]
  95. options[name] = existing
  96. ? Array.isArray(existing) ? existing.concat(hook) : [existing, hook]
  97. : [hook]
  98. }
  99. function tryWrap(fn) {
  100. return function (id, arg) {
  101. try {
  102. fn(id, arg)
  103. } catch (e) {
  104. console.error(e)
  105. console.warn(
  106. 'Something went wrong during Vue component hot-reload. Full reload required.'
  107. )
  108. }
  109. }
  110. }
  111. function updateOptions (oldOptions, newOptions) {
  112. for (var key in oldOptions) {
  113. if (!(key in newOptions)) {
  114. delete oldOptions[key]
  115. }
  116. }
  117. for (var key$1 in newOptions) {
  118. oldOptions[key$1] = newOptions[key$1]
  119. }
  120. }
  121. exports.rerender = tryWrap(function (id, options) {
  122. var record = map[id]
  123. if (!options) {
  124. record.instances.slice().forEach(function (instance) {
  125. instance.$forceUpdate()
  126. })
  127. return
  128. }
  129. if (typeof options === 'function') {
  130. options = options.options
  131. }
  132. if (record.Ctor) {
  133. record.Ctor.options.render = options.render
  134. record.Ctor.options.staticRenderFns = options.staticRenderFns
  135. record.instances.slice().forEach(function (instance) {
  136. instance.$options.render = options.render
  137. instance.$options.staticRenderFns = options.staticRenderFns
  138. // reset static trees
  139. // pre 2.5, all static trees are cahced together on the instance
  140. if (instance._staticTrees) {
  141. instance._staticTrees = []
  142. }
  143. // 2.5.0
  144. if (Array.isArray(record.Ctor.options.cached)) {
  145. record.Ctor.options.cached = []
  146. }
  147. // 2.5.3
  148. if (Array.isArray(instance.$options.cached)) {
  149. instance.$options.cached = []
  150. }
  151. // post 2.5.4: v-once trees are cached on instance._staticTrees.
  152. // Pure static trees are cached on the staticRenderFns array
  153. // (both already reset above)
  154. instance.$forceUpdate()
  155. })
  156. } else {
  157. // functional or no instance created yet
  158. record.options.render = options.render
  159. record.options.staticRenderFns = options.staticRenderFns
  160. // handle functional component re-render
  161. if (record.options.functional) {
  162. // rerender with full options
  163. if (Object.keys(options).length > 2) {
  164. updateOptions(record.options, options)
  165. } else {
  166. // template-only rerender.
  167. // need to inject the style injection code for CSS modules
  168. // to work properly.
  169. var injectStyles = record.options._injectStyles
  170. if (injectStyles) {
  171. var render = options.render
  172. record.options.render = function (h, ctx) {
  173. injectStyles.call(ctx)
  174. return render(h, ctx)
  175. }
  176. }
  177. }
  178. record.options._Ctor = null
  179. // 2.5.3
  180. if (Array.isArray(record.options.cached)) {
  181. record.options.cached = []
  182. }
  183. record.instances.slice().forEach(function (instance) {
  184. instance.$forceUpdate()
  185. })
  186. }
  187. }
  188. })
  189. exports.reload = tryWrap(function (id, options) {
  190. var record = map[id]
  191. if (options) {
  192. if (typeof options === 'function') {
  193. options = options.options
  194. }
  195. makeOptionsHot(id, options)
  196. if (record.Ctor) {
  197. if (version[1] < 2) {
  198. // preserve pre 2.2 behavior for global mixin handling
  199. record.Ctor.extendOptions = options
  200. }
  201. var newCtor = record.Ctor.super.extend(options)
  202. record.Ctor.options = newCtor.options
  203. record.Ctor.cid = newCtor.cid
  204. record.Ctor.prototype = newCtor.prototype
  205. if (newCtor.release) {
  206. // temporary global mixin strategy used in < 2.0.0-alpha.6
  207. newCtor.release()
  208. }
  209. } else {
  210. updateOptions(record.options, options)
  211. }
  212. }
  213. record.instances.slice().forEach(function (instance) {
  214. if (instance.$vnode && instance.$vnode.context) {
  215. instance.$vnode.context.$forceUpdate()
  216. } else {
  217. console.warn(
  218. 'Root or manually mounted instance modified. Full reload required.'
  219. )
  220. }
  221. })
  222. })