For most of webpack’s history, styling a project meant installing a small stack of packages: css-loader, style-loader or mini-css-extract-plugin, a CSS minimizer, and html-webpack-plugin for the page itself. That’s changing. Since webpack 5.109.0, CSS and HTML work out of the box, and webpack 5.113, released on 19 November 2026, goes a step further with CSS Modules directly inside HTML pages, built-in SVG optimisation and a new way to skip loaders for files that don’t need them.
This tutorial shows how to build a small site with zero CSS/HTML loaders, use the new <style module> feature, and migrate an existing project. Sources: the webpack blog posts CSS and HTML Are Built In and Webpack 5.113.
Install or upgrade
npm install --save-dev [email protected] webpack-cli
CSS with no configuration
Import a stylesheet from JavaScript and webpack handles it. No module.rules, no plugin:
// src/index.js
import "./styles.css";
import * as button from "./button.module.css";
const btn = document.createElement("button");
btn.className = button.primary;
btn.textContent = "Save";
document.body.append(btn);
According to the blog, webpack resolves @import and url(), scopes *.module.css files as CSS Modules, and extracts a .css file for each chunk. In mode: "production" (webpack 5.110.0 or later) it minifies the CSS, and with a browserslist target it also adds the vendor prefixes those browsers need.
HTML pages as entry points
You can now point webpack at an HTML file directly:
<!-- src/index.html -->
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Webpack native HTML</title>
<link rel="stylesheet" href="./styles.css" />
<script type="module" src="./index.js"></script>
</head>
<body>
<h1>Hello</h1>
<img src="./logo.png" alt="Logo" />
</body>
</html>
// webpack.config.js
export default {
entry: "./src/index.html",
};
Webpack bundles the scripts, stylesheets and images the page references and rewrites their URLs to the emitted files. If your project has a JavaScript entry instead, output.html: true generates the page and links its scripts and stylesheets. The Native HTML guide covers titles, favicons, Subresource Integrity and CSP.
New in 5.113: CSS Modules inside HTML
CSS Modules stop class names from colliding, but until now your markup needed to know the generated names, which meant rendering HTML from JavaScript. Webpack 5.113 connects the two for native HTML pages. Add module to a <style> tag and keep writing the original class names:
<style module>
.button {
color: white;
background: navy;
}
</style>
<button class="button">Save</button>
Webpack gives .button a scoped name and writes that same name into the button’s class attribute. Linked .module.css stylesheets work too, and classes that no CSS Module declares stay unchanged.
Two modules with the same class
When two modules both declare .button, name the module and reference it with a dot:
<style module="card">
.button { background: navy; }
</style>
<button class="card.button">Save card</button>
Things that are not rewritten
Scoped IDs stay connected to labels, ARIA references and in-page links. But selectors written inside JavaScript, such as document.querySelector(".button"), are not rewritten. The blog recommends using a separate attribute for JavaScript hooks:
<button class="button" data-action="save">Save</button>
<script type="module">
document.querySelector('[data-action="save"]')
.addEventListener("click", () => console.log("saved"));
</script>
This is good practice anyway: styling classes and behaviour hooks change for different reasons.
Sass, Less, PostCSS and Tailwind
Preprocessors keep working. Keep the preprocessor’s loader, drop the CSS loaders behind it, and set the rule’s type so its output goes into webpack’s CSS pipeline:
// webpack.config.js
export default {
module: {
rules: [
{
test: /\.s[ac]ss$/i,
use: ["sass-loader"],
type: "css/auto",
},
],
},
};
One rule covers both layout.scss (global) and button.module.scss (a CSS Module). For PostCSS and Tailwind on plain .css files, see the Native CSS guide, which also covers the few cases where experiments.css: true is still needed.
Other 5.113 features worth knowing
Smaller SVG assets
SVG optimisation for emitted .svg and .xml assets is opt-in (unless you use experiments.futureDefaults):
export default {
mode: "production",
optimization: {
minimizeOptions: {
xml: {},
},
},
};
With xml: {}, webpack shortens numbers, colours and path data, removes unused namespace declarations and trims whitespace. More aggressive options such as removeMetadata: true, precision and cleanupIds: true stay off by default because metadata can contain licence information, rounding can move points, and IDs may be referenced from other files.
Run loaders only when needed
The new sourceTest condition lets a loader check its input before running:
export default {
module: {
rules: [
{
test: /\.js$/i,
use: [
{
loader: "./loaders/instrument-loader.js",
sourceTest: /@instrument/,
},
],
},
],
},
};
Only files containing @instrument run through that loader’s normal function. Pitch functions still run.
Also in this release
- Better CommonJS tree shaking. A simple
exports.answer = 42no longer makes an otherwise unused module look like it has side effects. - More reliable hot updates when modules move between chunks.
output.allowOutsidecontrols whether webpack may write, copy and clean files outsideoutput.path.- Built-in TypeScript parsing now parses files once, without a separate type-stripping step.
Migrating an existing project
Existing css-loader and html-loader setups keep working. By default webpack detects those rules and leaves the built-in support off, so nothing breaks when you upgrade. When you’re ready, use the official codemods:
# CSS: replace css-loader/style-loader/mini-css-extract-plugin setups
npx codemod @webpack/css-plugins-to-native-css
# HTML: replace html-webpack-plugin/html-loader (webpack 5.112.0+)
npx codemod @webpack/html-plugins-to-native-html
The blog lists these packages as deprecated in favour of built-in support:
| Package | Replaced by |
|---|---|
css-loader |
Built-in CSS parsing, @import/url() and CSS Modules |
style-loader |
exportType: "style" |
mini-css-extract-plugin |
Built-in .css extraction per chunk |
css-minimizer-webpack-plugin |
Built-in CSS minification and prefixes (5.110.0+) |
html-loader |
Importing .html from JavaScript |
html-webpack-plugin |
An HTML entry or output.html |
html-minimizer-webpack-plugin |
Built-in HTML minification (5.110.0+) |
A safe migration order: upgrade webpack first and confirm the build is unchanged, run the CSS codemod and compare the output CSS, then run the HTML codemod and test the generated pages (especially CSP and SRI if you use them).
Key takeaways
- Webpack 5.109+ handles CSS and HTML natively; 5.113 adds
<style module>CSS Modules inside HTML pages. - New projects can drop five or more build dependencies.
- Old loader setups keep working, so you can migrate gradually with the official codemods.
- Use
data-*attributes, not scoped class names, for JavaScript selectors.
