- Include too many features
The main goal of reading vue source code is understanding the whole picture and some core features such as mvvm and virtual dom. Some features make things complicated, such as keep alive, dynamic component and functional component.
- Backward compatibility
Vue source code has a lot of backward compatible code, making it too complicated to understand. For example, Vue2.0 uses defineProperty to implement mvvm for backward compatibility. However, it is much easier to do that with ES6 Proxy.
This project follows Vue3.0 plans. I write a simplest vue step by step to make each commit easy to understand.
For each step, I write test case first, then implement it. (TDD)
In this part, we implement some basic features.
Running npm run test
and then click the DEBUG button in the pop-up browser, you will see the demo.
In this part, we improve mvvm to implement all features in vue2.x. Moreover, we implement "Detection of property addition / deletion" in vue3.0 plan. So, no Vue.$set.
Watcher scheduler solves the problem that changing multiple data triggers rendering multiple times
var cb = jasmine.createSpy('cb');
var vm = new Vue({
data () {
return {
a:1,
b:2,
}
},
render (h) {
cb()
return h('p', null, this.a + this.b)
}
}).$mount()
expect(cb).toHaveBeenCalledTimes(1)
vm.a = 10
vm.b = 11
setTimeout(_ => {
expect(cb).toHaveBeenCalledTimes(2) // change 'a' and 'b' only trigger one render
done()
})
-
Patch
-
Scoped Slot