资讯专栏INFORMATION COLUMN

vue源码分析系列之入口文件分析

kgbook / 3407人阅读

摘要:中引入了中的中引入了中的中,定义了的构造函数中的原型上挂载了方法,用来做初始化原型上挂载的属性描述符,返回原型上挂载的属性描述符返回原型上挂载与方法,用来为对象新增删除响应式属性原型上挂载方法原型上挂载事件相关的方法。

入口寻找

入口platforms/web/entry-runtime-with-compiler中import了./runtime/index导出的vue。

./runtime/index中引入了core/index中的vue.

core/index中引入了instance/index中的vue

instance/index中,定义了vue的构造函数

instance/index中的vue

</>复制代码

  1. function Vue (options) {
  2. if (process.env.NODE_ENV !== "production" &&
  3. !(this instanceof Vue)
  4. ) {
  5. warn("Vue is a constructor and should be called with the `new` keyword")
  6. }
  7. this._init(options)
  8. }
  9. // 原型上挂载了init方法,用来做初始化
  10. initMixin(Vue)
  11. // 原型上挂载$data的属性描述符getter,返回this._data
  12. // 原型上挂载$props的属性描述符getter, 返回this._props
  13. // 原型上挂载$set$delete方法,用来为对象新增/删除响应式属性
  14. // 原型上挂载$watch方法
  15. stateMixin(Vue)
  16. // 原型上挂载事件相关的方法, $on$once$off$emit
  17. eventsMixin(Vue)
  18. // 原型上挂载_update、$destroy$forceUpdate方法,与组件更新有关。
  19. lifecycleMixin(Vue)
  20. // 原型挂载组件渲染相关方法,_render方法(用来返回vnode,即虚拟dom)
  21. renderMixin(Vue)
  22. export default Vue
core/index中的vue index

</>复制代码

  1. import Vue from "./instance/index"
  2. import { initGlobalAPI } from "./global-api/index"
  3. import { isServerRendering } from "core/util/env"
  4. // 初始化一些全局API
  5. initGlobalAPI(Vue)
  6. // 是否是服务端渲染
  7. Object.defineProperty(Vue.prototype, "$isServer", {
  8. get: isServerRendering // global["process"].env.VUE_ENV === "server"
  9. })
  10. // ssr相关
  11. Object.defineProperty(Vue.prototype, "$ssrContext", {
  12. get () {
  13. /* istanbul ignore next */
  14. return this.$vnode && this.$vnode.ssrContext
  15. }
  16. })
  17. Vue.version = "__VERSION__"
  18. export default Vue
initGlobalAPI

</>复制代码

  1. // 定义vue配置对象,配置对象详情见 import config from "../config"中的备注
  2. const configDef = {}
  3. configDef.get = () => config
  4. Object.defineProperty(Vue, "config", configDef)
  5. // 定义一些内部公用方法
  6. Vue.util = {
  7. warn, // ⚠️警告打印相关
  8. extend, // 浅拷贝函数
  9. mergeOptions, // 配置合并,用到的时候细看
  10. defineReactive // 定义响应式属性的方法。
  11. }
  12. // 静态方法,同$set$delete$nextTick
  13. Vue.set = set
  14. Vue.delete = del
  15. Vue.nextTick = nextTick
  16. Vue.options = Object.create(null)
  17. ASSET_TYPES.forEach(type => {
  18. Vue.options[type + "s"] = Object.create(null)
  19. })
  20. // Vue.options => {"components":{},"directives":{},"filters":{}}
  21. // 跟Weex"s multi-instance scenarios多场景有关
  22. Vue.options._base = Vue;
  23. //将内置组件塞进来
  24. extend(Vue.options.components, builtInComponents)
  25. // 定义Vue.use,主要在应用在插件系统中
  26. initUse(Vue)
  27. // 定义Vue.mixin, 就一句this.options = mergeOptions(this.options, mixin)
  28. initMixin(Vue)
  29. // 定义Vue.extend, 用作原型继承,通过它,可以创建子组件的构造函数
  30. initExtend(Vue)
  31. // 扩展Vue.component,Vue.directive,Vue.filter方法
  32. initAssetRegisters(Vue)
runtime/index中的vue

</>复制代码

  1. import Vue from "core/index"
  2. import config from "core/config"
  3. import { extend, noop } from "shared/util"
  4. import { mountComponent } from "core/instance/lifecycle"
  5. import { devtools, inBrowser, isChrome } from "core/util/index"
  6. import {
  7. query,
  8. mustUseProp,
  9. isReservedTag,
  10. isReservedAttr,
  11. getTagNamespace,
  12. isUnknownElement
  13. } from "web/util/index"
  14. import { patch } from "./patch"
  15. import platformDirectives from "./directives/index"
  16. import platformComponents from "./components/index"
  17. // 一些标签检查类的方法。平台相关
  18. Vue.config.mustUseProp = mustUseProp
  19. Vue.config.isReservedTag = isReservedTag
  20. Vue.config.isReservedAttr = isReservedAttr
  21. Vue.config.getTagNamespace = getTagNamespace
  22. Vue.config.isUnknownElement = isUnknownElement
  23. // 平台相关指令组件
  24. // 指令有model与show
  25. // 组件有Transition与TransitionGroup
  26. extend(Vue.options.directives, platformDirectives)
  27. extend(Vue.options.components, platformComponents)
  28. // install platform patch function
  29. Vue.prototype.__patch__ = inBrowser ? patch : noop
  30. // public mount method
  31. Vue.prototype.$mount = function (
  32. el?: string | Element,
  33. hydrating?: boolean
  34. ): Component {
  35. el = el && inBrowser ? query(el) : undefined
  36. return mountComponent(this, el, hydrating)
  37. }
