You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

273 lines
7.1 KiB

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