Gathering detailed insights and metrics for ssr-mini-css-extract-plugin
Gathering detailed insights and metrics for ssr-mini-css-extract-plugin
Gathering detailed insights and metrics for ssr-mini-css-extract-plugin
Gathering detailed insights and metrics for ssr-mini-css-extract-plugin
Lightweight CSS extraction plugin
npm install ssr-mini-css-extract-plugin
Typescript
Module System
Min. Node Version
Node Version
NPM Version
62.4
Supply Chain
65.9
Quality
79
Maintenance
100
Vulnerability
99.6
License
JavaScript (93.68%)
CSS (5.51%)
HTML (0.75%)
Shell (0.03%)
SCSS (0.03%)
Total Downloads
12,917
Last Day
1
Last Week
29
Last Month
124
Last Year
2,953
MIT License
4,670 Stars
453 Commits
387 Forks
77 Watchers
8 Branches
89 Contributors
Updated on Jun 30, 2025
Minified
Minified + Gzipped
Latest Version
1.6.10
Package Id
ssr-mini-css-extract-plugin@1.6.10
Unpacked Size
89.86 kB
Size
21.84 kB
File Count
11
NPM Version
10.8.2
Node Version
18.20.4
Published on
Aug 18, 2024
Cumulative downloads
Total Downloads
Last Day
0%
1
Compared to previous day
Last Week
-25.6%
29
Compared to previous week
Last Month
22.8%
124
Compared to previous month
Last Year
-51.3%
2,953
Compared to previous year
3
1
28
This plugin extracts CSS into separate files. It creates a CSS file per JS file which contains CSS. It supports On-Demand-Loading of CSS and SourceMaps.
It builds on top of a new webpack v4 feature (module types) and requires webpack 4 to work.
Compared to the extract-text-webpack-plugin:
To begin, you'll need to install mini-css-extract-plugin
:
1npm install --save-dev mini-css-extract-plugin
It's recommended to combine mini-css-extract-plugin
with the css-loader
Then add the loader and the plugin to your webpack
config. For example:
style.css
1body { 2 background: green; 3}
component.js
1import './style.css';
webpack.config.js
1const MiniCssExtractPlugin = require('mini-css-extract-plugin'); 2 3module.exports = { 4 plugins: [new MiniCssExtractPlugin()], 5 module: { 6 rules: [ 7 { 8 test: /\.css$/i, 9 use: [MiniCssExtractPlugin.loader, 'css-loader'], 10 }, 11 ], 12 }, 13};
Name | Type | Default | Description |
---|---|---|---|
filename | {String|Function} | [name].css | This option determines the name of each output CSS file |
chunkFilename | {String|Function} | based on filename | This option determines the name of non-entry chunk files |
ignoreOrder | {Boolean} | false | Remove Order Warnings |
insert | {String|Function} | document.head.appendChild(linkTag); | Inserts <link> at the given position |
attributes | {Object} | {} | Adds custom attributes to tag |
linkType | {String|Boolean} | text/css | Allows loading asynchronous chunks with a custom link type |
experimentalUseImportModule | {Boolean} | false | Use an experimental webpack API to execute modules instead of child compilers |
filename
Type: String|Function
Default: [name].css
This option determines the name of each output CSS file.
Works like output.filename
chunkFilename
Type: String|Function
Default: based on filename
i Specifying
chunkFilename
as afunction
is only available in webpack@5
This option determines the name of non-entry chunk files.
Works like output.chunkFilename
ignoreOrder
Type: Boolean
Default: false
Remove Order Warnings. See examples below for details.
insert
Type: String|Function
Default: document.head.appendChild(linkTag);
By default, the mini-css-extract-plugin
appends styles (<link>
elements) to document.head
of the current window
.
However in some circumstances it might be necessary to have finer control over the append target or even delay link
elements insertion.
For example this is the case when you asynchronously load styles for an application that runs inside of an iframe.
In such cases insert
can be configured to be a function or a custom selector.
If you target an iframe make sure that the parent document has sufficient access rights to reach into the frame document and append elements to it.
String
Allows to setup custom query selector.
A new <link>
element will be inserted after the found item.
webpack.config.js
1new MiniCssExtractPlugin({
2 insert: '#some-element',
3});
A new <link>
element will be inserted after the element with id some-element
.
Function
Allows to override default behavior and insert styles at any position.
⚠ Do not forget that this code will run in the browser alongside your application. Since not all browsers support latest ECMA features like
let
,const
,arrow function expression
and etc we recommend you to use only ECMA 5 features and syntax.
⚠ The
insert
function is serialized to string and passed to the plugin. This means that it won't have access to the scope of the webpack configuration module.
webpack.config.js
1new MiniCssExtractPlugin({
2 insert: function (linkTag) {
3 var reference = document.querySelector('#some-element');
4 if (reference) {
5 reference.parentNode.insertBefore(linkTag, reference);
6 }
7 },
8});
A new <link>
element will be inserted before the element with id some-element
.
attributes
Type: Object
Default: {}
If defined, the mini-css-extract-plugin
will attach given attributes with their values on element.
webpack.config.js
1const MiniCssExtractPlugin = require('mini-css-extract-plugin'); 2 3module.exports = { 4 plugins: [ 5 new MiniCssExtractPlugin({ 6 attributes: { 7 id: 'target', 8 'data-target': 'example', 9 }, 10 }), 11 ], 12 module: { 13 rules: [ 14 { 15 test: /\.css$/i, 16 use: [MiniCssExtractPlugin.loader, 'css-loader'], 17 }, 18 ], 19 }, 20};
Note: It's only applied to dynamically loaded css chunks, if you want to modify link attributes inside html file, please using html-webpack-plugin
linkType
Type: String|Boolean
Default: text/css
This option allows loading asynchronous chunks with a custom link type, such as <link type="text/css" ...>.
String
Possible values: text/css
webpack.config.js
1const MiniCssExtractPlugin = require('mini-css-extract-plugin'); 2 3module.exports = { 4 plugins: [ 5 new MiniCssExtractPlugin({ 6 linkType: 'text/css', 7 }), 8 ], 9 module: { 10 rules: [ 11 { 12 test: /\.css$/i, 13 use: [MiniCssExtractPlugin.loader, 'css-loader'], 14 }, 15 ], 16 }, 17};
Boolean
false
disables the link type
attribute
webpack.config.js
1const MiniCssExtractPlugin = require('mini-css-extract-plugin'); 2 3module.exports = { 4 plugins: [ 5 new MiniCssExtractPlugin({ 6 linkType: false, 7 }), 8 ], 9 module: { 10 rules: [ 11 { 12 test: /\.css$/i, 13 use: [MiniCssExtractPlugin.loader, 'css-loader'], 14 }, 15 ], 16 }, 17};
experimentalUseImportModule
Use an experimental webpack API to execute modules instead of child compilers.
This improves performance and memory usage a lot, but isn't as stable as the normal approach.
When combined with experiments.layers
, this adds a layer
option to the loader options to specify the layer of the css execution.
You need to have at least webpack 5.33.2.
webpack.config.js
1const MiniCssExtractPlugin = require('mini-css-extract-plugin'); 2 3module.exports = { 4 plugins: [ 5 new MiniCssExtractPlugin({ 6 experimentalUseImportModule: true, 7 }), 8 ], 9 module: { 10 rules: [ 11 { 12 test: /\.css$/i, 13 use: [MiniCssExtractPlugin.loader, 'css-loader'], 14 }, 15 ], 16 }, 17};
Name | Type | Default | Description |
---|---|---|---|
publicPath | {String|Function} | webpackOptions.output.publicPath | Specifies a custom public path for the external resources like images, files, etc |
emit | {Boolean} | true | If false, the plugin will extract the CSS but will not emit the file |
esModule | {Boolean} | true | Use ES modules syntax |
modules | {Object} | undefined | Configuration CSS Modules |
publicPath
Type: String|Function
Default: the publicPath
in webpackOptions.output
Specifies a custom public path for the external resources like images, files, etc inside CSS
.
Works like output.publicPath
String
webpack.config.js
1const MiniCssExtractPlugin = require('mini-css-extract-plugin'); 2 3module.exports = { 4 plugins: [ 5 new MiniCssExtractPlugin({ 6 // Options similar to the same options in webpackOptions.output 7 // both options are optional 8 filename: '[name].css', 9 chunkFilename: '[id].css', 10 }), 11 ], 12 module: { 13 rules: [ 14 { 15 test: /\.css$/, 16 use: [ 17 { 18 loader: MiniCssExtractPlugin.loader, 19 options: { 20 publicPath: '/public/path/to/', 21 }, 22 }, 23 'css-loader', 24 ], 25 }, 26 ], 27 }, 28};
Function
webpack.config.js
1const MiniCssExtractPlugin = require('mini-css-extract-plugin'); 2 3module.exports = { 4 plugins: [ 5 new MiniCssExtractPlugin({ 6 // Options similar to the same options in webpackOptions.output 7 // both options are optional 8 filename: '[name].css', 9 chunkFilename: '[id].css', 10 }), 11 ], 12 module: { 13 rules: [ 14 { 15 test: /\.css$/, 16 use: [ 17 { 18 loader: MiniCssExtractPlugin.loader, 19 options: { 20 publicPath: (resourcePath, context) => { 21 return path.relative(path.dirname(resourcePath), context) + '/'; 22 }, 23 }, 24 }, 25 'css-loader', 26 ], 27 }, 28 ], 29 }, 30};
emit
Type: Boolean
Default: true
If true, emits a file (writes a file to the filesystem). If false, the plugin will extract the CSS but will not emit the file. It is often useful to disable this option for server-side packages.
esModule
Type: Boolean
Default: true
By default, mini-css-extract-plugin
generates JS modules that use the ES modules syntax.
There are some cases in which using ES modules is beneficial, like in the case of module concatenation and tree shaking.
You can enable a CommonJS syntax using:
webpack.config.js
1const MiniCssExtractPlugin = require('mini-css-extract-plugin'); 2 3module.exports = { 4 plugins: [new MiniCssExtractPlugin()], 5 module: { 6 rules: [ 7 { 8 test: /\.css$/i, 9 use: [ 10 { 11 loader: MiniCssExtractPlugin.loader, 12 options: { 13 esModule: false, 14 }, 15 }, 16 'css-loader', 17 ], 18 }, 19 ], 20 }, 21};
modules
Type: Object
Default: undefined
Configuration CSS Modules.
namedExport
Type: Boolean
Default: false
Enables/disables ES modules named export for locals.
⚠ Names of locals are converted to
camelCase
.
⚠ It is not allowed to use JavaScript reserved words in css class names.
⚠ Options
esModule
andmodules.namedExport
incss-loader
andMiniCssExtractPlugin.loader
should be enabled.
styles.css
1.foo-baz { 2 color: red; 3} 4.bar { 5 color: blue; 6}
index.js
1import { fooBaz, bar } from './styles.css'; 2 3console.log(fooBaz, bar);
You can enable a ES module named export using:
webpack.config.js
1const MiniCssExtractPlugin = require('mini-css-extract-plugin'); 2 3module.exports = { 4 plugins: [new MiniCssExtractPlugin()], 5 module: { 6 rules: [ 7 { 8 test: /\.css$/, 9 use: [ 10 { 11 loader: MiniCssExtractPlugin.loader, 12 options: { 13 esModule: true, 14 modules: { 15 namedExport: true, 16 }, 17 }, 18 }, 19 { 20 loader: 'css-loader', 21 options: { 22 esModule: true, 23 modules: { 24 namedExport: true, 25 localIdentName: 'foo__[name]__[local]', 26 }, 27 }, 28 }, 29 ], 30 }, 31 ], 32 }, 33};
For production
builds it's recommended to extract the CSS from your bundle being able to use parallel loading of CSS/JS resources later on.
This can be achieved by using the mini-css-extract-plugin
, because it creates separate css files.
For development
mode (including webpack-dev-server
) you can use style-loader, because it injects CSS into the DOM using multiple and works faster.
i Do not use together
style-loader
andmini-css-extract-plugin
.
webpack.config.js
1const MiniCssExtractPlugin = require('mini-css-extract-plugin'); 2const devMode = process.env.NODE_ENV !== 'production'; 3 4module.exports = { 5 module: { 6 rules: [ 7 { 8 test: /\.(sa|sc|c)ss$/, 9 use: [ 10 devMode ? 'style-loader' : MiniCssExtractPlugin.loader, 11 'css-loader', 12 'postcss-loader', 13 'sass-loader', 14 ], 15 }, 16 ], 17 }, 18 plugins: [].concat(devMode ? [] : [new MiniCssExtractPlugin()]), 19};
webpack.config.js
1const MiniCssExtractPlugin = require('mini-css-extract-plugin'); 2 3module.exports = { 4 plugins: [ 5 new MiniCssExtractPlugin({ 6 // Options similar to the same options in webpackOptions.output 7 // all options are optional 8 filename: '[name].css', 9 chunkFilename: '[id].css', 10 ignoreOrder: false, // Enable to remove warnings about conflicting order 11 }), 12 ], 13 module: { 14 rules: [ 15 { 16 test: /\.css$/, 17 use: [ 18 { 19 loader: MiniCssExtractPlugin.loader, 20 options: { 21 // you can specify a publicPath here 22 // by default it uses publicPath in webpackOptions.output 23 publicPath: '../', 24 }, 25 }, 26 'css-loader', 27 ], 28 }, 29 ], 30 }, 31};
publicPath
option as functionwebpack.config.js
1const MiniCssExtractPlugin = require('mini-css-extract-plugin'); 2 3module.exports = { 4 plugins: [ 5 new MiniCssExtractPlugin({ 6 // Options similar to the same options in webpackOptions.output 7 // both options are optional 8 filename: '[name].css', 9 chunkFilename: '[id].css', 10 }), 11 ], 12 module: { 13 rules: [ 14 { 15 test: /\.css$/, 16 use: [ 17 { 18 loader: MiniCssExtractPlugin.loader, 19 options: { 20 publicPath: (resourcePath, context) => { 21 // publicPath is the relative path of the resource to the context 22 // e.g. for ./css/admin/main.css the publicPath will be ../../ 23 // while for ./css/main.css the publicPath will be ../ 24 return path.relative(path.dirname(resourcePath), context) + '/'; 25 }, 26 }, 27 }, 28 'css-loader', 29 ], 30 }, 31 ], 32 }, 33};
This plugin should not be used with style-loader
in the loaders chain.
Here is an example to have both HMR in development
and your styles extracted in a file for production
builds.
(Loaders options left out for clarity, adapt accordingly to your needs.)
You should not use HotModuleReplacementPlugin
plugin if you are using a webpack-dev-server
.
webpack-dev-server
enables / disables HMR using hot
option.
webpack.config.js
1const webpack = require('webpack'); 2const MiniCssExtractPlugin = require('mini-css-extract-plugin'); 3const devMode = process.env.NODE_ENV !== 'production'; 4 5const plugins = [ 6 new MiniCssExtractPlugin({ 7 // Options similar to the same options in webpackOptions.output 8 // both options are optional 9 filename: devMode ? '[name].css' : '[name].[contenthash].css', 10 chunkFilename: devMode ? '[id].css' : '[id].[contenthash].css', 11 }), 12]; 13if (devMode) { 14 // only enable hot in development 15 plugins.push(new webpack.HotModuleReplacementPlugin()); 16} 17 18module.exports = { 19 plugins, 20 module: { 21 rules: [ 22 { 23 test: /\.(sa|sc|c)ss$/, 24 use: [ 25 MiniCssExtractPlugin.loader, 26 'css-loader', 27 'postcss-loader', 28 'sass-loader', 29 ], 30 }, 31 ], 32 }, 33};
Note: HMR is automatically supported in webpack 5. No need to configure it. Skip the following:
The mini-css-extract-plugin
supports hot reloading of actual css files in development.
Some options are provided to enable HMR of both standard stylesheets and locally scoped CSS or CSS modules.
Below is an example configuration of mini-css for HMR use with CSS modules.
You should not use HotModuleReplacementPlugin
plugin if you are using a webpack-dev-server
.
webpack-dev-server
enables / disables HMR using hot
option.
webpack.config.js
1const webpack = require('webpack'); 2const MiniCssExtractPlugin = require('mini-css-extract-plugin'); 3 4const plugins = [ 5 new MiniCssExtractPlugin({ 6 // Options similar to the same options in webpackOptions.output 7 // both options are optional 8 filename: devMode ? '[name].css' : '[name].[contenthash].css', 9 chunkFilename: devMode ? '[id].css' : '[id].[contenthash].css', 10 }), 11]; 12if (devMode) { 13 // only enable hot in development 14 plugins.push(new webpack.HotModuleReplacementPlugin()); 15} 16 17module.exports = { 18 plugins, 19 module: { 20 rules: [ 21 { 22 test: /\.css$/, 23 use: [ 24 { 25 loader: MiniCssExtractPlugin.loader, 26 options: {}, 27 }, 28 'css-loader', 29 ], 30 }, 31 ], 32 }, 33};
To minify the output, use a plugin like css-minimizer-webpack-plugin.
webpack.config.js
1const MiniCssExtractPlugin = require('mini-css-extract-plugin'); 2const CssMinimizerPlugin = require('css-minimizer-webpack-plugin'); 3 4module.exports = { 5 plugins: [ 6 new MiniCssExtractPlugin({ 7 filename: '[name].css', 8 chunkFilename: '[id].css', 9 }), 10 ], 11 module: { 12 rules: [ 13 { 14 test: /\.css$/, 15 use: [MiniCssExtractPlugin.loader, 'css-loader'], 16 }, 17 ], 18 }, 19 optimization: { 20 minimizer: [ 21 // For webpack@5 you can use the `...` syntax to extend existing minimizers (i.e. `terser-webpack-plugin`), uncomment the next line 22 // `...`, 23 new CssMinimizerPlugin(), 24 ], 25 }, 26};
This will enable CSS optimization only in production mode. If you want to run it also in development set the optimization.minimize
option to true.
The runtime code detects already added CSS via <link>
or <style>
tag.
This can be useful when injecting CSS on server-side for Server-Side-Rendering.
The href
of the <link>
tag has to match the URL that will be used for loading the CSS chunk.
The data-href
attribute can be used for <link>
and <style>
too.
When inlining CSS data-href
must be used.
The CSS can be extracted in one CSS file using optimization.splitChunks.cacheGroups
.
webpack.config.js
1const MiniCssExtractPlugin = require('mini-css-extract-plugin'); 2 3module.exports = { 4 optimization: { 5 splitChunks: { 6 cacheGroups: { 7 styles: { 8 name: 'styles', 9 type: 'css/mini-extract', 10 // For webpack@4 11 // test: /\.css$/, 12 chunks: 'all', 13 enforce: true, 14 }, 15 }, 16 }, 17 }, 18 plugins: [ 19 new MiniCssExtractPlugin({ 20 filename: '[name].css', 21 }), 22 ], 23 module: { 24 rules: [ 25 { 26 test: /\.css$/, 27 use: [MiniCssExtractPlugin.loader, 'css-loader'], 28 }, 29 ], 30 }, 31};
Note that type
should be used instead of test
in Webpack 5, or else an extra .js
file can be generated besides the .css
file. This is because test
doesn't know which modules should be dropped (in this case, it won't detect that .js
should be dropped).
You may also extract the CSS based on the webpack entry name. This is especially useful if you import routes dynamically but want to keep your CSS bundled according to entry. This also prevents the CSS duplication issue one had with the ExtractTextPlugin.
1const path = require('path'); 2const MiniCssExtractPlugin = require('mini-css-extract-plugin'); 3 4function recursiveIssuer(m, c) { 5 const issuer = c.moduleGraph.getIssuer(m); 6 // For webpack@4 issuer = m.issuer 7 8 if (issuer) { 9 return recursiveIssuer(issuer, c); 10 } 11 12 const chunks = c.chunkGraph.getModuleChunks(m); 13 // For webpack@4 chunks = m._chunks 14 15 for (const chunk of chunks) { 16 return chunk.name; 17 } 18 19 return false; 20} 21 22module.exports = { 23 entry: { 24 foo: path.resolve(__dirname, 'src/foo'), 25 bar: path.resolve(__dirname, 'src/bar'), 26 }, 27 optimization: { 28 splitChunks: { 29 cacheGroups: { 30 fooStyles: { 31 name: 'styles_foo', 32 test: (m, c, entry = 'foo') => 33 m.constructor.name === 'CssModule' && 34 recursiveIssuer(m, c) === entry, 35 chunks: 'all', 36 enforce: true, 37 }, 38 barStyles: { 39 name: 'styles_bar', 40 test: (m, c, entry = 'bar') => 41 m.constructor.name === 'CssModule' && 42 recursiveIssuer(m, c) === entry, 43 chunks: 'all', 44 enforce: true, 45 }, 46 }, 47 }, 48 }, 49 plugins: [ 50 new MiniCssExtractPlugin({ 51 filename: '[name].css', 52 }), 53 ], 54 module: { 55 rules: [ 56 { 57 test: /\.css$/, 58 use: [MiniCssExtractPlugin.loader, 'css-loader'], 59 }, 60 ], 61 }, 62};
With the filename
option you can use chunk data to customize the filename.
This is particularly useful when dealing with multiple entry points and wanting to get more control out of the filename for a given entry point/chunk.
In the example below, we'll use filename
to output the generated css into a different directory.
webpack.config.js
1const MiniCssExtractPlugin = require('mini-css-extract-plugin'); 2 3module.exports = { 4 plugins: [ 5 new MiniCssExtractPlugin({ 6 filename: ({ chunk }) => `${chunk.name.replace('/js/', '/css/')}.css`, 7 }), 8 ], 9 module: { 10 rules: [ 11 { 12 test: /\.css$/, 13 use: [MiniCssExtractPlugin.loader, 'css-loader'], 14 }, 15 ], 16 }, 17};
For long term caching use filename: "[contenthash].css"
. Optionally add [name]
.
webpack.config.js
1const MiniCssExtractPlugin = require('mini-css-extract-plugin'); 2 3module.exports = { 4 plugins: [ 5 new MiniCssExtractPlugin({ 6 filename: '[name].[contenthash].css', 7 chunkFilename: '[id].[contenthash].css', 8 }), 9 ], 10 module: { 11 rules: [ 12 { 13 test: /\.css$/, 14 use: [MiniCssExtractPlugin.loader, 'css-loader'], 15 }, 16 ], 17 }, 18};
For projects where css ordering has been mitigated through consistent use of scoping or naming conventions, the css order warnings can be disabled by setting the ignoreOrder flag to true for the plugin.
webpack.config.js
1const MiniCssExtractPlugin = require('mini-css-extract-plugin'); 2 3module.exports = { 4 plugins: [ 5 new MiniCssExtractPlugin({ 6 ignoreOrder: true, 7 }), 8 ], 9 module: { 10 rules: [ 11 { 12 test: /\.css$/i, 13 use: [MiniCssExtractPlugin.loader, 'css-loader'], 14 }, 15 ], 16 }, 17};
If you'd like to extract the media queries from the extracted CSS (so mobile users don't need to load desktop or tablet specific CSS anymore) you should use one of the following plugins:
Please take a moment to read our contributing guidelines if you haven't yet done so.
No vulnerabilities found.
Reason
GitHub workflow tokens follow principle of least privilege
Details
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 13/27 approved changesets -- score normalized to 4
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
0 commit(s) and 2 issue activity found in the last 90 days -- score normalized to 1
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
21 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-06-23
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More