You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import{initMixin}from'./init'import{stateMixin}from'./state'import{renderMixin}from'./render'import{eventsMixin}from'./events'import{lifecycleMixin}from'./lifecycle'import{warn}from'../util/index'functionVue(options){// vue 的构造函数if(process.env.NODE_ENV!=='production'&&!(thisinstanceofVue)){warn('Vue is a constructor and should be called with the `new` keyword')}this._init(options)}initMixin(Vue)// 初始化模块stateMixin(Vue)// 状态相关模块eventsMixin(Vue)// 事件相关模块lifecycleMixin(Vue)// 生命周期相关模块renderMixin(Vue)// 页面渲染相关模块exportdefaultVue
源码架构
flow
类似于
typescript
,提供了静态类型检测功能Vue 的 flow 相关类型声明配置都在根目录的 flow 文件夹下
rollup
rollup & webpack
rollup
webpack
Vue 的 rollup 相关构建配置都在 scripts 目录下,构建命令可以在
package.json
文件中找到Runtime Only & Runtime + Compiler 两种模式
Runtime Only 版本通常需要借助 webpack 的 vue-loader 工具把 .vue 文件编译成浏览器可识别的 JS 文件,这个过程是在编译阶段做的,所以只需要用到运行时的 vue 代码,相对来说体积更加轻量
Runtime + Compiler 版本没有对 .vue 文件进行预编译的操作,所以额外的编译器做编译工作,所以对于性能上有所损耗
在实际开发中,更推荐使用 Runtime Only 版本
Tree-Shaking
直译为——树摇,可以理解为通过工具"摇"我们的 JS 文件,将其中用不到的代码"摇"掉。
构建指令
入口
入口在
src/platforms
下,分为 web 和 weex 两种平台。以 web 为例,可以看到之前说的 Runtime Only & Runtime + Compiler 两种模式的入口文件:两个文件入口中都引入了
import Vue from './runtime/index'
Vue 的运行时部分,找到./runtime/index
这部分代码这部分代码引入了 vue 的核心
vue 核心引入了 instance 中的代码,揭晓了 Vue 的最终面目
结论:Vue 本质就是一个构造函数,所以我们一般会使用
new Vue()
的方式来创建 vue 的实例。除此之外,Vue 其它功能模块由对应的 Mixin 来完成,这些 Minxin 把 Vue 当参数传入,然后在 Vue 的 prototype 上拓展功能,所以我们创建的 vue 实例可以直接调用一些 API。问题汇总
如何理解「运行时」(Runtime only) ?
Bench:ssr
的作用?依赖部分没有
dependencies
只有devDependencies
?commitizen
和scripts/verify-commit-msg.js
指令区别?实时编译的性能瓶颈在哪?
为什么不使用
class Vue {}
的形式来实现?问题解答
Runtime + Compiler
&Runtime Only
两种模式,分别对应打包后dist
文件夹下的vue.js
和vue.runtime.js
。我们从
src/platforms/web
入口文件可以看到两种模式对应的入口:rollup
打包到dist
目录的vue.js
中,所以相对于vue.runtime.js
来说体积更大。vue-loader
工具把.vue
文件编译成浏览器可识别的JS
文件。例如将template
转化为对应的render
函数。这个过程是在离线编译阶段做好的,所以只需要用到运行时的vue
代码。bench:ssr
主要对应根目录下的benchmarks/ssr
文件夹,做一些 ssr 相关基准的测试工作,返回相应服务端模板字符串渲染的总耗时。相关代码:
问题待解决
commitizen
以便捷的命令行提示,相对新手比较友好,方便提交代码。scripts/verify-commit-msg.js
无提示,直接做提交检测,相对第一种提交方式更加快捷,但需要一定的开发经验来支撑。实时编译最终需要走到
new Vue()
,vue 的实例相对复杂,私有属性、原型链属性生态庞大,所以开销很大,也是性能的一大瓶颈。原因如下
参考
The text was updated successfully, but these errors were encountered: