Set up Master CSS in Svelte
Guide to setting up Master CSS in your Svelte 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 svelte.se.beta.css.master.co.
You can skip all installation steps.
npx degit master-co/css/examples/svelte-with-static-extraction my-projectcd my-projectnpm install @master/css-extractor.vite@betanpm installnpm run dev -- --open
Installation
Create a project
If you don't have a Svelte project, create one first. It's recommended to refer to Svelte Kit.
npm create svelte@latest projectcd projectnpm install
Install Master CSS
Install the Master CSS extractor into your project via package managers.
npm install @master/css-extractor.vite@beta
Initialize configuration file
Run npx mcss init
to create a configuration file master.css.ts.
npx mcss init
Set up CSS extractor
- Create a
vite.config.ts
file - Add a
CSSExtractorPlugin
to the viteplugins
import { defineConfig } from 'vite'import { CSSExtractorPlugin } from '@master/css-extractor.vite' export default defineConfig({ plugins: [ CSSExtractorPlugin() ]})
Import virtual CSS module
Import the virtual CSS module .virtual/master.css
into the src/routes/+layout.svelte
.
<script> import Header from './Header.svelte'; import './styles.css'; import '.virtual/master.css';</script>…
Launch server
Run npm run dev -- --open
to start your Vite development server
npm run dev -- --open
Start using Master CSS
Now style your first element using Master CSS syntax!
<h1 class="font:40 fg:blue font:heavy italic m:50 text:center">Hello World</h1>
Open your browser to watch the changes.