As a well – established loader provider in the web development industry, I’m often approached by developers and webmasters regarding the effective use of various loaders in webpack. One of the frequently asked questions is about the mini – css – extract – plugin loader. In this blog post, I’ll share my in – depth knowledge and experiences on how to use the mini – css – extract – plugin loader in webpack, which will hopefully help you optimize your web projects. Loader

Understanding the Mini – CSS – Extract – Plugin Loader
Before diving into the usage details, it’s essential to understand what the mini – css – extract – plugin loader is and why it’s important. The mini – css – extract – plugin is a webpack plugin that extracts CSS into separate files. It creates a CSS file per JS file which contains CSS. This is a significant improvement over the previous approach of injecting CSS into the DOM using style – loader, especially for production builds.
The loader part of it is responsible for handling the CSS files and preparing them to be extracted by the plugin. It allows webpack to recognize and process CSS files as part of the build process.
Prerequisites
To use the mini – css – extract – plugin loader, you first need to have a basic webpack project set up. This involves having Node.js and npm (Node Package Manager) installed on your system. You’ll also need to have webpack and webpack – cli installed in your project directory. You can install them using the following commands:
npm init -y
npm install webpack webpack - cli --save - dev
Once you have the basic webpack setup, you can install the mini – css – extract – plugin and its associated loader:
npm install mini - css - extract - plugin --save - dev
Configuration in Webpack
The next step is to configure the mini – css – extract – plugin loader in your webpack configuration file (usually named webpack.config.js).
Here’s a basic example of a webpack configuration with the mini – css – extract – plugin:
const MiniCssExtractPlugin = require('mini - css - extract - plugin');
module.exports = {
entry: './src/index.js',
output: {
path: __dirname + '/dist',
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
'css - loader'
]
}
]
},
plugins: [
new MiniCssExtractPlugin({
filename: '[name].css',
chunkFilename: '[id].css'
})
]
};
In this configuration:
- Entry: Specifies the entry point of your application. In this case, it’s
./src/index.js. - Output: Defines where the bundled files will be saved. The JavaScript bundle will be saved as
bundle.jsin thedistdirectory. - Module Rules: Here, we define a rule for handling CSS files. The
testproperty uses a regular expression to match all files with the.cssextension. Theuseproperty is an array that specifies the loaders to be used. We first use theMiniCssExtractPlugin.loaderto extract the CSS into separate files, and then thecss - loaderto interpret@importandurl()likeimport/require()and resolve them. - Plugins: We initialize the
MiniCssExtractPluginwith some options. Thefilenameoption determines the name of the output CSS file, and thechunkFilenameoption is for the name of the CSS file for dynamic imports.
Advanced Configuration
Handling Multiple CSS Sources
In real – world projects, you may have multiple CSS sources, such as third – party libraries and your own custom stylesheets. You can handle them in different ways.
For example, if you want to apply different configurations to different CSS sources, you can use multiple rules in the module.rules section:
module.exports = {
//... other configuration
module: {
rules: [
{
test: /\.css$/,
exclude: /node_modules/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css - loader',
options: {
importLoaders: 1,
modules: true
}
},
'postcss - loader'
]
},
{
test: /\.css$/,
include: /node_modules/,
use: [
MiniCssExtractPlugin.loader,
'css - loader'
]
}
]
},
//... other configuration
};
In this setup, we have two rules for CSS files. The first rule excludes CSS files from the node_modules directory and applies css - loader with module support and postcss - loader. The second rule includes only CSS files from the node_modules directory and simply uses the basic css - loader.
Combining with Other Loaders
The mini – css – extract – plugin loader can be combined with other loaders to achieve more complex functionality. For instance, if you want to use Sass in your project, you can add the sass - loader to the loader chain:
module.exports = {
//... other configuration
module: {
rules: [
{
test: /\.(scss|sass)$/,
use: [
MiniCssExtractPlugin.loader,
'css - loader',
'sass - loader'
]
}
]
},
//... other configuration
};
This configuration allows you to write your styles in Sass files, which will be compiled to CSS and then extracted into separate files.
Production and Development Considerations
Production
In a production environment, you’ll want to optimize the CSS output. You can use tools like optimize - css - assets - webpack - plugin to minify the CSS files. Here’s how you can integrate it with the mini – css – extract – plugin:
npm install optimize - css - assets - webpack - plugin --save - dev
const MiniCssExtractPlugin = require('mini - css - extract - plugin');
const OptimizeCSSAssetsPlugin = require('optimize - css - assets - webpack - plugin');
module.exports = {
//... other configuration
optimization: {
minimizer: [
new OptimizeCSSAssetsPlugin({})
]
},
plugins: [
new MiniCssExtractPlugin({
filename: '[name].[contenthash].css',
chunkFilename: '[id].[contenthash].css'
})
]
};
The optimize - css - assets - webpack - plugin will minify the CSS files, and we’ve also added the contenthash to the filenames. This ensures that the browser cache is busted when the CSS content changes.
Development
In a development environment, you may want to have faster builds and easier debugging. One approach is to use the style - loader instead of the MiniCssExtractPlugin.loader during development. You can create separate webpack configurations for development and production:
webpack.dev.js:
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.css$/,
use: [
'style - loader',
'css - loader'
]
}
]
}
};
webpack.prod.js:
const MiniCssExtractPlugin = require('mini - css - extract - plugin');
const OptimizeCSSAssetsPlugin = require('optimize - css - assets - webpack - plugin');
module.exports = {
entry: './src/index.js',
output: {
path: __dirname + '/dist',
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
'css - loader'
]
}
]
},
optimization: {
minimizer: [
new OptimizeCSSAssetsPlugin({})
]
},
plugins: [
new MiniCssExtractPlugin({
filename: '[name].[contenthash].css',
chunkFilename: '[id].[contenthash].css'
})
]
};
You can then run the appropriate configuration using the following commands:
npx webpack --config webpack.dev.js
npx webpack --config webpack.prod.js
Conclusion and Call to Action

The mini – css – extract – plugin loader is a powerful tool in the webpack ecosystem that can significantly improve the performance and maintainability of your web projects. By carefully configuring it and combining it with other loaders, you can achieve optimal results in both development and production environments.
Wheel Loader As a loader provider, I have extensive experience in helping businesses and developers optimize their webpack configurations. If you’re facing challenges with using the mini – css – extract – plugin loader or any other loaders, I’m here to assist you. Whether it’s customizing the configuration, integrating with other tools, or optimizing for performance, I can provide expert solutions tailored to your needs. Don’t hesitate to contact me for a free consultation and let’s discuss how we can enhance your web development projects.
References
- Webpack official documentation
- Mini – CSS – Extract – Plugin official GitHub repository
- Various web development blogs and forums
Shandong Feisite Machinery Co., Ltd.
As one of the most professional loader manufacturers and suppliers in China, we’re featured by quality products and good service. Please rest assured to buy cheap loader made in China here and get quotation from our factory. Customized orders are welcome.
Address: 100 meters south of Zengfusi Village, Mihe Town, Qingzhou City, Shandong Province
E-mail: Wpeng19911103@qq.com
WebSite: https://www.shandongfeisite.com/