⊗tlWpHtSB 43 of 55 menu

Bundles of styles in a layout in Webpack

Now let's make Webpack automatically include the CSS bundle into the HTML layout of the site.

Let's import two CSS files to the entry point:

import './styles1.css'; import './styles2.css';

Let's set up the assembly of these files into one common bundle. First, let's perform all the necessary imports:

import path from 'path'; import MiniCssExtractPlugin from "mini-css-extract-plugin"; import HtmlWebpackPlugin from 'html-webpack-plugin';

Now let's make the settings:

export default { context: path.resolve( 'src'), entry: './index.js', output: { filename: 'build.[contenthash].js', path: path.resolve('dist'), }, plugins: [ new MiniCssExtractPlugin({ filename: 'build.[contenthash].css' }), new HtmlWebpackPlugin(), ], module: { rules: [ { test: /\.css$/i, use: [MiniCssExtractPlugin.loader, 'css-loader'], }, ], }, };

As a result, after the build we will have a bundle with scripts and a bundle with styles connected:

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Webpack App</title> <script defer src="build.4173b379c6d6ff439604.js"></script> <link href="build.97299e5ee87c9c343a4c.css" rel="stylesheet"> </head> <body> </body> </html>

Include three CSS files to the entry point. Make them gather into a common bundle. Check that this bundle is automatically included in the layout file.

enru