Gathering detailed insights and metrics for extract-css-chunks-webpack-plugin
Gathering detailed insights and metrics for extract-css-chunks-webpack-plugin
Gathering detailed insights and metrics for extract-css-chunks-webpack-plugin
Gathering detailed insights and metrics for extract-css-chunks-webpack-plugin
css-chunks-html-webpack-plugin
Injecting css chunks extracted using extract-css-chunks-webpack-plugin to HTML for html-webpack-plugin Edit
extract-css-chunks-webpack-plugin-with-page-direction
Extract CSS from chunks into stylesheets + HMR. Supports Webpack 4 + SSR
tv-extract-css-chunks-webpack-plugin
Extract CSS from chunks into stylesheets + HMR
@prepair/extract-css-chunks-webpack-plugin
Extract CSS from chunks into stylesheets + HMR. Supports Webpack 4 + SSR
Extract CSS from chunks into multiple stylesheets + HMR
npm install extract-css-chunks-webpack-plugin
Typescript
Module System
Min. Node Version
88.4
Supply Chain
69.2
Quality
76.5
Maintenance
100
Vulnerability
99.6
License
JavaScript (87.06%)
CSS (6.9%)
HTML (6.04%)
Total Downloads
104,523,807
Last Day
11,386
Last Week
223,974
Last Month
952,054
Last Year
11,355,904
MIT License
695 Stars
510 Commits
85 Forks
13 Watchers
32 Branches
78 Contributors
Updated on Nov 07, 2024
Latest Version
4.10.0
Package Id
extract-css-chunks-webpack-plugin@4.10.0
Unpacked Size
62.52 kB
Size
17.07 kB
File Count
15
Published on
Sep 15, 2023
Cumulative downloads
Total Downloads
4
1
38
If you like our work, check out our Redux-based router redux-first-router or its sucessor which, Rudy
HEADLINES (May 2018): Now Independently supports Webpack 4: Yep that's right. The universal family is now fully Webpack 4. Thank you to all our users for your loyalty and patience! If you love Universal, then you are gonna fall head over heels when we bring out the main course!
So... why did we rebuild extract-css-chunks-webpack-plugin
? What does it offer?
It's got all the goodness of mini-css-extract-plugin
but with 2 gleaming, sought after benefits.
Compared to the existing loaders, we are offering a single solution as opposed to needing to depend on multiple loaders to cater for different features:
insert
optionAdditionally, if you are already a user of the universal family -- we will be waving goodbye to the mandatory window.__CSS_CHUNKS__
.
The functionality is still available to you via chunk flushing, and it can come in super handy when needing to easily resolve style assets as urls that might need to be passed to a third party.
webpack.config.js
1const ExtractCssChunks = require('extract-css-chunks-webpack-plugin'); 2 3module.exports = { 4 plugins: [new ExtractCssChunks()], 5 module: { 6 rules: [ 7 { 8 test: /\.css$/i, 9 use: [ExtractCssChunks.loader, 'css-loader'], 10 }, 11 ], 12 }, 13};
publicPath
Type: String|Function
Default: the publicPath
in webpackOptions.output
Specifies a custom public path for the target file(s).
String
webpack.config.js
1const ExtractCssChunks = require('extract-css-chunks-webpack-plugin'); 2 3module.exports = { 4 plugins: [ 5 new ExtractCssChunks({ 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: ExtractCssChunks.loader, 19 options: { 20 publicPath: '/public/path/to/', 21 }, 22 }, 23 'css-loader', 24 ], 25 }, 26 ], 27 }, 28};
Function
webpack.config.js
1const ExtractCssChunks = require('extract-css-chunks-webpack-plugin'); 2 3module.exports = { 4 plugins: [ 5 new ExtractCssChunks({ 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: ExtractCssChunks.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};
esModule
Type: Boolean
Default: false
By default, extract-css-chunks-webpack-plugin
generates JS modules that use the CommonJS 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 ES module syntax using:
webpack.config.js
1const ExtractCssChunks = require('extract-css-chunks-webpack-plugin'); 2 3module.exports = { 4 plugins: [new ExtractCssChunks()], 5 module: { 6 rules: [ 7 { 8 test: /\.css$/i, 9 use: [ 10 { 11 loader: ExtractCssChunks.loader, 12 options: { 13 esModule: true, 14 }, 15 }, 16 'css-loader', 17 ], 18 }, 19 ], 20 }, 21};
webpack.config.js
1const ExtractCssChunks = require('extract-css-chunks-webpack-plugin'); 2 3module.exports = { 4 plugins: [ 5 new ExtractCssChunks({ 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: ExtractCssChunks.loader, 20 options: { 21 // you can specify a publicPath here 22 // by default it uses publicPath in webpackOptions.output 23 publicPath: '../', 24 hmr: process.env.NODE_ENV === 'development', 25 }, 26 }, 27 'css-loader', 28 ], 29 }, 30 ], 31 }, 32};
publicPath
option as functionwebpack.config.js
1const ExtractCssChunks = require('extract-css-chunks-webpack-plugin'); 2 3module.exports = { 4 plugins: [ 5 new ExtractCssChunks({ 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: ExtractCssChunks.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 be used only on production
builds without style-loader
in the loaders chain, especially if you want to have HMR in development
.
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.)
webpack.config.js
1const ExtractCssChunks = require('extract-css-chunks-webpack-plugin'); 2const devMode = process.env.NODE_ENV !== 'production'; 3 4module.exports = { 5 plugins: [ 6 new ExtractCssChunks({ 7 // Options similar to the same options in webpackOptions.output 8 // both options are optional 9 filename: devMode ? '[name].css' : '[name].[hash].css', 10 chunkFilename: devMode ? '[id].css' : '[id].[hash].css', 11 }), 12 ], 13 module: { 14 rules: [ 15 { 16 test: /\.(sa|sc|c)ss$/, 17 use: [ 18 { 19 loader: ExtractCssChunks.loader, 20 options: { 21 hmr: process.env.NODE_ENV === 'development', 22 }, 23 }, 24 'css-loader', 25 'postcss-loader', 26 'sass-loader', 27 ], 28 }, 29 ], 30 }, 31};
The extract-css-chunks-webpack-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 extract-css-chunks
for HMR use with CSS modules.
While we attempt to hmr css-modules. It is not easy to perform when code-splitting with custom chunk names.
reloadAll
is an option that should only be enabled if HMR isn't working correctly.
The core challenge with css-modules is that when code-split, the chunk ids can and do end up different compared to the filename.
webpack.config.js
1const ExtractCssChunks = require('extract-css-chunks-webpack-plugin'); 2 3module.exports = { 4 plugins: [ 5 new ExtractCssChunks({ 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: ExtractCssChunks.loader, 19 options: { 20 // only enable hot in development 21 hmr: process.env.NODE_ENV === 'development', 22 // if hmr does not work, this is a forceful method. 23 reloadAll: true, 24 }, 25 }, 26 'css-loader', 27 ], 28 }, 29 ], 30 }, 31};
To minify the output, use a plugin like optimize-css-assets-webpack-plugin.
Setting optimization.minimizer
overrides the defaults provided by webpack, so make sure to also specify a JS minimizer:
webpack.config.js
1const TerserJSPlugin = require('terser-webpack-plugin'); 2const ExtractCssChunks = require('extract-css-chunks-webpack-plugin'); 3const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin'); 4 5module.exports = { 6 optimization: { 7 minimizer: [new TerserJSPlugin({}), new OptimizeCSSAssetsPlugin({})], 8 }, 9 plugins: [ 10 new ExtractCssChunks({ 11 filename: '[name].css', 12 chunkFilename: '[id].css', 13 }), 14 ], 15 module: { 16 rules: [ 17 { 18 test: /\.css$/, 19 use: [ExtractCssChunks.loader, 'css-loader'], 20 }, 21 ], 22 }, 23};
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.
Similar to what extract-text-webpack-plugin does, the CSS can be extracted in one CSS file using optimization.splitChunks.cacheGroups
.
webpack.config.js
1const ExtractCssChunks = require('extract-css-chunks-webpack-plugin'); 2 3module.exports = { 4 optimization: { 5 splitChunks: { 6 cacheGroups: { 7 styles: { 8 name: 'styles', 9 test: /\.css$/, 10 chunks: 'all', 11 enforce: true, 12 }, 13 }, 14 }, 15 }, 16 plugins: [ 17 new ExtractCssChunks({ 18 filename: '[name].css', 19 }), 20 ], 21 module: { 22 rules: [ 23 { 24 test: /\.css$/, 25 use: [ExtractCssChunks.loader, 'css-loader'], 26 }, 27 ], 28 }, 29};
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 ExtractCssChunks = require('extract-css-chunks-webpack-plugin'); 3 4function recursiveIssuer(m) { 5 if (m.issuer) { 6 return recursiveIssuer(m.issuer); 7 } else if (m.name) { 8 return m.name; 9 } else { 10 return false; 11 } 12} 13 14module.exports = { 15 entry: { 16 foo: path.resolve(__dirname, 'src/foo'), 17 bar: path.resolve(__dirname, 'src/bar'), 18 }, 19 optimization: { 20 splitChunks: { 21 cacheGroups: { 22 fooStyles: { 23 name: 'foo', 24 test: (m, c, entry = 'foo') => 25 m.constructor.name === 'CssModule' && recursiveIssuer(m) === entry, 26 chunks: 'all', 27 enforce: true, 28 }, 29 barStyles: { 30 name: 'bar', 31 test: (m, c, entry = 'bar') => 32 m.constructor.name === 'CssModule' && recursiveIssuer(m) === entry, 33 chunks: 'all', 34 enforce: true, 35 }, 36 }, 37 }, 38 }, 39 plugins: [ 40 new ExtractCssChunks({ 41 filename: '[name].css', 42 }), 43 ], 44 module: { 45 rules: [ 46 { 47 test: /\.css$/, 48 use: [ExtractCssChunks.loader, 'css-loader'], 49 }, 50 ], 51 }, 52};
With the moduleFilename
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 moduleFilename
to output the generated css into a different directory.
webpack.config.js
1const ExtractCssChunks = require('extract-css-chunks-webpack-plugin'); 2 3module.exports = { 4 plugins: [ 5 new ExtractCssChunks({ 6 moduleFilename: ({ name }) => `${name.replace('/js/', '/css/')}.css`, 7 }), 8 ], 9 module: { 10 rules: [ 11 { 12 test: /\.css$/, 13 use: [ExtractCssChunks.loader, 'css-loader'], 14 }, 15 ], 16 }, 17};
For long term caching use filename: "[contenthash].css"
. Optionally add [name]
.
webpack.config.js
1const ExtractCssChunks = require('extract-css-chunks-webpack-plugin'); 2 3module.exports = { 4 plugins: [ 5 new ExtractCssChunks({ 6 filename: '[name].[contenthash].css', 7 chunkFilename: '[id].[contenthash].css', 8 }), 9 ], 10 module: { 11 rules: [ 12 { 13 test: /\.css$/, 14 use: [ExtractCssChunks.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 ExtractCssChunks = require('extract-css-chunks-webpack-plugin'); 2 3module.exports = { 4 plugins: [ 5 new ExtractCssChunks({ 6 ignoreOrder: true, 7 }), 8 ], 9 module: { 10 rules: [ 11 { 12 test: /\.css$/i, 13 use: [ExtractCssChunks.loader, 'css-loader'], 14 }, 15 ], 16 }, 17};
Type: Function
Default: head
By default, the extract-css-chunks-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 instertion. 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.
insert
as a functionAllows 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.
1new ExtractCssChunksPlugin({
2 insert: function insert(linkTag) {
3 const 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
.
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:
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 6/27 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
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
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
98 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-06-30
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 MoreLast Day
-22.8%
11,386
Compared to previous day
Last Week
-3.7%
223,974
Compared to previous week
Last Month
-1.6%
952,054
Compared to previous month
Last Year
-25.9%
11,355,904
Compared to previous year