Set up Master CSS in Laravel
Guide to setting up Master CSS in your Laravel project.
Master CSS Static Extraction integrates build tools to scan project source code, extract class names, generate CSS rules, and write them into a virtual CSS module.
Zero runtime
Generate virtual CSS modules at build time
Semi-dynamic
Scan all source code to extract class names
Cross-page caching
Download the CSS bundle for all pages at once
Quick start
Clone the example
Copy-paste the following commands to quickly start using the example.
You can skip all installation steps.
npm create @master/css@rc project --example laravel-with-static-extractioncd project
Launch server
Run Laravel and Vite development servers by splitting two terminals.
php artisan serve
Installation
Create a project
If you don't have a Laravel project, create one first. It's recommended to refer to Installation - Laravel
composer create-project laravel/laravel example-app
Initialize configuration file
Run npm create @master/css@rc
to create a configuration file master.css.js.
npm create @master/css@rc
Install Master CSS
Install Master CSS Extractor into your project via package managers.
npm install @master/css-extractor.vite@rc
Set up CSS extractor
Add a CSSExtractorPlugin
to the vite.config.js
and include view sources.
import { defineConfig } from 'vite'import laravel from 'laravel-vite-plugin'import CSSExtractorPlugin from '@master/css-extractor.vite' export default defineConfig({ plugins: [ laravel({ input: ['resources/css/app.css', 'resources/js/app.js'], refresh: true, }), CSSExtractorPlugin({ sources: ['resources/views/**/*.php'] }) ],})
Import virtual CSS module
Import the virtual CSS module .virtual/master.css
into the resources/js/app.js
.
import './bootstrap'import '.virtual/master.css'
Set up your view layout
Import the entry by adding @vite('resources/js/app.js')
in your resources/views/layouts/welcome.blade.php
<!DOCTYPE html><html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> <head> … @vite('resources/js/app.js') </head> …</html>
Launch server
Run Laravel and Vite development servers by splitting two terminals.
php artisan serve
Hello world!
Now style your first element using Master CSS syntax!
…<body> <h1 class="font:40 font:heavy italic m:12x text:center"> Hello World </h1></body>…
Open your browser to watch the changes.