Ensiklopedia VibeKoding: Principles of Frontend Project Architecture.Ensiklopedia VibeKoding: Principles of Frontend Project Architecture.
How do you choose the right architecture for projects of different sizes, from simple HTML pages to complex enterprise applications? It's like asking: from a studio apartment to a large shopping mall, how do you design different spatial layouts based on needs? Good architecture should evolve with the project, rather than being over-engineered from the start.How do you choose the right architecture for projects of different sizes, from simple HTML pages to complex enterprise applications? It's like asking: from a studio apartment to a large shopping mall, how do you design different spatial layouts based on needs? Good architecture should evolve with the project, rather than being over-engineered from the start.
------
Frontend project architecture should match project complexity. We classify projects into three levels based on technical complexity and user scale:Frontend project architecture should match project complexity. We classify projects into three levels based on technical complexity and user scale:
| Level | Tech Stack | User Scale | Typical Scenarios | Core Focus |
|---|---|---|---|---|
| Beginner | HTML/CSS/JS | Individual/small team | Personal blogs, landing pages, simple tools | Quick launch, simple maintenance |
| Intermediate | Vue/React + build tools | Small-to-medium business | Management systems, e-commerce frontends, SaaS | Component reuse, state management |
| Enterprise | Framework + micro-frontend/SSR | Large applications | Large platforms, complex business systems | Performance optimization, team collaboration, scalability |
Don't over-engineer! Many projects start with simple HTML and gradually introduce frameworks and tools as needs grow. - Personal projects โ Beginner level - Startup MVP โ Beginner or intermediate level - Enterprise management systems โ Intermediate level - Large internet platforms โ Enterprise levelDon't over-engineer! Many projects start with simple HTML and gradually introduce frameworks and tools as needs grow. - Personal projects โ Beginner level - Startup MVP โ Beginner or intermediate level - Enterprise management systems โ Intermediate level - Large internet platforms โ Enterprise level
------
CODE my-simple-project/ โโโ index.html # Homepage โโโ about.html # About page (if needed) โโโ css/ โ โโโ reset.css # Reset styles โ โโโ variables.css # CSS variables (colors, fonts, etc.) โ โโโ components.css # Component styles (buttons, cards, etc.) โ โโโ main.css # Main stylesheet โโโ js/ โ โโโ utils.js # Utility functions โ โโโ api.js # Simple API calls โ โโโ main.js # Main logic โโโ assets/ โ โโโ images/ # Image assets โ โโโ fonts/ # Font files โโโ README.md # Project documentation
HTML: Semantic tags, clear structureHTML: Semantic tags, clear structure
html <!-- index.html --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>My Personal Blog</title> <link rel="stylesheet" href="css/reset.css"> <link rel="stylesheet" href="css/variables.css"> <link rel="stylesheet" href="css/components.css"> <link rel="stylesheet" href="css/main.css"> </head> <body> <header class="site-header"> <nav class="main-nav"> <a href="index.html">Home</a> <a href="about.html">About</a> </nav> </header> <main class="content"> <article class="blog-post"> <h1>Article Title</h1> <p>Article content...</p> </article> </main> <footer class="site-footer"> <p>© 2024 My Blog</p> </footer> <script src="js/utils.js"></script> <script src="js/main.js"></script> </body> </html>
CSS: Use CSS variables to manage themesCSS: Use CSS variables to manage themes
css /* variables.css */ :root { --primary-color: #3498db; --text-color: #333; --bg-color: #fff; --spacing-sm: 8px; --spacing-md: 16px; --spacing-lg: 24px; --font-base: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; } /* components.css - Reusable component styles */ .btn { padding: var(--spacing-sm) var(--spacing-md); border: none; border-radius: 4px; background: var(--primary-color); color: white; cursor: pointer; } .card { padding: var(--spacing-md); border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); }
JavaScript: Modular organization (using ES6 modules or simple splitting)JavaScript: Modular organization (using ES6 modules or simple splitting)
javascript // utils.js const utils = { // Simplified DOM operations $(selector) { return document.querySelector(selector); }, // Simple debounce debounce(fn, delay) { let timer; return function(...args) { clearTimeout(timer); timer = setTimeout(() => fn.apply(this, args), delay); }; }, // Local storage wrapper storage: { get(key) { return JSON.parse(localStorage.getItem(key) || 'null'); }, set(key, value) { localStorage.setItem(key, JSON.stringify(value)); } } }; // main.js document.addEventListener('DOMContentLoaded', () => { // Page initialization logic initNavigation(); loadBlogPosts(); });
โ Do:โ Do:
โ Avoid:โ Avoid:
style="...")Inline styles (style="...")------
CODE my-vue-project/ โโโ public/ # Static assets โ โโโ index.html โ โโโ favicon.ico โโโ src/ โ โโโ assets/ # Styles, images, fonts โ โ โโโ styles/ โ โ โ โโโ variables.scss โ โ โ โโโ mixins.scss โ โ โ โโโ global.scss โ โ โโโ images/ โ โโโ components/ # Shared components โ โ โโโ common/ # Global shared (Button, Modal, etc.) โ โ โ โโโ Button/ โ โ โ โ โโโ index.vue โ โ โ โ โโโ Button.scss โ โ โ โโโ Modal/ โ โ โโโ business/ # Business components (UserCard, etc.) โ โโโ views/ # Page components โ โ โโโ Home/ โ โ โโโ User/ โ โ โ โโโ List.vue โ โ โ โโโ Detail.vue โ โ โโโ Product/ โ โโโ router/ # Route configuration โ โ โโโ index.js โ โโโ stores/ # Pinia/Vuex state management โ โ โโโ user.js โ โ โโโ app.js โ โโโ services/ # API services โ โ โโโ request.js # axios wrapper โ โ โโโ user.js โ โ โโโ product.js โ โโโ utils/ # Utility functions โ โ โโโ format.js โ โ โโโ validate.js โ โ โโโ storage.js โ โโโ composables/ # Composable functions โ โ โโโ useAuth.js โ โ โโโ useLoading.js โ โโโ constants/ # Constant definitions โ โ โโโ index.js โ โโโ App.vue โ โโโ main.js โโโ tests/ # Test files โโโ .env # Environment variables โโโ vite.config.js โโโ package.json โโโ README.md
CODE my-react-project/ โโโ public/ โโโ src/ โ โโโ assets/ โ โโโ components/ โ โ โโโ common/ # Shared components โ โ โ โโโ Button/ โ โ โ โ โโโ index.jsx โ โ โ โ โโโ Button.module.css โ โ โ โโโ Modal/ โ โ โโโ business/ # Business components โ โโโ pages/ # Page components โ โ โโโ Home/ โ โ โโโ User/ โ โ โโโ Product/ โ โโโ hooks/ # Custom Hooks โ โ โโโ useAuth.js โ โ โโโ useFetch.js โ โโโ services/ # API services โ โ โโโ api.js โ โ โโโ userService.js โ โโโ store/ # Redux/Zustand state management โ โ โโโ slices/ โ โ โโโ index.js โ โโโ utils/ โ โโโ constants/ โ โโโ App.jsx โ โโโ main.jsx โโโ tests/ โโโ package.json
Single Responsibility: One component does one thingSingle Responsibility: One component does one thing
vue <!-- โ Bad example: Component does too much --> <template> <div> <form @submit="handleSubmit"> <!-- Form content --> </form> <table> <!-- Data table --> </table> <div class="charts"> <!-- Charts --> </div> </div> </template> <!-- โ Good example: Split into independent components --> <template> <div> <UserForm @submit="fetchData" /> <UserTable :data="users" /> <UserStats :data="users" /> </div> </template>
| State Type | Storage Location | Example |
|---|---|---|
| Global state | Pinia/Redux | User info, login status, theme settings |
| Page state | Page component | List query conditions, pagination info |
| Component state | Component internal | Form inputs, modal show/hide |
| Server state | TanStack Query/SWR | Server data, caching |
Approach 1: Organize by type (suitable for small projects)Approach 1: Organize by type (suitable for small projects)
CODE src/ โโโ components/ # All components โโโ views/ # All pages โโโ stores/ # All state โโโ services/ # All services
Approach 2: Organize by feature (suitable for medium-to-large projects)Approach 2: Organize by feature (suitable for medium-to-large projects)
CODE src/ โโโ features/ โ โโโ auth/ # All code for authentication feature โ โโโ user/ # All code for user feature โ โโโ product/ # All code for product feature โโโ shared/ # Shared resources โโโ App.vue
- Project pages < 10 โ Organize by type - Project pages > 20 โ Organize by feature - Team > 5 people โ Organize by feature for parallel development- Project pages < 10 โ Organize by type - Project pages > 20 โ Organize by feature - Team > 5 people โ Organize by feature for parallel development
------
When a project grows to a certain size and a single codebase becomes difficult to maintain, micro-frontend architecture can be considered.When a project grows to a certain size and a single codebase becomes difficult to maintain, micro-frontend architecture can be considered.
CODE Large E-Commerce Platform/ โโโ Base Application (Main Framework) โ โโโ Top Navigation โ โโโ Side Menu โ โโโ User Center Entry โ โโโ Sub-application Container โโโ Product Sub-application (Independently deployed) โ โโโ Product List โ โโโ Product Details โ โโโ Product Management โโโ Order Sub-application (Independently deployed) โ โโโ Shopping Cart โ โโโ Order List โ โโโ Payment Flow โโโ User Sub-application (Independently deployed) โ โโโ Personal Center โ โโโ Shipping Addresses โ โโโ Coupons โโโ Marketing Sub-application (Independently deployed) โโโ Campaign Pages โโโ Coupon Distribution โโโ Points Mall
Advantages of micro-frontends:Advantages of micro-frontends:
CODE enterprise-project/ โโโ apps/ # Micro-frontend sub-applications โ โโโ main/ # Base application โ โโโ product/ โ โโโ order/ โ โโโ user/ โโโ packages/ # Shared packages (Monorepo) โ โโโ ui-components/ # Shared component library โ โโโ utils/ # Utility functions โ โโโ constants/ # Constant definitions โ โโโ types/ # TypeScript types โโโ shared/ # Shared configuration โ โโโ eslint-config/ โ โโโ ts-config/ โ โโโ vite-config/ โโโ docs/ # Project documentation โโโ scripts/ # Build scripts โโโ package.json
Large applications need to focus on performance optimization:Large applications need to focus on performance optimization:
CODE Performance Optimization Strategy/ โโโ Build-time Optimization โ โโโ Code Splitting โ โโโ Route Lazy Loading โ โโโ Tree Shaking โ โโโ Asset Compression โโโ Runtime Optimization โ โโโ Virtual Scrolling (long lists) โ โโโ Image Lazy Loading โ โโโ On-demand Component Rendering โ โโโ Caching Strategy โโโ Network Optimization โโโ CDN Acceleration โโโ HTTP Caching โโโ Resource Preloading โโโ Service Worker
For scenarios requiring SEO or fast first-screen performance:For scenarios requiring SEO or fast first-screen performance:
| Approach | Suitable Scenarios | Representative Frameworks |
|---|---|---|
| SSR | Needs SEO, fast first-screen rendering | Next.js, Nuxt.js |
| SSG | Static content, infrequent updates | Astro, VitePress |
| Hybrid | Part static, part dynamic | Next.js (ISR) |
------
Characteristics: Fast iteration, limited resources, rapidly changing requirementsCharacteristics: Fast iteration, limited resources, rapidly changing requirements
Recommended Architecture:Recommended Architecture:
Directory Structure: Simple organization by typeDirectory Structure: Simple organization by type
Characteristics: Complex business, team collaboration, stability requiredCharacteristics: Complex business, team collaboration, stability required
Recommended Architecture:Recommended Architecture:
Directory Structure: Organize by feature, establish standardsDirectory Structure: Organize by feature, establish standards
Characteristics: High concurrency, multi-team collaboration, long-term maintenanceCharacteristics: High concurrency, multi-team collaboration, long-term maintenance
Recommended Architecture:Recommended Architecture:
Directory Structure: Monorepo + micro-frontendDirectory Structure: Monorepo + micro-frontend
------
CODE Stage 1: Personal Blog (HTML/CSS/JS) โ Need: Backend management needed Stage 2: Add Admin Panel (Vue/React + simple structure) โ Need: User system, comments feature Stage 3: Feature Modularization (organize by feature) โ Need: Multi-team collaboration, independent deployment Stage 4: Micro-frontend Architecture (Monorepo)
| Signal | Description | Suggestion |
|---|---|---|
| Build time > 5 minutes | Project too large | Code splitting, micro-frontend |
| Frequent merge conflicts | Collaboration difficulties | Organize by feature, module splitting |
| Fix one thing, break another | Severe coupling | Refactor, strengthen testing |
| First screen load > 3 seconds | Performance issues | Lazy loading, SSR, optimization |
| Slow onboarding for new members | Disorganized structure | Documentation, standards, refactoring |
------
There is no silver bullet for architecture โ what fits is best. - Small projects: Don't over-engineer, HTML/CSS/JS is sufficient - Medium projects: Establish standards, componentization, modularization - Large projects: Consider micro-frontends, performance optimization, team collaboration Remember these points: 1. Progressive evolution: Start simple, grow with needs 2. Unified conventions: Keep naming, structure, and code style consistent 3. Documentation first: Record architecture decisions for knowledge transfer 4. Regular refactoring: Pay off technical debt in a timely manner Ultimate goal: Make code like an organized space โ regardless of size, running efficiently.There is no silver bullet for architecture โ what fits is best. - Small projects: Don't over-engineer, HTML/CSS/JS is sufficient - Medium projects: Establish standards, componentization, modularization - Large projects: Consider micro-frontends, performance optimization, team collaboration Remember these points: 1. Progressive evolution: Start simple, grow with needs 2. Unified conventions: Keep naming, structure, and code style consistent 3. Documentation first: Record architecture decisions for knowledge transfer 4. Regular refactoring: Pay off technical debt in a timely manner Ultimate goal: Make code like an organized space โ regardless of size, running efficiently.
------