Set up Master CSS in Angular
Guide to setting up Master CSS in your Angular 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
Create a project
If you don't have a Angular project, create one first. It's recommended to refer Angular CLI.
ng new my-appcd my-app
Create a configuration file
Run the command to create a master.css.js file.
npm create @master/css@rc
Install Master CSS and others
Install the Master CSS and Angular custom webpack plugin into your project via package managers.
npm i @master/css.webpack@rc @angular-builders/custom-webpack
Set up CSS extractor
- Create a
webpack.config.js
file - Add a
MasterCSSExtractorPlugin
to the plugins
const { MasterCSSExtractorPlugin } = require('@master/css.webpack') module.exports = (config) => { config.plugins.push( new MasterCSSExtractorPlugin({ sources: ['src/index.html'] }) ) return config }
Configure Angular
Use @angular-builders/custom-webpack
instead of the default extractor for custom webpack configuration.
{ …, "projects": { "my-app": { …, "architect": { "build": { "extractor": "@angular-devkit/build-angular:browser", "extractor": "@angular-builders/custom-webpack:browser", "options": { "customWebpackConfig": { "path": "./webpack.config.js" }, … }, … }, "serve": { "extractor": "@angular-devkit/build-angular:dev-server", "extractor": "@angular-builders/custom-webpack:dev-server", "options": { "browserTarget": "my-app:build" }, … }, … } }, … }}
Import virtual CSS module
Import the master.css
virtual CSS module into the global CSS file src/styles.css
.
Don't import it in the src/main.ts
, you'll get some errors about webpack loaders.
/* You can add global styles to this file, and also import other style files */@import 'virtual:master.css';
Launch server
Run the development server.
npm run start
Start using Master CSS
Now style your first element using Master CSS syntax!
<h1 class="italic m:12x fg:strong font:40 font:heavy">Hello World</h1>
Open your browser to watch the changes.