website/docs/en/plugins/rspack/css-extract-rspack-plugin.mdx
import { Table } from '@builtIns'; import { ApiMeta } from '@components/ApiMeta.tsx';
<ApiMeta specific={['Rspack']} />
Rspack is currently incompatible with mini-css-extract-plugin, but you can use the CssExtractRspackPlugin as a replacement. It can be used with css-loader to extract CSS into separate files.
If your project does not depend on css-loader and mini-css-extract-plugin, it is recommended to use Rspack's built-in CSS solution, which offers better performance.
When using CssExtractRspackPlugin, you need to register the plugin in the plugins section and register CssExtractRspackPlugin.loader in module.rules.
import { rspack } from '@rspack/core';
export default {
plugins: [new rspack.CssExtractRspackPlugin({})],
module: {
rules: [
{
test: /\.css$/i,
use: [rspack.CssExtractRspackPlugin.loader, 'css-loader'],
type: 'javascript/auto',
},
],
},
};
CssExtractRspackPlugin adds extracted CSS modules with the css/mini-extract module type. You can use splitChunks.cacheGroups.{cacheGroup}.type to create a cache group that only matches CSS extracted by CssExtractRspackPlugin, without matching JavaScript modules or native CSS modules.
import { rspack } from '@rspack/core';
export default {
plugins: [new rspack.CssExtractRspackPlugin({})],
module: {
rules: [
{
test: /\.css$/i,
type: 'javascript/auto',
use: [rspack.CssExtractRspackPlugin.loader, 'css-loader'],
},
],
},
optimization: {
splitChunks: {
chunks: 'all',
cacheGroups: {
extractedCss: {
type: 'css/mini-extract',
name: 'styles',
chunks: 'all',
enforce: true,
},
},
},
},
};
In this example, extractedCss only selects modules whose module type is css/mini-extract. enforce: true makes the CSS chunk be created even when the normal minSize or request-limit heuristics would skip it; remove enforce if you want the cache group to follow the normal splitChunks heuristics.
Options for CssExtractRspackPlugin.
interface PluginOptions {
filename?: string | ((pathData: PathData, assetInfo?: AssetInfo) => string);
chunkFilename?:
string | ((pathData: PathData, assetInfo?: AssetInfo) => string);
ignoreOrder?: boolean;
insert?: string | ((linkTag: HTMLLinkElement) => void);
attributes?: Record<string, string>;
linkType?: string | 'text/css' | false;
runtime?: boolean;
pathinfo?: boolean;
enforceRelative?: boolean;
}
Options for CssExtractRspackPlugin.loader.
interface LoaderOptions {
publicPath?: string | ((resourcePath: string, context: string) => string);
emit?: boolean;
esModule?: boolean;
}
Please note that if you want to use this plugin and css together, it's recommended to explicitly set type to javascript/auto.
For example:
import { rspack } from '@rspack/core';
export default {
plugins: [new rspack.CssExtractRspackPlugin({})],
module: {
rules: [
{
test: /src\/legacy-project\/.*\.css$/,
type: 'javascript/auto', // Cover the default CSS module type and treat it as a regular JS file.
use: [rspack.CssExtractRspackPlugin.loader, 'css-loader'],
},
{
test: /src\/new-project\/.*\.css$/,
type: 'css/auto', // Handle with built-in CSS
},
],
},
};