Vue 2 Source Code: Introduction and Debugging Setup
🎯 Introduction
This is the first article in the Vue 2 source code course. Rather than teaching API usage, we will open the Vue 2 source and see what happens behind the data, computed, and watch APIs you use every day.
By the end of this article, you will be able to set up a local Vue 2 source debugging environment, set breakpoints and step through the code in a browser, and navigate the source tree to find the code relevant to your questions.
🧱 Why Read the Vue 2 Source?
Reading source code takes time. Is it worthwhile? In my experience, it helps in three main ways.
Debug with a clearer direction. You have probably seen code that looks correct but does not update the view, or an array change that never appears in the template. With API knowledge alone, resolving these issues can involve trial and error. Understanding reactivity explains limitations such as why assigning directly to an array index does not trigger an update, giving you a clear place to start investigating.
Write code that fits the framework. Once you understand dependency tracking with Dep and Watcher, you can see why computed suits cached calculations better than methods, and why multiple assignments can be batched into one update. These become informed choices rather than rules to memorize.
Understand why APIs exist. Some Vue 2 designs, such as this.$set and $nextTick, can seem unusual at first. Reading the source reveals the specific problems they solve. That understanding also helps when learning Vue 3 and other frameworks.
✨ Version Used in This Course
This course uses Vue 2.7.16. Vue 2.7 is the final minor release of the 2.x series. Its API is largely consistent with 2.6, with backward-compatible additions such as the built-in Composition API. It represents the final form of Vue 2, while its core mechanisms—reactivity, virtual DOM, and template compilation—remain consistent across the 2.x series.
If your project uses 2.6, the course still applies: the core code paths are largely the same.
🧭 A Map of the Source Tree
Open the src directory in the vuejs/vue repository. The following tree highlights important files in its main directories. Select a file to see its role; later articles explore these areas in detail.
Here is a quick overview of each directory:
| Directory | Responsibility |
|---|---|
src/core | Core Vue logic: instance initialization, lifecycle, reactivity, and virtual DOM |
src/compiler | Template compilation: parse templates into an AST and generate render functions |
src/platforms | Platform-specific code that assembles the browser version of Vue (web entry point) |
src/server | Server-side rendering (SSR), outside the scope of this course |
src/sfc | Single-file component (.vue) parsing to support build tools |
src/shared | Shared utilities such as type checks and option merging |
The src/core directory contains Vue's core logic and is the main focus of this course: observer handles reactivity, vdom handles virtual DOM and patching, and instance handles initialization and lifecycle management.
A useful sequence to remember is: template → compiler generates a render function → vdom creates virtual nodes → patch updates the real DOM → when data changes, observer notifies the Watcher to run the process again. This is the central execution path of the Vue runtime.
🛠 Set Up the Debugging Environment
Let's put this into practice. Setup takes only a few minutes, and you can then use breakpoints in the actual source to verify the explanations in every article.
This course was verified with Node.js v22. Clone the source and check out 2.7.16:
# Clone the repository (local Node.js version: v22)
git clone https://github.com/vuejs/vue.git vue2-source
cd vue2-source
# Check out v2.7.16
git checkout v2.7.16
# Install dependencies (use the versions in the repository's package.json)
npm install
npm install --legacy-peer-deps or another package manager. This does not affect the debugging steps that follow.After installation, build a development version with source maps:
# Build dist/vue.js with warnings and source maps for debugging
npm run dev
The resulting dist/vue.js is an unminified development build. Create a debugging page in examples, such as examples/debug/01-hello.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Vue 2 Source Debugging</title>
</head>
<body>
<div id="app">{{ message }}</div>
<script src="../../dist/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data() {
return {
message: 'hello vue2 source',
};
},
});
// Change the data after two seconds to observe the update process
setTimeout(() => {
debugger;
app.message = 'data changed';
}, 2000);
</script>
</body>
</html>
Open this HTML file directly in your browser and open the developer tools. Execution will pause at debugger. You can then:
- Find
webpack:///src/core/instance/index.jsin the Sources panel (source maps map the distribution code back to its source). - Set breakpoints in functions such as
initStateandobserve. - Step through execution and inspect data changes and the call stack.
This will be your standard workflow: find the key function → set a breakpoint → step through the data and call stack.
npm run dev produces a development build with source maps, suitable for debugging. The production build (npm run build) minifies the code and removes warnings, making it less suitable for studying the source.💡 Tips for Reading Source Code
Here are a few useful habits:
- Start with a concrete question. Avoid reading everything from the first line. Ask, for example, “How does changing
dataupdate the view?” This leads naturally to theObject.definePropertysetter,Dep.notify, andWatcher.run. - Follow execution from the entry point. Find the
new Vue()entry point (src/core/instance/index.js) and follow initialization step by step. This is more effective than browsing files without a direction. - Verify your understanding with small implementations. After studying a mechanism, write a simplified version in a few dozen lines, such as a small reactivity system or diff algorithm. Implementing it correctly tests your understanding. Several articles include exercises like these.
- Focus on the main logic. You can skip Flow type annotations and compatibility edge cases. Source excerpts in this course are simplified for teaching, with type annotations and nonessential branches removed.
🗺 Course Roadmap
The course has eight articles, starting with the main execution paths before exploring individual mechanisms:
- Introduction and setup (this article): the source tree and debugging environment.
- Initialization: what happens during
new Vue()and the execution order inside_init. - Reactivity: how
Object.definePropertyintercepts reads and writes, and why arrays need special handling. - Dep and Watcher: dependency tracking, update notification, and how data changes reach the view.
- Asynchronous updates and nextTick: batched update queues, microtasks, and macrotasks.
- Virtual DOM and patching: the VNode structure and how patching compares and updates nodes.
- Template compilation: the three-stage template → AST → render function pipeline.
- How computed and watch work: differences between their Watchers and lazy evaluation.
❓ Knowledge Check
Q1: Why start with Vue 2 instead of reading the Vue 3 source directly?
Vue 3 is more widely used today, but Vue 2 has a smaller codebase and a more straightforward structure, making it a useful starting point. Both versions share core ideas such as reactivity, virtual DOM, and template compilation. Studying Vue 2 first makes it easier to recognize what changed in Vue 3.
Q2: Can I follow the course without knowing how to use browser breakpoints?
You can read the articles, but spending a few minutes learning the Chrome DevTools Sources panel, breakpoints, and stepping will help. These are basic tools for reading source code and will be used frequently.
Q3: How do npm run dev and npm run build differ?
npm run dev builds an unminified version with source maps and full warnings for debugging and learning. npm run build produces a minified production version without warnings, which is less suitable for reading the source.
Q4: Are the source excerpts identical to the repository code?
They are simplified for teaching. Flow type annotations and some edge-case branches are removed while the core logic is preserved. Each excerpt identifies the file it is simplified from so you can compare it with the original.
Q5: Do I have to write the small implementations?
No, but it is strongly recommended. Understanding code when reading it and implementing it yourself are different skills. Writing a simplified version in a few dozen lines is a reliable way to check your understanding.
🧪 Exercise
Once your environment is ready, add a count field to data in examples/debug/01-hello.html and render it on the page. Then use browser breakpoints to observe initialization during new Vue().
const app = new Vue({
el: '#app',
data() {
return {
message: 'hello vue2 source',
// Write your code here: add a count field
};
},
});
// Write your code here: change count after three seconds and add debugger before the change
Find src/core/instance/init.js in the Sources panel, set a breakpoint at the initState call, and observe initialization.
🎉 Your Vue 2 source debugging environment is ready, and you now have a map of the course! In the next article, we will start at new Vue() and trace initialization step by step.
