Writing in the front
Recently, I took some time to read the source code of VUe-next/Runtime-core one by one. During this time, I sorted out a lot of notes, but they were fragmented. Originally wanted to sort out, write an article to share, but felt that the final results can only be a huge length of the analysis of the text, even if I annotate the source code line by line, its reading experience will be very poor, because everyone read code habits are different, different ideas. Is the so-called throw a brick to attract jade, so, I feel to write a guide as this brick should be enough, I hope to help to want to see the source code but feel unable to see, do not know how to start readers.
On the other hand, it’s kind of digging a hole for yourself, because a lot of what’s involved in this article can’t be explained in a few words, which means that other articles will have to be written later to fill in the gaps. I try to put together as many cohesive modules as possible, and then share them. I try to improve the quality of the article by avoiding the situation of listing code.
Reading notes I hosted on the wordfinch, if you don’t want to mess it up, you can read them here.
The preparatory work
Work to good its things, must first sharpen its tools, to see the source, take tablet to see certainly is not (of course, also do not exclude cattle). All you need is an editor that supports code hopping. I use VSCode, but if you use VIM, Sublime will do.
In addition, some reserve knowledge is required:
- Because it’s reading
vue-next
Code, and ispre-alpha
The version that this requires you to be on beforevue
Have a certain understanding, if it is the first contact, I feel the meaning of reading the source code is not very great - Need proficiency
debug
The basic skills and processes throughdebug
There are two benefits to looking at code in this way- The debug process has a clear call stack log
- The variables under various scopes are clear at a glance
- The need to
typescript
Some degree of control, at least to knowinterface
,enum
Concepts such as
How to read
There are generally three ways:
- Look directly
- Executable code that passes unit tests
- Self-written executable code
The second option is recommended because unit tests are maintained by the official team and the quality is guaranteed, and unit tests are generally simple and annotated, which helps us understand the code.
Vue-next uses jest for unit tests, so you can install the jest plug-in in vscode. It supports the debug lens shortcut to directly debug a unit test.
Note, however, that you configure a custom option:
"jest.debugCodeLens.showWhenTestStateIn": [
"fail"."unknown"."pass", // notice here]Copy the code
The “pass” indicates that the Debug Lens is displayed above even if the unit test passes. By default, the Debug lens is not displayed if the unit test case passes.
Module function induction
There are multiple files in the Run-time core directory, so I’ll treat each file as a submodule for the moment. The code quality of VUE is still good, and the coupling between modules is not particularly high. Because of this, basically each module has its own corresponding unit test file.
When I was looking at it, I was basically looking at unit tests of each module, and then during debugging, I would take the initiative to jump through some code to see the implementation details. The following are some summaries and generalizations of the functions of all modules in this directory under the current latest code.
There are a few more individual modules that I haven’t looked through yet, but it doesn’t affect the overall source-reading process, so I’ll come back to supplement these todos later when I work on these individual modules separately.
publicapi
related
Modules that implement the public API are named in a format like APIxxx, as follows:
apiApp.ts
: There are three more important interfaces to look at,App
,AppConfig
和AppContext
, if forvue2
If you’re familiar with it, it’s easy to understand.createApp
Is a factory method that returns a matchApp
An object whose internal methods are bound by an interfaceAppContext
Interface to interact with objects constrained by.apiCreateComponent.ts
: This file contains multiple internal values forcreateComponent
Overloading declarations for function signatures should exist to help TS provide better type inference to improve the development experience.apiInject.ts
: Component dependency injectionfeature
The implementation of logic, implementation is very simple, direct andcomponent
It was exposed in the filecurrentInstance
Variables interact to achieve inheritance, assignment, value and other logicapiLifecycle.ts
: New component declaration cyclehooks
, basically seeinjectHook
The method will do. Heretarget
Point by defaultcurrentInstance
, and then some callback logic is cachedcurrentInstance
Instance of the declaration cycle callback function arrayapiOptions.ts
: contains the value forcomponent
How to parseoptions
The implementation of logic, the code is relatively long but also more complex, coupling and other several files higher. But it’s not really necessary to go through all the code inside it, becauseoptions
The parsing logic for each paragraph is independent of the other and can therefore be specific to oneoption
To look at the internal parsing logic, I’ve only looked at it so fardata
As well aslifecycle
.apiReactivity.ts
: is thereactivity
In the packageapi
Re – export, nothing extraapiWatch.ts
: I haven’t looked at it carefully yet, but according to the name, it’s andwatch
The relevantapi
After a cursory look, the coupling is low, so you can look at it later
Component related
component.ts
: mainly contains how to create an internal component instance of the logic, the code is relatively long, but if you look at it, you will find that it is actually calling other modules exposed API, the logic itself is relatively simple. Note that this file will expose a pairsetCurrentInstance
和getCurrentInstance
Methods are used to maintaincurrentInstance
Variable, and it will be used in other modulescomponentProxy.ts
: a statementrender proxy
The implementation logic of thisproxy
Is responsible for how the external interacts with the internal component instance, think of it as an external component instancecomponentProps.ts
: the main seeresolveProps
, how does the implementation parse the various forms ofprops
componentSlots.ts
: the main seeresolveChildren
, how does the implementation parse the various forms ofchildren
nodecomponentRendererUtils.ts
: some render componentsutil
Method, according to the name to understand the meaning of each methodcreateRenderer.ts
: This has a low coupling with other files and can be understood asVNode
Can render in any context, as long as its interface is implemented, such as applets,native
,canvas
Or memory environment, how to write onerenderer
Directly, seeruntime-test
orruntime-dom
The code fordirectives.ts
: Instruction-related interiorapi
, the current version of the code, which can be manytodo
So you can come back later
other
errorHandling.ts
: Error handling related, I haven’t looked at it carefully yetscheduler.ts
: The job scheduler is related, and I haven’t looked at it carefully yetshapeFlags.ts
: and the components themselveschildren
Type enumeration declarations and constantssuspense.ts
:suspense
Relevant, haven’t seen it yet, for other filessuspense
I was completely following the logicreact
I did not encounter any obstacles at the momentwarning.ts
: Warning related, mostly util methods, by name
Recommended reading order
Speaking directly about my own reading order, I think it is relatively consistent with cognitive habits:
- See first
vnode.ts
和h.ts
Such as onvdom
Look at the new codeVNode
Data structure of - And then see
apiApp.ts
How is the mounting processVNode
Associated with the rendering context, this process naturally involves a variety ofapixxx.ts
It would be nice to look at them one by one - Because what WE saw before is public
api
If you need implementation details, look furthercomponent.ts
, which mainly contains the data structure of the internal component instance and the creation process, again, the break point reads line by line - For some parsing, tool methods, you can see the implementation details at the end of the process, there is no need to explore the end of the process, because some method names can be very clear behind the implementation of the logic is what
Code will encounter during the period of suspense, lifecycle, this kind of code can also be read as a separate content, look at the very beginning, also can not be too obsessed with detail, when have a probably understanding of whole process then in retrospect will clear a lot, then I will pack a special article is how to achieve this.
Write in the last
Although the vuE-Next code is still in the early stage, the overall reading experience is good, with a clear structure and high readability. Some key modules are also annotated. The only deficiency is that many places still use the AS keyword to conduct type assertion. I think there’s probably a better way to do type inference in these places.
Focus on full stack _101, only talk about technology, not life
Another: all kinds of scale full stack outsourcing projects, interested in private chat