Set up Master CSS in Webpack

Guide to setting up Master CSS in your Webpack 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


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

You can skip all installation steps.

npx degit master-co/css/examples/webpack-with-static-extraction my-project
cd my-project
npm install @master/css@beta
npm install
npm run dev


Create a project

If you don't have a Webpack project, create one first. It's recommended to refer to Getting Started - Webpack

mkdir project
cd project
npm init -y
npm install webpack webpack-cli webpack-dev-server html-webpack-plugin css-loader style-loader ts-loader --save-dev

Install Master CSS Extractor

Install the Master CSS extractor into your project via package managers.

npm install @master/css-extractor.webpack@beta

Initialize configuration file

Run npx mcss init to create a configuration file master.css.ts.

npx mcss init

Create a Webpack config file

Create and configure the webpack.config.js file.

const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const { CSSExtractorPlugin } = require('@master/css-extractor.webpack')
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.join(__dirname, 'dist')
plugins: [
new HtmlWebpackPlugin({
template: path.join(__dirname, 'src/index.html')
new CSSExtractorPlugin({ sources: ['./src/index.html'] })
devServer: {
watchFiles: ['src/**/*']
module: {
rules: [
{ test: /\.css$/, use: ['style-loader', 'css-loader'] }

Configure a HTML file

Create and configure the src/index.html file.

<!doctype html>
<html lang="en">
<meta charset="utf-8" />
<h1>Hello World</h1>

Import virtual CSS module

Import the virtual CSS module .virtual/master.css into the src/index.js.

import '.virtual/master.css';

Add scripts to package.json

Add start and build scripts to standardize the development process.

"scripts": {
"dev": "webpack serve --mode development",
"build": "webpack --mode production"

Launch server

Run the development server.

npm run dev

Hello world!

Now style your first element using Master CSS syntax!

<!doctype html>
<html lang="en">
<meta charset="utf-8" />
<script src="./index.js"></script>
<h1 class="font:40 font:heavy italic m:50 text:center">Hello World</h1>

Open your browser to watch the changes.


Hello World

MIT License © Aoyue Design LLC.