Build Tool: Vite
Vite is a modern build tool that offers:- Lightning-fast Hot Module Replacement (HMR) during development
- Optimized production builds using Rollup
- Native ES modules support
- Built-in support for React, TypeScript, CSS modules, and more
- Zero-config setup with sensible defaults
Vite Configuration
The project’s build configuration is defined invite.config.js:
vite.config.js
Configuration Options
Array of Vite plugins. The
@vitejs/plugin-react plugin enables React Fast Refresh and JSX transformation.The base public path when served in production. Set to
/github-dashboard/ to match the GitHub Pages subdirectory. Must match the repository name when deploying to GitHub Pages.The
base option is critical for GitHub Pages deployments. It ensures all asset paths (JS, CSS, images) are correctly resolved when the app is hosted in a subdirectory.Build Scripts
Thepackage.json defines several build-related scripts:
package.json
Script Descriptions
npm run dev
npm run dev
Starts the Vite development server with Hot Module Replacement (HMR).
- Runs on
http://localhost:5173by default - Provides instant feedback on code changes
- Shows detailed error messages in the browser
- Supports source maps for debugging
npm run build
npm run build
Creates an optimized production build in the Build process:
dist directory.- Cleans the
distdirectory - Transforms and bundles all source files
- Minifies JavaScript and CSS
- Optimizes assets (images, fonts, etc.)
- Generates hashed filenames for cache busting
- Creates source maps (optional)
npm run preview
npm run preview
Previews the production build locally before deployment.
- Serves the
distdirectory onhttp://localhost:4173 - Simulates production environment
- Useful for testing before deploying
- Does not include HMR or development features
npm run deploy
npm run deploy
Builds the project and deploys to GitHub Pages.This command:
- Runs
npm run buildto create production assets - Uses
gh-pages -d distto publish thedistfolder - Pushes to the
gh-pagesbranch automatically
Build Output Structure
The production build generates the following structure in thedist directory:
Output Characteristics
The main HTML entry point with:
- Minified HTML
- Inline critical CSS (optional)
- Preload hints for faster loading
- Hashed asset references for cache busting
Optimized JavaScript bundles:
- Tree-shaken to remove unused code
- Minified with Terser
- Code-split by route or component (when configured)
- Hashed filenames for long-term caching
Compiled CSS with:
- CSS Modules transformed to scoped class names
- Minified and compressed
- Unused styles removed
- Hashed filenames
Build Optimizations
Vite applies several optimizations automatically:Code Splitting
- Vendor dependencies bundled separately
- Dynamic imports create separate chunks
- Reduces initial load time
Tree Shaking
- Removes unused code from final bundle
- Only includes imported functions and components
- Significantly reduces bundle size
Minification
- JavaScript minified with esbuild (fast) or Terser (smaller)
- CSS minified with Lightning CSS
- HTML whitespace removed
Asset Optimization
- Images can be optimized with plugins
- SVGs are inlined when small enough
- Fonts are subset when possible
Cache Busting
- All assets get content-based hashes in filenames
index-a1b2c3d4.jschanges only when content changes- Enables long-term caching with
Cache-Control: max-age=31536000
Build Performance
Typical build times for GitScope:Build Analysis
To analyze your bundle size and dependencies:Environment-Specific Builds
Vite uses environment variables prefixed withVITE_ that are exposed to your client code:
src/config.js
Troubleshooting Build Issues
Build Fails with Memory Error
package.json
Assets Not Loading in Production
Verify thebase path in vite.config.js matches your deployment subdirectory.
CSS Modules Not Working
Ensure files use the.module.css extension:
Header.module.css✓Header.css✗
Large Bundle Size
- Use
rollup-plugin-visualizerto identify large dependencies - Consider code splitting with dynamic imports
- Remove unused dependencies from
package.json - Use lighter alternatives for heavy libraries
Next Steps
GitHub Pages Deployment
Deploy your built application to GitHub Pages
Environment Setup
Configure your local development environment