compiler-sfc
This is the source code of Vue.js 2.x, a progressive JavaScript framework for building user interfaces. It includes the complete runtime (reactivity system, virtual DOM, component instance lifecycle), the template compiler that transforms HTML templates into render functions, server-side rendering, Single File Component (.vue) compilation, and a backport of Vue 3 Composition API features (ref, reactive, watch, setup) for Vue 2 compatibility.
Tech Stack
Key Features
- Reactive Observation System
- Virtual DOM & Patching
- Component Instance Lifecycle
- Template Compiler
- Global API (Vue.extend, Vue.mixin, Vue.use, Vue.component/directive/filter)
- Web Platform Runtime
- KeepAlive
- Vue 3 Composition API Backport
- SFC Compiler Package
- Server-Side Renderer
- Multi-Format Build System
Entry Points
Start here when working with this codebase
compiler-sfc/index.jsApplication entry point
Start here to understand how the application initializes
Coding Conventions
Standards and patterns used in this codebase
Use TypeScript for source code with internal type definitions separated into a dedicated `src/types` directory, while public-facing type declarations live in a top-level `types` directory. Prefer interfaces and type aliases over inline types for complex structures.
Internal types in src/types/component.ts, src/types/vnode.ts; public declarations in types/vue.d.ts, types/options.d.tsOrganize code by platform and concern using a layered architecture: shared utilities at the bottom, core platform-agnostic logic in the middle, and platform-specific implementations (web, server) at the top. Each module has an index entry point that re-exports its public API.
src/shared (utilities) → src/core (platform-agnostic) → src/platforms/web (platform-specific). Entry points like compiler-sfc/index.js re-export from packages.Use Rollup for bundling with a centralized configuration generator that defines multiple build targets (runtime-only, full, compiler-only, ESM, CJS, UMD). Build configs are generated programmatically from a declarative target map in scripts/config.js, with module aliases resolved via scripts/alias.js.
scripts/config.js defines builds like 'web-runtime-cjs-dev', 'web-full-esm', etc. scripts/build.js orchestrates compilation with optional minification.Use compile-time feature flags as global boolean constants (declared in global.d.ts) to enable tree-shaking of optional features. Feature flags are replaced at build time and declared globally for TypeScript.
declare var __WEEX__: boolean; declare var __SSR__: boolean; Feature flags in scripts/feature-flags.js control experimental compiler features.Use Vitest as the test framework with a dedicated setup file that configures global test utilities and environment variables. Path aliases from the build system are mirrored in the test configuration for consistent module resolution.
vitest.config.ts sets up resolve.alias matching scripts/alias.js paths, and test/vitest.setup.ts configures global utilities.Backport newer API patterns (Vue 3 Composition API) into the existing codebase under a dedicated directory (src/v3) with clear separation from the original implementation. Maintain backward compatibility while exposing new APIs.
src/v3/reactivity contains ref, reactive, computed backports; src/v3/sfc-helpers provides useCssModule, useCssVars; these coexist with the original Options API in src/core.Automate releases with a script that handles version bumping, changelog generation, building, testing, git tagging, and npm publishing for multiple packages in sequence. Support both stable and pre-release workflows.
scripts/release.js handles versioning, runs build and tests, publishes vue, compiler-sfc, and server-renderer packages, then pushes git tags.For sub-packages that are published separately but built from the monorepo, use thin re-export modules (index.js, index.mjs, index.d.ts) that delegate to the actual compiled package, supporting both CJS and ESM consumers.
compiler-sfc/index.js: module.exports = require('@vue/compiler-sfc'); compiler-sfc/index.mjs: export * from '@vue/compiler-sfc'Separate the virtual DOM system (vnode creation, diffing/patching) from the component instance system (lifecycle, state, events) and the reactivity system (observer, dep, watcher, scheduler). Each subsystem is self-contained with clear interfaces between them.
src/core/observer (reactivity), src/core/instance (component), src/core/vdom (virtual DOM) are independent subsystems that compose together.