Startingwithversion[4.0.0](https://github.com/webpack-contrib/css-loader/blob/master/CHANGELOG.md#400-2020-07-25), absolute paths are parsed based on the server root.
-`undefined` - enable CSS modules for all files matching `/\.module\.\w+$/i.test(filename)` and `/\.icss\.\w+$/i.test(filename)`regexp.
-`true`-enableCSSmodulesforallfiles.
-`false`-disablesCSSModulesforallfiles.
-`string` - disables CSS Modules for all files and set the `mode`option,moreinformationyoucanread[here](https://github.com/webpack-contrib/css-loader#mode)
-`object` - enable CSS modules for all files, if `modules.auto` option is not specified, otherwise the `modules.auto`optionwilldeterminewhetherifitisCSSmodulesornot,moreinformationyoucanread[here](https://github.com/webpack-contrib/css-loader#auto)
The`modules`optionenables/disablesthe**[CSSModules](https://github.com/css-modules/css-modules)** specification and setup basic behaviour.
-`true` - enables CSS modules or interoperable CSS format, sets the [`modules.mode`](#mode) option to `local` value for all files which satisfy `/\.module(s)?\.\w+$/i.test(filename)` condition or sets the [`modules.mode`](#mode) option to `icss` value for all files which satisfy `/\.icss\.\w+$/i.test(filename)`condition
-`[folder]` the folder the resource relative to the `compiler.context` option or `modules.localIdentContext`option.
-`[path]` the path of the resource relative to the `compiler.context` option or `modules.localIdentContext`option.
-`[file]`-filenameandpath.
-`[ext]` - extension with leading `.`.
-`[hash]` - the hash of the string, generated based on `localIdentHashSalt`, `localIdentHashFunction`, `localIdentHashDigest`, `localIdentHashDigestLength`, `localIdentContext`, `resourcePath` and `exportName`
Allowstoenables/disablesorsetupsnumberofloadersappliedbeforeCSSloaderfor`@import` at-rules, CSS modules and ICSS imports, i.e. `@import`/`composes`/`@value value from './values.css'`/etc.
Theoption`importLoaders` allows you to configure how many loaders before `css-loader` should be applied to `@import`edresourcesandCSSmodules/ICSSimports.
TherearesomecasesinwhichusingESmodulesisbeneficial,likeinthecaseof[moduleconcatenation](https://webpack.js.org/plugins/module-concatenation-plugin/) and [tree shaking](https://webpack.js.org/guides/tree-shaking/).
>Youdon'tneed[`style-loader`](https://github.com/webpack-contrib/style-loader) anymore, please remove it.
>**Warning**
>
>The`esModules` option should be enabled if you want to use it with [`CSS modules`](https://github.com/webpack-contrib/css-loader#modules), by default for locals will be used [named export](https://github.com/webpack-contrib/css-loader#namedexport).
>Youdon'tneed[`style-loader`](https://github.com/webpack-contrib/style-loader) anymore, please remove it.
>**Warning**
>
>The`esModules` option should be enabled if you want to use it with [`CSS modules`](https://github.com/webpack-contrib/css-loader#modules), by default for locals will be used [named export](https://github.com/webpack-contrib/css-loader#namedexport).
Thiscanbeachievedbyusingthe[mini-css-extract-plugin](https://github.com/webpack-contrib/mini-css-extract-plugin), because it creates separate css files.
For`development` mode (including `webpack-dev-server`)youcanuse[style-loader](https://github.com/webpack-contrib/style-loader), because it injects CSS into the DOM using multiple <style></style> and works faster.
>**Note**
>
>Donotuse`style-loader` and `mini-css-extract-plugin`together.
/* Disabled url handling for the second url in the 'background' declaration */
color:red;
background:url("./url/img.png"),
/* webpackIgnore: true */url("./url/img.png");
}
/* prettier-ignore */
.class{
/* Disabled url handling for the second url in the 'background' declaration */
color:red;
background:url("./url/img.png"),
/* webpackIgnore: true */
url("./url/img.png");
}
/* prettier-ignore */
.class{
/* Disabled url handling for third and sixth urls in the 'background-image' declaration */
background-image:image-set(
url(./url/img.png)2x,
url(./url/img.png)3x,
/* webpackIgnore: true */url(./url/img.png)4x,
url(./url/img.png)5x,
url(./url/img.png)6x,
/* webpackIgnore: true */
url(./url/img.png)7x
);
}
```
###Assets
Thefollowing`webpack.config.js`canloadCSSfiles,embedsmallPNG/JPG/GIF/SVGimagesaswellasfontsas[DataURLs](https://tools.ietf.org/html/rfc2397) and copy larger files to the output directory.
-Thiscanbeachievedbyusingthe[mini-css-extract-plugin](https://github.com/webpack-contrib/mini-css-extract-plugin) to extract the CSS when running in production mode.
-Asanalternative,ifseekingbetterdevelopmentperformanceandcssoutputsthatmimicproduction.[extract-css-chunks-webpack-plugin](https://github.com/faceyspacey/extract-css-chunks-webpack-plugin) offers a hot module reload friendly, extended version of mini-css-extract-plugin. HMR real CSS files in dev, works like mini-css in non-dev
// Run `postcss-loader` on each CSS `@import` and CSS modules/ICSS imports, do not forget that `sass-loader` compile non CSS `@import`'s into a single file
// If you need run `sass-loader` and `postcss-loader` on each CSS `@import` please set it to `2`
// More information here https://webpack.js.org/guides/asset-modules/
type:"asset",
},
],
},
};
```
###ResolveunresolvedURLsusinganalias
**index.css**
```css
.class{
background:url(/assets/unresolved/img.png);
}
```
**webpack.config.js**
```js
module.exports={
module:{
rules:[
{
test:/\.css$/i,
use:["style-loader","css-loader"],
},
],
},
resolve:{
alias:{
"/assets/unresolved/img.png":path.resolve(
__dirname,
"assets/real-path-to-img/img.png"
),
},
},
};
```
###Namedexportwithcustomexportnames
**webpack.config.js**
```js
module.exports={
module:{
rules:[
{
test:/\.css$/i,
loader:"css-loader",
options:{
modules:{
namedExport:true,
exportLocalsConvention:function(name){
returnname.replace(/-/g,"_");
},
},
},
},
],
},
};
```
###Separating`Interoperable CSS`-only and `CSS Module`features
Thefollowingsetupisanexampleofallowing`Interoperable CSS` features only (such as `:import` and `:export`) without using further `CSS Module` functionality by setting `mode` option for all files that do not match `*.module.scss` naming convention. This is for reference as having `ICSS` features applied to all files was default `css-loader`behaviorbeforev4.
Meanwhileallfilesmatching`*.module.scss` are treated as `CSS Modules`inthisexample.