Set up Master CSS in Webpack
Guide to setting up Master CSS in your Webpack project.
Master CSS Runtime Rendering observes changes in DOM class names at browser runtime, generates corresponding CSS rules, and injects them into the running style sheet.
Fixed style cost
All features work out of the box with ~16KB transfer cost
Fully automatic
Capture any program-generated class names
CSS lifecycle
CSS is generated on demand and frees memory when not in use
Quick start
Clone the example
Copy-paste the following commands to quickly start using the webpack.rr.beta.css.master.co.
You can skip all installation steps.
npx degit master-co/css/examples/webpack-with-runtime-rendering my-projectcd my-projectnpm install @master/css@betanpm installnpm run dev
Installation
Create a project
If you don't have a Webpack project, create one first. It's recommended to refer to Getting Started - Webpack
mkdir projectcd projectnpm init -ynpm install webpack webpack-cli webpack-dev-server html-webpack-plugin --save-dev
Install Master CSS
Install Master CSS into your project via package managers.
npm install @master/css@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') module.exports = { entry: './src/index.js', output: { filename: 'bundle.js', path: path.join(__dirname, 'dist') }, plugins: [ new HtmlWebpackPlugin({ template: path.join(__dirname, '') }) ], devServer: { watchFiles: ['src/**/*'] }}
Create a HTML file
Create and configure the src/index.html
file.
<!doctype html><html lang="en" style="display: none"><head> <meta charset="utf-8" /> <script src="./index.js"></script></head><body> <h1>Hello World</h1></body></html>
Set up CSS runtime engine
Create src/index.js
file and import Master CSS into it.
import { initRuntime } from '@master/css';import config from '../master.css.js'; initRuntime(config);
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" style="display: none"><head> <meta charset="utf-8" /> <script src="./index.js"></script></head><body> <h1 class="font:40 font:heavy italic m:50 text:center">Hello World</h1></body></html>
Open your browser to watch the changes.