Set up Master CSS in Angular
Guide to setting up Master CSS in your Angular project.
Master CSS Progressive Rendering scans the rendered HTML ahead of time, server-side or at build time, generates the corresponding CSS rules for each page, and lazy loads the runtime engine to keep track of the dynamic class names.
Faster page loading
Non-rendering-blocking internal CSS and lazy loading
Fully automatic
Capture any program-generated class names
CSS encapsulation
Only ship the page-used CSS instead of the whole site
Quick start
Clone the example
Copy-paste the following commands to quickly start using the example.
You can skip all installation steps.
npx degit master-co/css/examples/angular-with-progressive-rendering my-projectcd my-projectnpm install @master/css@beta @master/css-server@betanpm installnpm run dev
Installation
Create a project
If you don't have a Angular project, create one first. It's recommended to refer Angular CLI.
ng new projectcd project
Install Master CSS
Install Master CSS Server into your project via package managers.
npm install @master/css@beta @master/css-server@beta
Initialize configuration file
Run npx mcss init
to create a configuration file master.css.ts.
npx mcss init
Set up CSS runtime engine
Initialize the runtime engine with your configuration.
There is currently no solution for lazy loading runtime engine in Angular. Wait for Angular to officially release a major version based on Vite.
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';import { AppModule } from './app/app.module';import { initRuntime } from '@master/css';import config from '../master.css'; initRuntime(config); platformBrowserDynamic().bootstrapModule(AppModule) .catch(err => console.error(err));
Set up Angular Universal
Run the command to create a Angular server-side application.
npx ng add @nguniversal/express-engine
Set up CSS pre-rendering
All CSS rules are pre-rendered and injected into HTML on the server side or at build time.
import { render } from '@master/css-server';import config from './master.css';…export function app() { … server.get('*', (req, res) => { … res.render(indexHtml, { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] }); res.render(indexHtml, { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] }, (error: Error, html: string) => { error ? console.error(error) : res.send(render(html, config).html) } ) });}
Launch server
Run the development server.
npm run dev:ssr
Start using Master CSS
Now style your first element using Master CSS syntax!
<h1 class="font:40 font:heavy italic m:50 text:center">Hello World</h1>
Open your browser to watch the changes.