# Build Tools & Configuration Concept uses Vite as its build tool, providing lightning-fast development and optimized production builds. This guide covers everything you need to know about the build system. ## Why Vite? Vite offers several advantages over traditional bundlers: - ⚡ **Instant Server Start** - No bundling required in development - 🔥 **Lightning Fast HMR** - Hot Module Replacement in milliseconds - 📦 **Optimized Builds** - Rollup-based production builds - 🎯 **Native ESM** - Leverages browser's native ES modules - 🔧 **Zero Config** - Works out of the box with sensible defaults ## Vite Configuration The `vite.config.js` file controls how Vite builds your project: ```javascript import { defineConfig } from 'vite'; import { resolve } from 'path'; import handlebars from 'vite-plugin-handlebars'; import { viteStaticCopy } from 'vite-plugin-static-copy'; export default defineConfig({ root: 'src', base: '/', build: { outDir: '../dist', emptyOutDir: true, rollupOptions: { input: { main: resolve(__dirname, 'src/index.html'), // Add all your pages here login: resolve(__dirname, 'src/pages/auth/login.html'), products: resolve(__dirname, 'src/pages/ecommerce/products.html'), // ... more pages } } }, plugins: [ handlebars({ partialDirectory: resolve(__dirname, 'src/partials'), context: { title: 'Concept Dashboard' } }), viteStaticCopy({ targets: [ { src: 'assets/images/*', dest: 'assets/images' } ] }) ], server: { port: 3000, hot: true, open: true } }); ``` ## Key Configuration Options ### Root Directory ```javascript root: 'src' ``` Sets the project root to the `src` directory. ### Build Output ```javascript build: { outDir: '../dist', emptyOutDir: true } ``` - `outDir`: Where production files are generated - `emptyOutDir`: Cleans the output directory before building ### Multiple Entry Points ```javascript rollupOptions: { input: { main: '/index.html', dashboard: '/pages/dashboard.html', login: '/pages/auth/login.html' } } ``` Each entry creates a separate HTML file in the build. ### Development Server ```javascript server: { port: 3000, hot: true, open: true, host: 'localhost' } ``` ## NPM Scripts The `package.json` defines useful scripts: ```json { "scripts": { "dev": "vite", "build": "vite build", "preview": "vite preview", "clean": "rm -rf dist", "build:analyze": "vite build --analyze", "build:watch": "vite build --watch" } } ``` ### Script Explanations - **`npm run dev`** - Start development server - **`npm run build`** - Create production build - **`npm run preview`** - Preview production build locally - **`npm run clean`** - Remove dist directory - **`npm run build:analyze`** - Analyze bundle size - **`npm run build:watch`** - Rebuild on file changes ## Development Workflow ### 1. Starting Development ```bash npm run dev ``` This starts the Vite dev server with: - Hot Module Replacement (HMR) - Fast refresh for styles - Error overlay - Network access for mobile testing ### 2. Making Changes When you modify files: - **HTML/Handlebars** - Page refreshes automatically - **SCSS** - Styles update without refresh - **JavaScript** - Modules hot-reload - **Assets** - Changes reflect immediately ### 3. Adding New Pages To add a new page: 1. Create the HTML file: ```html {{> layouts/main title="New Feature"}} {{#*inline "content"}} {{/inline}} {{> layouts/base}} ``` 2. Add to `vite.config.js`: ```javascript input: { 'new-feature': resolve(__dirname, 'src/pages/features/new-feature.html') } ``` 3. The page is now accessible at `/pages/features/new-feature.html` ## Production Build ### Building for Production ```bash npm run build ``` This command: 1. Cleans the output directory 2. Compiles SCSS to CSS 3. Transpiles JavaScript 4. Optimizes assets 5. Generates HTML files 6. Creates source maps ### Build Optimizations Vite automatically applies these optimizations: - **Code Splitting** - Shared code in separate chunks - **Tree Shaking** - Removes unused code - **Minification** - Compressed HTML, CSS, JS - **Asset Optimization** - Compressed images - **CSS Extraction** - Separate CSS files - **Chunk Hashing** - Cache busting ### Build Output ``` dist/ ├── assets/ │ ├── index-[hash].css # Main styles │ ├── index-[hash].js # Main JavaScript │ └── vendor-[hash].js # Vendor libraries ├── pages/ │ └── [all HTML pages] ├── index.html └── favicon.ico ``` ## Environment Variables ### Using Environment Variables Create a `.env` file: ```env VITE_APP_NAME=My Dashboard VITE_API_URL=https://api.example.com VITE_VERSION=1.0.0 ``` Access in JavaScript: ```javascript console.log(import.meta.env.VITE_APP_NAME); console.log(import.meta.env.VITE_API_URL); ``` Access in HTML/Handlebars: ```html