entry-runtime-with-compiler中的vue

</>复制代码

  1. import config from "core/config"
  2. import { warn, cached } from "core/util/index"
  3. import { mark, measure } from "core/util/perf"
  4. import Vue from "./runtime/index"
  5. import { query } from "./util/index"
  6. import { shouldDecodeNewlines } from "./util/compat"
  7. import { compileToFunctions } from "./compiler/index"
  8. // 根据id返回dom内容
  9. const idToTemplate = cached(id => {
  10. const el = query(id)
  11. return el && el.innerHTML
  12. })
  13. // 重写$mount方法
  14. const mount = Vue.prototype.$mount
  15. Vue.prototype.$mount = function (
  16. el?: string | Element,
  17. hydrating?: boolean
  18. ): Component {
  19. el = el && query(el)
  20. /* 将vue绑定到body或者html元素上的错误提示 */
  21. if (el === document.body || el === document.documentElement) {
  22. process.env.NODE_ENV !== "production" && warn(
  23. `Do not mount Vue to or - mount to normal elements instead.`
  24. )
  25. return this
  26. }
  27. const options = this.$options
  28. // 解析template或者el属性,将其转化为render函数
  29. if (!options.render) {
  30. let template = options.template
  31. // 获得模板字符串
  32. if (template) {
  33. if (typeof template === "string") {
  34. if (template.charAt(0) === "#") {
  35. template = idToTemplate(template)
  36. }
  37. } else if (template.nodeType) {
  38. template = template.innerHTML
  39. } else {
  40. if (process.env.NODE_ENV !== "production") {
  41. warn("invalid template option:" + template, this)
  42. }
  43. return this
  44. }
  45. } else if (el) {
  46. template = getOuterHTML(el)
  47. }
  48. // 获得模板字符串后,编译模板为render函数
  49. if (template) {
  50. /* istanbul ignore if */
  51. if (process.env.NODE_ENV !== "production" && config.performance && mark) {
  52. mark("compile")
  53. }
  54. const { render, staticRenderFns } = compileToFunctions(template, {
  55. shouldDecodeNewlines,
  56. delimiters: options.delimiters,
  57. comments: options.comments
  58. }, this)
  59. options.render = render
  60. options.staticRenderFns = staticRenderFns
  61. /* istanbul ignore if */
  62. if (process.env.NODE_ENV !== "production" && config.performance && mark) {
  63. mark("compile end")
  64. measure(`vue ${this._name} compile`, "compile", "compile end")
  65. }
  66. }
  67. }
  68. return mount.call(this, el, hydrating)
  69. }
  70. /**
  71. * Get outerHTML of elements, taking care
  72. * of SVG elements in IE as well.
  73. */
  74. function getOuterHTML (el: Element): string {
  75. if (el.outerHTML) {
  76. return el.outerHTML
  77. } else {
  78. const container = document.createElement("div")
  79. container.appendChild(el.cloneNode(true))
  80. return container.innerHTML
  81. }
  82. }
  83. Vue.compile = compileToFunctions
  84. export default Vue
文章链接

vue源码分析系列

vue源码分析系列之debug环境搭建

vue源码分析系列之响应式数据(一)

vue源码分析系列之响应式数据(二)

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/101636.html

相关文章

  • vue源码分析系列入debug环境搭建

    摘要:目标是为了可以调试版本的,也就是下的源码,所以主要是的开启。结语至此就可以开心的研究源码啦。文章链接源码分析系列源码分析系列之入口文件分析源码分析系列之响应式数据一源码分析系列之响应式数据二 概述 为了探究vue的本质,所以想debug一下源码,但是怎么开始是个问题,于是有了这样一篇记录。目标是为了可以调试es6版本的,也就是src下的源码,所以主要是sourceMap的开启。原文来自...

    nihao 评论0 收藏0
  • vue源码分析系列响应式数据(四)

    摘要:执行当时传入的回调,并将新值与旧值一并传入。文章链接源码分析系列源码分析系列之环境搭建源码分析系列之入口文件分析源码分析系列之响应式数据一源码分析系列之响应式数据二源码分析系列之响应式数据三 前言 上一节着重讲述了initComputed中的代码,以及数据是如何从computed中到视图层的,以及data修改后如何作用于computed。这一节主要记录initWatcher中的内容。 ...

    GHOST_349178 评论0 收藏0
  • vue源码分析系列

    摘要:本系列文章旨在化繁为简,通读源码,描述背后的实现逻辑。记录方式主要是代码注释文章链接源码分析系列之环境搭建源码分析系列之入口文件分析源码分析系列之响应式数据一源码分析系列之响应式数据二 概述 在使用vue的时候,会遇到很多神奇的地方,比如 修改vue实例中data对象的属性值,会触发dom值的改变;改变dom中的输入,会触发data对应属性的改变,即双向数据绑定。 通过watch可以...

    Joonas 评论0 收藏0
  • vue源码分析系列响应式数据(一)

    摘要:代码初始化部分一个的时候做了什么当我们一个时,实际上执行了的构造函数,这个构造函数内部挂载了很多方法,可以在我的上一篇文章中看到。合并构造函数上挂载的与当前传入的非生产环境,包装实例本身,在后期渲染时候,做一些校验提示输出。 概述 在使用vue的时候,data,computed,watch是一些经常用到的概念,那么他们是怎么实现的呢,让我们从一个小demo开始分析一下它的流程。 dem...

    liujs 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<