Skip to content

Commit 1bfc2d1

Browse files
committed
First draft of new asset chapter
1 parent 1ddffe4 commit 1bfc2d1

13 files changed

Lines changed: 2406 additions & 14 deletions

File tree

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
title: Introduction
3+
description: Overview of asset management in UserFrosting 6, including the transition from Webpack Encore to Vite.
4+
---
5+
6+
UserFrosting uses a modern asset bundling system to compile and manage frontend assets like JavaScript, TypeScript, Vue components, and stylesheets. Think of it as a smart assistant that transforms your development files into optimized, production-ready resources that browsers love.
7+
8+
Starting with UserFrosting 6, the framework has transitioned from Webpack Encore to **Vite** as its default and recommended asset bundler. Don't worry if you're new to build tools—this chapter will guide you through everything you need to know!
9+
10+
## Why Vite?
11+
12+
UserFrosting 6 adopts [Vite](https://vitejs.dev) as its primary asset bundler. If you've worked with build tools before, you'll immediately notice the difference. If this is your first time with asset bundling, you're starting with one of the best tools available!
13+
14+
Here's what makes Vite special:
15+
16+
- **Lightning-fast dev server**: Vite uses native ES modules during development. What does this mean for you? Your development server starts instantly—no more waiting for bundling—and changes appear in your browser almost immediately thanks to Hot Module Replacement (HMR)
17+
- **Optimized builds**: For production, Vite uses Rollup to create highly optimized bundles with automatic code splitting (breaking your code into smaller chunks) and tree-shaking (removing unused code)
18+
- **First-class TypeScript support**: Write TypeScript without any additional configuration—Vite handles it automatically
19+
- **Modern by default**: Built for modern browsers while still supporting older ones when needed
20+
- **Simpler configuration**: Compared to Webpack, Vite's configuration is more intuitive and requires less boilerplate code
21+
- **Better Vue 3 support**: Vite's official Vue plugin provides optimized handling of Single File Components (`.vue` files)
22+
23+
While Vite is the recommended bundler, UserFrosting 6 maintains backward compatibility with [Webpack Encore](/asset-management/webpack-encore) for existing projects that require it. You're not forced to migrate immediately—you can take your time!
24+
25+
## Asset Workflow Overview
26+
27+
Let's walk through how assets flow from your development files to your user's browser. Understanding this workflow will help you troubleshoot issues and optimize your application:
28+
29+
1. **Source files** are created in `app/assets/`:
30+
- TypeScript/JavaScript files (`main.ts`, components, etc.)
31+
- Vue 3 Single File Components (`.vue` files)
32+
- Stylesheets (LESS, Sass, CSS)
33+
- Static assets (images, fonts, etc.)
34+
35+
2. **Vite processes** these files differently based on your environment:
36+
- **Development mode**: Serves files directly with HMR for instant updates—no waiting for builds!
37+
- **Production mode**: Bundles, minifies, and optimizes everything for the fastest possible load times
38+
39+
3. **Compiled assets** are output to `public/assets/`:
40+
- JavaScript bundles with automatic code splitting (your app is broken into smaller chunks that load on-demand)
41+
- Extracted CSS files
42+
- Manifest file (`.vite/manifest.json`) that maps your source files to the compiled versions
43+
- Copied static assets
44+
45+
4. **Twig templates** reference assets using helper functions:
46+
- `{{ vite_js('main.ts') }}` - Load JavaScript entry points
47+
- `{{ vite_css('main.ts') }}` - Load CSS from entry points
48+
- `{{ vite_preload('component.ts') }}` - Preload modules for better performance
49+
50+
## Development vs Production
51+
52+
Vite behaves differently depending on whether you're developing your application or deploying it to production. This dual approach gives you the best of both worlds: speed during development and optimization for your users.
53+
54+
**Development Mode** (`assets.vite.dev = true`):
55+
- Vite development server runs on port 5173 (configurable)
56+
- Assets are served directly from memory—no build step required, changes are instant!
57+
- Hot Module Replacement enables updates without page refresh (your form data stays intact while code updates)
58+
- Source maps and detailed error messages help you debug issues quickly
59+
- Twig functions point to the Vite dev server
60+
61+
**Production Mode** (`assets.vite.dev = false`):
62+
- Assets are pre-built and optimized for the fastest possible load times
63+
- Files are served as static assets from `public/assets/`
64+
- Minification and tree-shaking reduce file sizes significantly
65+
- Cache-busting via hashed filenames ensures users always get the latest version
66+
- Twig functions point to the manifest-mapped filenames
67+
68+
You'll spend most of your time in development mode, but understanding production mode is important for deployment and troubleshooting.
69+
70+
## What's Next?
71+
72+
This chapter will guide you through:
73+
74+
- **Getting Started**: Setting up Vite configuration and understanding the project structure
75+
- **Bakery Commands**: Using CLI commands to build and serve assets
76+
- **Using Assets**: Integrating assets into your Twig templates
77+
- **Advanced Usage**: TypeScript, Vue 3, preprocessors, and optimization techniques
78+
- **Sprinkle Assets**: Managing assets across multiple sprinkles
79+
- **Migration**: Transitioning from Webpack Encore to Vite
80+
81+
Ready to get started? Continue to the [Getting Started](/asset-management/getting-started) guide.

0 commit comments

Comments
 (0)