CSS and HTML Are Built In

Since webpack 5.109.0, CSS and HTML work out of the box. Import a stylesheet or use an HTML page as your entry point, and webpack handles the rest. Fewer dependencies, less configuration.

CSS with no configuration

Import a stylesheet and webpack does the rest:

import "./styles.css";
import * as button from "./button.module.css";

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. No module.rules, no plugin.

HTML pages with no plugin

Reference your styles and scripts in the page:

src/index.html

<link rel="stylesheet" href="./styles.css" />
<script type="module" src="./index.js"></script>

Then use that page as the entry:

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. For a project with a JavaScript entry, output.html: true generates the page and links its scripts and stylesheets. The Native HTML guide covers titles, favicons, Subresource Integrity, CSP and the rest.

What you can remove

These packages are deprecated in favor of the built-in support:

PackageReplaced by
css-loaderBuilt-in CSS parsing, @import / url() resolution and CSS Modules
style-loaderexportType: "style"
mini-css-extract-pluginBuilt-in extraction of a .css file per chunk, by default
css-minimizer-webpack-pluginBuilt-in CSS minification and vendor prefixes (5.110.0+)
html-loaderImporting .html from JavaScript
html-webpack-pluginAn HTML entry or output.html
html-minimizer-webpack-pluginBuilt-in HTML minification (5.110.0+)

Preprocessors and template engines such as sass-loader, less-loader, postcss-loader and Pug keep working with the built-in support.

Sass, Less, PostCSS and Tailwind

Keep the preprocessor's loader and drop the CSS loaders behind it. The rule's type hands its output to webpack's CSS pipeline:

 {
   test: /\.s[ac]ss$/i,
-  use: ["style-loader", "css-loader", "sass-loader"],
+  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 .css files, and the few cases where experiments.css: true is needed, see Preprocessors, PostCSS and Tailwind.

Existing projects keep working

Existing css-loader and html-loader setups keep working. By default, webpack detects their rules and keeps the corresponding built-in support off, so you can migrate when you're ready.

Migrate an existing project

For CSS, run the @webpack/css-plugins-to-native-css codemod:

npx codemod @webpack/css-plugins-to-native-css

For HTML, run the @webpack/html-plugins-to-native-html codemod (webpack 5.112.0 or later):

npx codemod @webpack/html-plugins-to-native-html

For manual migration, follow the Native CSS and Native HTML guides. New projects scaffolded with npx create-webpack-app already use the built-in support.

Edit this page·
« Previous
Blog

2 Contributors

alexander-akaitbjohansebas