{"id":13493853,"url":"https://github.com/webpack-contrib/css-loader","last_synced_at":"2025-05-13T10:54:36.366Z","repository":{"id":560580,"uuid":"3954884","full_name":"webpack-contrib/css-loader","owner":"webpack-contrib","description":"CSS Loader","archived":false,"fork":false,"pushed_at":"2025-02-27T10:16:12.000Z","size":28358,"stargazers_count":4321,"open_issues_count":17,"forks_count":608,"subscribers_count":58,"default_branch":"master","last_synced_at":"2025-04-27T05:38:27.043Z","etag":null,"topics":["css","loader","webpack","webpack-loader"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/webpack-contrib.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"open_collective":"webpack"}},"created_at":"2012-04-07T01:04:57.000Z","updated_at":"2025-04-19T17:08:03.000Z","dependencies_parsed_at":"2024-03-02T14:29:00.706Z","dependency_job_id":"28359a38-3c63-41ad-b507-6c16e2dd3630","html_url":"https://github.com/webpack-contrib/css-loader","commit_stats":{"total_commits":812,"total_committers":132,"mean_commits":6.151515151515151,"dds":0.7315270935960592,"last_synced_commit":"9cf9cd877f934af4aef91f5aaf127467d4964b8e"},"previous_names":["webpack/css-loader"],"tags_count":123,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webpack-contrib%2Fcss-loader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webpack-contrib%2Fcss-loader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webpack-contrib%2Fcss-loader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webpack-contrib%2Fcss-loader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/webpack-contrib","download_url":"https://codeload.github.com/webpack-contrib/css-loader/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251171346,"owners_count":21547094,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["css","loader","webpack","webpack-loader"],"created_at":"2024-07-31T19:01:19.394Z","updated_at":"2025-04-28T10:25:40.402Z","avatar_url":"https://github.com/webpack-contrib.png","language":"JavaScript","readme":"\u003cdiv align=\"center\"\u003e\n  \u003cimg width=\"180\" height=\"180\" vspace=\"20\"\n    src=\"https://cdn.worldvectorlogo.com/logos/css-3.svg\"\u003e\n  \u003ca href=\"https://github.com/webpack/webpack\"\u003e\n    \u003cimg width=\"200\" height=\"200\"\n      src=\"https://webpack.js.org/assets/icon-square-big.svg\"\u003e\n  \u003c/a\u003e\n\u003c/div\u003e\n\n[![npm][npm]][npm-url]\n[![node][node]][node-url]\n[![tests][tests]][tests-url]\n[![coverage][cover]][cover-url]\n[![discussion][discussion]][discussion-url]\n[![size][size]][size-url]\n\n# css-loader\n\nThe `css-loader` interprets `@import` and `url()` like `import/require()` and will resolve them.\n\n## Getting Started\n\n\u003e [!WARNING]\n\u003e\n\u003e To use the latest version of css-loader, webpack@5 is required\n\nTo begin, you'll need to install `css-loader`:\n\n```console\nnpm install --save-dev css-loader\n```\n\nor\n\n```console\nyarn add -D css-loader\n```\n\nor\n\n```console\npnpm add -D css-loader\n```\n\nThen add the plugin to your `webpack` config. For example:\n\n**file.js**\n\n```js\nimport * as css from \"file.css\";\n```\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        use: [\"style-loader\", \"css-loader\"],\n      },\n    ],\n  },\n};\n```\n\nAnd run `webpack` via your preferred method.\n\nIf, for one reason or another, you need to extract CSS as a file (i.e. do not store CSS in a JS module) you might want to check out the [recommend example](https://github.com/webpack-contrib/css-loader#recommend).\n\n## Options\n\n- **[`url`](#url)**\n- **[`import`](#import)**\n- **[`modules`](#modules)**\n- **[`sourceMap`](#sourcemap)**\n- **[`importLoaders`](#importloaders)**\n- **[`esModule`](#esmodule)**\n- **[`exportType`](#exporttype)**\n\n### `url`\n\nType:\n\n```ts\ntype url =\n  | boolean\n  | {\n      filter: (url: string, resourcePath: string) =\u003e boolean;\n    };\n```\n\nDefault: `true`\n\nAllow to enable/disables handling the CSS functions `url` and `image-set`.\nIf set to `false`, `css-loader` will not parse any paths specified in `url` or `image-set`.\nA function can also be passed to control this behavior dynamically based on the path to the asset.\nStarting with version [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.\n\nExamples resolutions:\n\n```js\nurl(image.png) =\u003e require('./image.png')\nurl('image.png') =\u003e require('./image.png')\nurl(./image.png) =\u003e require('./image.png')\nurl('./image.png') =\u003e require('./image.png')\nurl('http://dontwritehorriblecode.com/2112.png') =\u003e require('http://dontwritehorriblecode.com/2112.png')\nimage-set(url('image2x.png') 1x, url('image1x.png') 2x) =\u003e require('./image1x.png') and require('./image2x.png')\n```\n\nTo import assets from a `node_modules` path (include `resolve.modules`) and for `alias`, prefix it with a `~`:\n\n```js\nurl(~module/image.png) =\u003e require('module/image.png')\nurl('~module/image.png') =\u003e require('module/image.png')\nurl(~aliasDirectory/image.png) =\u003e require('otherDirectory/image.png')\n```\n\n#### `boolean`\n\nEnable/disable `url()` resolving.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        loader: \"css-loader\",\n        options: {\n          url: true,\n        },\n      },\n    ],\n  },\n};\n```\n\n#### `object`\n\nAllow to filter `url()`. All filtered `url()` will not be resolved (left in the code as they were written).\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        loader: \"css-loader\",\n        options: {\n          url: {\n            filter: (url, resourcePath) =\u003e {\n              // resourcePath - path to css file\n\n              // Don't handle `img.png` urls\n              if (url.includes(\"img.png\")) {\n                return false;\n              }\n\n              // Don't handle images under root-relative /external_images/\n              if (/^\\/external_images\\//.test(url)) {\n                return false;\n              }\n\n              return true;\n            },\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\n### `import`\n\nType:\n\n\u003c!-- use other name to prettify since import is reserved keyword --\u003e\n\n```ts\ntype importFn =\n  | boolean\n  | {\n      filter: (\n        url: string,\n        media: string,\n        resourcePath: string,\n        supports?: string,\n        layer?: string,\n      ) =\u003e boolean;\n    };\n```\n\nDefault: `true`\n\nAllows to enables/disables `@import` at-rules handling.\nControl `@import` resolving. Absolute urls in `@import` will be moved in runtime code.\n\nExamples resolutions:\n\n```\n@import 'style.css' =\u003e require('./style.css')\n@import url(style.css) =\u003e require('./style.css')\n@import url('style.css') =\u003e require('./style.css')\n@import './style.css' =\u003e require('./style.css')\n@import url(./style.css) =\u003e require('./style.css')\n@import url('./style.css') =\u003e require('./style.css')\n@import url('http://dontwritehorriblecode.com/style.css') =\u003e @import url('http://dontwritehorriblecode.com/style.css') in runtime\n```\n\nTo import styles from a `node_modules` path (include `resolve.modules`) and for `alias`, prefix it with a `~`:\n\n```\n@import url(~module/style.css) =\u003e require('module/style.css')\n@import url('~module/style.css') =\u003e require('module/style.css')\n@import url(~aliasDirectory/style.css) =\u003e require('otherDirectory/style.css')\n```\n\n#### `boolean`\n\nEnable/disable `@import` resolving.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        loader: \"css-loader\",\n        options: {\n          import: true,\n        },\n      },\n    ],\n  },\n};\n```\n\n#### `object`\n\n##### `filter`\n\nType:\n\n```ts\ntype filter = (url: string, media: string, resourcePath: string) =\u003e boolean;\n```\n\nDefault: `undefined`\n\nAllow to filter `@import`. All filtered `@import` will not be resolved (left in the code as they were written).\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        loader: \"css-loader\",\n        options: {\n          import: {\n            filter: (url, media, resourcePath) =\u003e {\n              // resourcePath - path to css file\n\n              // Don't handle `style.css` import\n              if (url.includes(\"style.css\")) {\n                return false;\n              }\n\n              return true;\n            },\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\n### `modules`\n\nType:\n\n```ts\ntype modules =\n  | boolean\n  | \"local\"\n  | \"global\"\n  | \"pure\"\n  | \"icss\"\n  | {\n      auto: boolean | regExp | ((resourcePath: string) =\u003e boolean);\n      mode:\n        | \"local\"\n        | \"global\"\n        | \"pure\"\n        | \"icss\"\n        | ((resourcePath) =\u003e \"local\" | \"global\" | \"pure\" | \"icss\");\n      localIdentName: string;\n      localIdentContext: string;\n      localIdentHashSalt: string;\n      localIdentHashFunction: string;\n      localIdentHashDigest: string;\n      localIdentRegExp: string | regExp;\n      getLocalIdent: (\n        context: LoaderContext,\n        localIdentName: string,\n        localName: string,\n      ) =\u003e string;\n      namedExport: boolean;\n      exportGlobals: boolean;\n      exportLocalsConvention:\n        | \"as-is\"\n        | \"camel-case\"\n        | \"camel-case-only\"\n        | \"dashes\"\n        | \"dashes-only\"\n        | ((name: string) =\u003e string);\n      exportOnlyLocals: boolean;\n      getJSON: ({\n        resourcePath,\n        imports,\n        exports,\n        replacements,\n      }: {\n        resourcePath: string;\n        imports: object[];\n        exports: object[];\n        replacements: object[];\n      }) =\u003e Promise\u003cvoid\u003e | void;\n    };\n```\n\nDefault: `undefined`\n\nAllows to enable/disable CSS Modules or ICSS and setup configuration:\n\n- `undefined` - enable CSS modules for all files matching `/\\.module\\.\\w+$/i.test(filename)` and `/\\.icss\\.\\w+$/i.test(filename)` regexp.\n- `true` - enable CSS modules for all files.\n- `false` - disables CSS Modules for all files.\n- `string` - disables CSS Modules for all files and set the `mode` option, more information you can read [here](https://github.com/webpack-contrib/css-loader#mode)\n- `object` - enable CSS modules for all files, if `modules.auto` option is not specified, otherwise the `modules.auto` option will determine whether if it is CSS modules or not, more information you can read [here](https://github.com/webpack-contrib/css-loader#auto)\n\nThe `modules` option enables/disables the **[CSS Modules](https://github.com/css-modules/css-modules)** specification and setup basic behaviour.\n\nUsing `false` value increase performance because we avoid parsing **CSS Modules** features, it will be useful for developers who use vanilla css or use other technologies.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        loader: \"css-loader\",\n        options: {\n          modules: true,\n        },\n      },\n    ],\n  },\n};\n```\n\n#### `Features`\n\n##### `Scope`\n\nUsing `local` value requires you to specify `:global` classes.\nUsing `global` value requires you to specify `:local` classes.\nUsing `pure` value requires selectors must contain at least one local class or id.\n\nYou can find more information [here](https://github.com/css-modules/css-modules).\n\nStyles can be locally scoped to avoid globally scoping styles.\n\nThe syntax `:local(.className)` can be used to declare `className` in the local scope. The local identifiers are exported by the module.\n\nWith `:local` (without brackets) local mode can be switched on for this selector.\nThe `:global(.className)` notation can be used to declare an explicit global selector.\nWith `:global` (without brackets) global mode can be switched on for this selector.\n\nThe loader replaces local selectors with unique identifiers. The chosen unique identifiers are exported by the module.\n\n```css\n:local(.className) {\n  background: red;\n}\n:local .className {\n  color: green;\n}\n:local(.className .subClass) {\n  color: green;\n}\n:local .className .subClass :global(.global-class-name) {\n  color: blue;\n}\n```\n\n```css\n._23_aKvs-b8bW2Vg3fwHozO {\n  background: red;\n}\n._23_aKvs-b8bW2Vg3fwHozO {\n  color: green;\n}\n._23_aKvs-b8bW2Vg3fwHozO ._13LGdX8RMStbBE9w-t0gZ1 {\n  color: green;\n}\n._23_aKvs-b8bW2Vg3fwHozO ._13LGdX8RMStbBE9w-t0gZ1 .global-class-name {\n  color: blue;\n}\n```\n\n\u003e [!NOTE]\n\u003e\n\u003e Identifiers are exported\n\n```js\nexports.locals = {\n  className: \"_23_aKvs-b8bW2Vg3fwHozO\",\n  subClass: \"_13LGdX8RMStbBE9w-t0gZ1\",\n};\n```\n\nCamelCase is recommended for local selectors. They are easier to use within the imported JS module.\n\nYou can use `:local(#someId)`, but this is not recommended. Use classes instead of ids.\n\n##### `Composing`\n\nWhen declaring a local classname you can compose a local class from another local classname.\n\n```css\n:local(.className) {\n  background: red;\n  color: yellow;\n}\n\n:local(.subClass) {\n  composes: className;\n  background: blue;\n}\n```\n\nThis doesn't result in any change to the CSS itself but exports multiple classnames.\n\n```js\nexports.locals = {\n  className: \"_23_aKvs-b8bW2Vg3fwHozO\",\n  subClass: \"_13LGdX8RMStbBE9w-t0gZ1 _23_aKvs-b8bW2Vg3fwHozO\",\n};\n```\n\n```css\n._23_aKvs-b8bW2Vg3fwHozO {\n  background: red;\n  color: yellow;\n}\n\n._13LGdX8RMStbBE9w-t0gZ1 {\n  background: blue;\n}\n```\n\n##### `Importing`\n\nTo import a local classname from another module.\n\n\u003e [!NOTE]\n\u003e\n\u003e We strongly recommend that you specify the extension when importing a file, since it is possible to import a file with any extension and it is not known in advance which file to use.\n\n```css\n:local(.continueButton) {\n  composes: button from \"library/button.css\";\n  background: red;\n}\n```\n\n```css\n:local(.nameEdit) {\n  composes: edit highlight from \"./edit.css\";\n  background: red;\n}\n```\n\nTo import from multiple modules use multiple `composes:` rules.\n\n```css\n:local(.className) {\n  composes:\n    edit highlight from \"./edit.css\",\n    button from \"module/button.css\",\n    classFromThisModule;\n  background: red;\n}\n```\n\nor\n\n```css\n:local(.className) {\n  composes: edit highlight from \"./edit.css\";\n  composes: button from \"module/button.css\";\n  composes: classFromThisModule;\n  background: red;\n}\n```\n\n##### `Values`\n\nYou can use `@value` to specific values to be reused throughout a document.\n\nWe recommend use prefix `v-` for values, `s-` for selectors and `m-` for media at-rules.\n\n```css\n@value v-primary: #BF4040;\n@value s-black: black-selector;\n@value m-large: (min-width: 960px);\n\n.header {\n  color: v-primary;\n  padding: 0 10px;\n}\n\n.s-black {\n  color: black;\n}\n\n@media m-large {\n  .header {\n    padding: 0 20px;\n  }\n}\n```\n\n#### `boolean`\n\nEnable **CSS Modules** features.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        loader: \"css-loader\",\n        options: {\n          modules: true,\n        },\n      },\n    ],\n  },\n};\n```\n\n#### `string`\n\nEnable **CSS Modules** features and setup `mode`.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        loader: \"css-loader\",\n        options: {\n          // Using `local` value has same effect like using `modules: true`\n          modules: \"global\",\n        },\n      },\n    ],\n  },\n};\n```\n\n#### `object`\n\nEnable **CSS Modules** features and setup options for them.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        loader: \"css-loader\",\n        options: {\n          modules: {\n            mode: \"local\",\n            auto: true,\n            exportGlobals: true,\n            localIdentName: \"[path][name]__[local]--[hash:base64:5]\",\n            localIdentContext: path.resolve(__dirname, \"src\"),\n            localIdentHashSalt: \"my-custom-hash\",\n            namedExport: true,\n            exportLocalsConvention: \"as-is\",\n            exportOnlyLocals: false,\n            getJSON: ({ resourcePath, imports, exports, replacements }) =\u003e {},\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\n##### `auto`\n\nType:\n\n```ts\ntype auto =\n  | boolean\n  | regExp\n  | ((\n      resourcePath: string,\n      resourceQuery: string,\n      resourceFragment: string,\n    ) =\u003e boolean);\n```\n\nDefault: `undefined`\n\nAllows auto enable CSS modules/ICSS based on the filename, query or fragment when `modules` option is object.\n\nPossible values:\n\n- `undefined` - enable CSS modules for all files.\n- `true` - enable CSS modules for all files matching `/\\.module\\.\\w+$/i.test(filename)` and `/\\.icss\\.\\w+$/i.test(filename)` regexp.\n- `false` - disables CSS Modules.\n- `RegExp` - enable CSS modules for all files matching `/RegExp/i.test(filename)` regexp.\n- `function` - enable CSS Modules for files based on the filename satisfying your filter function check.\n\n###### `boolean`\n\nPossible values:\n\n- `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\n- `false` - disables CSS modules or interoperable CSS format based on filename\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        loader: \"css-loader\",\n        options: {\n          modules: {\n            auto: true,\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\n###### `RegExp`\n\nEnable CSS modules for files based on the filename satisfying your regex check.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        loader: \"css-loader\",\n        options: {\n          modules: {\n            auto: /\\.custom-module\\.\\w+$/i,\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\n###### `function`\n\nEnable CSS modules for files based on the filename, query or fragment satisfying your filter function check.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        loader: \"css-loader\",\n        options: {\n          modules: {\n            auto: (resourcePath, resourceQuery, resourceFragment) =\u003e {\n              return resourcePath.endsWith(\".custom-module.css\");\n            },\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\n##### `mode`\n\nType:\n\n```ts\ntype mode =\n  | \"local\"\n  | \"global\"\n  | \"pure\"\n  | \"icss\"\n  | ((\n      resourcePath: string,\n      resourceQuery: string,\n      resourceFragment: string,\n    ) =\u003e \"local\" | \"global\" | \"pure\" | \"icss\");\n```\n\nDefault: `'local'`\n\nSetup `mode` option. You can omit the value when you want `local` mode.\n\nControls the level of compilation applied to the input styles.\n\nThe `local`, `global`, and `pure` handles `class` and `id` scoping and `@value` values.\nThe `icss` will only compile the low level `Interoperable CSS` format for declaring `:import` and `:export` dependencies between CSS and other languages.\n\nICSS underpins CSS Module support, and provides a low level syntax for other tools to implement CSS-module variations of their own.\n\n###### `string`\n\nPossible values - `local`, `global`, `pure`, and `icss`.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        loader: \"css-loader\",\n        options: {\n          modules: {\n            mode: \"global\",\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\n###### `function`\n\nAllows set different values for the `mode` option based on the filename, query or fragment.\n\nPossible return values - `local`, `global`, `pure` and `icss`.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        loader: \"css-loader\",\n        options: {\n          modules: {\n            // Callback must return \"local\", \"global\", or \"pure\" values\n            mode: (resourcePath, resourceQuery, resourceFragment) =\u003e {\n              if (/pure.css$/i.test(resourcePath)) {\n                return \"pure\";\n              }\n\n              if (/global.css$/i.test(resourcePath)) {\n                return \"global\";\n              }\n\n              return \"local\";\n            },\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\n##### `localIdentName`\n\nType:\n\n```ts\ntype localIdentName = string;\n```\n\nDefault: `'[hash:base64]'`\n\nAllows to configure the generated local ident name.\n\nFor more information on options see:\n\n- [webpack template strings](https://webpack.js.org/configuration/output/#template-strings),\n- [output.hashDigest](https://webpack.js.org/configuration/output/#outputhashdigest),\n- [output.hashDigestLength](https://webpack.js.org/configuration/output/#outputhashdigestlength),\n- [output.hashFunction](https://webpack.js.org/configuration/output/#outputhashfunction),\n- [output.hashSalt](https://webpack.js.org/configuration/output/#outputhashsalt).\n\nSupported template strings:\n\n- `[name]` the basename of the resource\n- `[folder]` the folder the resource relative to the `compiler.context` option or `modules.localIdentContext` option.\n- `[path]` the path of the resource relative to the `compiler.context` option or `modules.localIdentContext` option.\n- `[file]` - filename and path.\n- `[ext]` - extension with leading `.`.\n- `[hash]` - the hash of the string, generated based on `localIdentHashSalt`, `localIdentHashFunction`, `localIdentHashDigest`, `localIdentHashDigestLength`, `localIdentContext`, `resourcePath` and `exportName`\n- `[\u003chashFunction\u003e:hash:\u003chashDigest\u003e:\u003chashDigestLength\u003e]` - hash with hash settings.\n- `[local]` - original class.\n\nRecommendations:\n\n- use `'[path][name]__[local]'` for development\n- use `'[hash:base64]'` for production\n\nThe `[local]` placeholder contains original class.\n\n**Note:** all reserved (`\u003c\u003e:\"/\\|?*`) and control filesystem characters (excluding characters in the `[local]` placeholder) will be converted to `-`.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        loader: \"css-loader\",\n        options: {\n          modules: {\n            localIdentName: \"[path][name]__[local]--[hash:base64:5]\",\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\n##### `localIdentContext`\n\nType:\n\n```ts\ntype localIdentContex = string;\n```\n\nDefault: `compiler.context`\n\nAllows to redefine basic loader context for local ident name.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        loader: \"css-loader\",\n        options: {\n          modules: {\n            localIdentContext: path.resolve(__dirname, \"src\"),\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\n##### `localIdentHashSalt`\n\nType:\n\n```ts\ntype localIdentHashSalt = string;\n```\n\nDefault: `undefined`\n\nAllows to add custom hash to generate more unique classes.\nFor more information see [output.hashSalt](https://webpack.js.org/configuration/output/#outputhashsalt).\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        loader: \"css-loader\",\n        options: {\n          modules: {\n            localIdentHashSalt: \"hash\",\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\n##### `localIdentHashFunction`\n\nType:\n\n```ts\ntype localIdentHashFunction = string;\n```\n\nDefault: `md4`\n\nAllows to specify hash function to generate classes .\nFor more information see [output.hashFunction](https://webpack.js.org/configuration/output/#outputhashfunction).\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        loader: \"css-loader\",\n        options: {\n          modules: {\n            localIdentHashFunction: \"md4\",\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\n##### `localIdentHashDigest`\n\nType:\n\n```ts\ntype localIdentHashDigest = string;\n```\n\nDefault: `hex`\n\nAllows to specify hash digest to generate classes.\nFor more information see [output.hashDigest](https://webpack.js.org/configuration/output/#outputhashdigest).\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        loader: \"css-loader\",\n        options: {\n          modules: {\n            localIdentHashDigest: \"base64\",\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\n##### `localIdentHashDigestLength`\n\nType:\n\n```ts\ntype localIdentHashDigestLength = number;\n```\n\nDefault: `20`\n\nAllows to specify hash digest length to generate classes.\nFor more information see [output.hashDigestLength](https://webpack.js.org/configuration/output/#outputhashdigestlength).\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        loader: \"css-loader\",\n        options: {\n          modules: {\n            localIdentHashDigestLength: 5,\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\n##### `hashStrategy`\n\nType: `'resource-path-and-local-name' | 'minimal-subset'`\nDefault: `'resource-path-and-local-name'`\n\nShould local name be used when computing the hash.\n\n- `'resource-path-and-local-name'` Both resource path and local name are used when hashing. Each identifier in a module gets its own hash digest, always.\n- `'minimal-subset'` Auto detect if identifier names can be omitted from hashing. Use this value to optimize the output for better GZIP or Brotli compression.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        loader: \"css-loader\",\n        options: {\n          modules: {\n            hashStrategy: \"minimal-subset\",\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\n##### `localIdentRegExp`\n\nType:\n\n```ts\ntype localIdentRegExp = string | RegExp;\n```\n\nDefault: `undefined`\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        loader: \"css-loader\",\n        options: {\n          modules: {\n            localIdentRegExp: /page-(.*)\\.css/i,\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\n##### `getLocalIdent`\n\nType:\n\n```ts\ntype getLocalIdent = (\n  context: LoaderContext,\n  localIdentName: string,\n  localName: string,\n) =\u003e string;\n```\n\nDefault: `undefined`\n\nAllows to specify a function to generate the classname.\nBy default we use built-in function to generate a classname.\nIf the custom function returns `null` or `undefined`, we fallback to the\nbuilt-in function to generate the classname.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        loader: \"css-loader\",\n        options: {\n          modules: {\n            getLocalIdent: (context, localIdentName, localName, options) =\u003e {\n              return \"whatever_random_class_name\";\n            },\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\n##### `namedExport`\n\nType:\n\n```ts\ntype namedExport = boolean;\n```\n\nDefault: Depends on the value of the `esModule` option. If the value of the `esModule` options is `true`, this value will also be `true`, otherwise it will be `false`.\n\nEnables/disables ES modules named export for locals.\n\n\u003e [!WARNING]\n\u003e\n\u003e Because it is not allowed to use the `default` class in CSS when the `namedExport` is `true` (since ECMA modules have a reserved keyword `default` for default export), it will be automatically renamed to the `_default` class.\n\n**styles.css**\n\n```css\n.foo-baz {\n  color: red;\n}\n.bar {\n  color: blue;\n}\n.default {\n  color: green;\n}\n```\n\n**index.js**\n\n```js\nimport * as styles from \"./styles.css\";\n\n// If using `exportLocalsConvention: \"as-is\"` (default value):\nconsole.log(styles[\"foo-baz\"], styles.bar);\n\n// If using `exportLocalsConvention: \"camel-case-only\"`:\nconsole.log(styles.fooBaz, styles.bar);\n\n// For the `default` classname\nconsole.log(styles[\"_default\"]);\n```\n\nYou can enable a ES module named export using:\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        loader: \"css-loader\",\n        options: {\n          esModule: true,\n          modules: {\n            namedExport: true,\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\nTo set a custom name for namedExport, can use [`exportLocalsConvention`](#exportLocalsConvention) option as a function.\nExample below in the [`examples`](#examples) section.\n\n##### `exportGlobals`\n\nType:\n\n```ts\ntype exportsGLobals = boolean;\n```\n\nDefault: `false`\n\nAllow `css-loader` to export names from global class or id, so you can use that as local name.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        loader: \"css-loader\",\n        options: {\n          modules: {\n            exportGlobals: true,\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\n##### `exportLocalsConvention`\n\nType:\n\n```ts\ntype exportLocalsConvention =\n  | \"as-is\"\n  | \"camel-case\"\n  | \"camel-case-only\"\n  | \"dashes\"\n  | \"dashes-only\"\n  | ((name: string) =\u003e string);\n```\n\nDefault: Depends on the value of the `modules.namedExport` option, if `true` - `as-is`, otherwise `camel-case-only`.\n\n\u003e [!WARNING]\n\u003e\n\u003e Names of locals are converted to camelcase when the named export is `false`, i.e. the `exportLocalsConvention` option has\n\u003e `camelCaseOnly` value by default. You can set this back to any other valid option but selectors\n\u003e which are not valid JavaScript identifiers may run into problems which do not implement the entire modules specification.\n\nStyle of exported class names.\n\n###### `string`\n\nBy default, the exported JSON keys mirror the class names (i.e `as-is` value).\n\n|          Name           |   Type   | Description                                                                                      |\n| :---------------------: | :------: | :----------------------------------------------------------------------------------------------- |\n|      **`'as-is'`**      | `string` | Class names will be exported as is.                                                              |\n|   **`'camel-case'`**    | `string` | Class names will be camelized, the original class name will not to be removed from the locals    |\n| **`'camel-case-only'`** | `string` | Class names will be camelized, the original class name will be removed from the locals           |\n|     **`'dashes'`**      | `string` | Only dashes in class names will be camelized                                                     |\n|   **`'dashes-only'`**   | `string` | Dashes in class names will be camelized, the original class name will be removed from the locals |\n\n**file.css**\n\n```css\n.class-name {\n}\n```\n\n**file.js**\n\n```js\nimport { className } from \"file.css\";\n```\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        loader: \"css-loader\",\n        options: {\n          modules: {\n            exportLocalsConvention: \"camel-case-only\",\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\n###### `function`\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        loader: \"css-loader\",\n        options: {\n          modules: {\n            exportLocalsConvention: function (name) {\n              return name.replace(/-/g, \"_\");\n            },\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        loader: \"css-loader\",\n        options: {\n          modules: {\n            exportLocalsConvention: function (name) {\n              return [\n                name.replace(/-/g, \"_\"),\n                // dashesCamelCase\n                name.replace(/-+(\\w)/g, (match, firstLetter) =\u003e\n                  firstLetter.toUpperCase(),\n                ),\n              ];\n            },\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\n##### `exportOnlyLocals`\n\nType:\n\n```ts\ntype exportOnlyLocals = boolean;\n```\n\nDefault: `false`\n\nExport only locals.\n\n**Useful** when you use **css modules** for pre-rendering (for example SSR).\nFor pre-rendering with `mini-css-extract-plugin` you should use this option instead of `style-loader!css-loader` **in the pre-rendering bundle**.\nIt doesn't embed CSS but only exports the identifier mappings.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        loader: \"css-loader\",\n        options: {\n          modules: {\n            exportOnlyLocals: true,\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\n##### `getJSON`\n\nType:\n\n```ts\ntype getJSON = ({\n  resourcePath,\n  imports,\n  exports,\n  replacements,\n}: {\n  resourcePath: string;\n  imports: object[];\n  exports: object[];\n  replacements: object[];\n}) =\u003e Promise\u003cvoid\u003e | void;\n```\n\nDefault: `undefined`\n\nEnables a callback to output the CSS modules mapping JSON. The callback is invoked with an object containing the following:\n\n- `resourcePath`: the absolute path of the original resource, e.g., `/foo/bar/baz.module.css`\n\n- `imports`: an array of import objects with data about import types and file paths, e.g.,\n\n```json\n[\n  {\n    \"type\": \"icss_import\",\n    \"importName\": \"___CSS_LOADER_ICSS_IMPORT_0___\",\n    \"url\": \"\\\"-!../../../../../node_modules/css-loader/dist/cjs.js??ruleSet[1].rules[4].use[1]!../../../../../node_modules/postcss-loader/dist/cjs.js!../../../../../node_modules/sass-loader/dist/cjs.js!../../../../baz.module.css\\\"\",\n    \"icss\": true,\n    \"index\": 0\n  }\n]\n```\n\n(Note that this will include all imports, not just those relevant to CSS modules.)\n\n- `exports`: an array of export objects with exported names and values, e.g.,\n\n```json\n[\n  {\n    \"name\": \"main\",\n    \"value\": \"D2Oy\"\n  }\n]\n```\n\n- `replacements`: an array of import replacement objects used for linking `imports` and `exports`, e.g.,\n\n```json\n{\n  \"replacementName\": \"___CSS_LOADER_ICSS_IMPORT_0_REPLACEMENT_0___\",\n  \"importName\": \"___CSS_LOADER_ICSS_IMPORT_0___\",\n  \"localName\": \"main\"\n}\n```\n\nUsing `getJSON`, it's possible to output a files with all CSS module mappings.\nIn the following example, we use `getJSON` to cache canonical mappings and\nadd stand-ins for any composed values (through `composes`), and we use a custom plugin\nto consolidate the values and output them to a file:\n\n**webpack.config.js**\n\n```js\nconst path = require(\"path\");\nconst fs = require(\"fs\");\n\nconst CSS_LOADER_REPLACEMENT_REGEX =\n  /(___CSS_LOADER_ICSS_IMPORT_\\d+_REPLACEMENT_\\d+___)/g;\nconst REPLACEMENT_REGEX = /___REPLACEMENT\\[(.*?)]\\[(.*?)]___/g;\nconst IDENTIFIER_REGEX = /\\[(.*?)]\\[(.*?)]/;\nconst replacementsMap = {};\nconst canonicalValuesMap = {};\nconst allExportsJson = {};\n\nfunction generateIdentifier(resourcePath, localName) {\n  return `[${resourcePath}][${localName}]`;\n}\n\nfunction addReplacements(resourcePath, imports, exportsJson, replacements) {\n  const importReplacementsMap = {};\n\n  // create a dict to quickly identify imports and get their absolute stand-in strings in the currently loaded file\n  // e.g., { '___CSS_LOADER_ICSS_IMPORT_0_REPLACEMENT_0___': '___REPLACEMENT[/foo/bar/baz.css][main]___' }\n  importReplacementsMap[resourcePath] = replacements.reduce(\n    (acc, { replacementName, importName, localName }) =\u003e {\n      const replacementImportUrl = imports.find(\n        (importData) =\u003e importData.importName === importName,\n      ).url;\n      const relativePathRe = /.*!(.*)\"/;\n      const [, relativePath] = replacementImportUrl.match(relativePathRe);\n      const importPath = path.resolve(path.dirname(resourcePath), relativePath);\n      const identifier = generateIdentifier(importPath, localName);\n      return { ...acc, [replacementName]: `___REPLACEMENT${identifier}___` };\n    },\n    {},\n  );\n\n  // iterate through the raw exports and add stand-in variables\n  // ('___REPLACEMENT[\u003cabsolute_path\u003e][\u003cclass_name\u003e]___')\n  // to be replaced in the plugin below\n  for (const [localName, classNames] of Object.entries(exportsJson)) {\n    const identifier = generateIdentifier(resourcePath, localName);\n\n    if (CSS_LOADER_REPLACEMENT_REGEX.test(classNames)) {\n      // if there are any replacements needed in the concatenated class names,\n      // add them all to the replacements map to be replaced altogether later\n      replacementsMap[identifier] = classNames.replaceAll(\n        CSS_LOADER_REPLACEMENT_REGEX,\n        (_, replacementName) =\u003e\n          importReplacementsMap[resourcePath][replacementName],\n      );\n    } else {\n      // otherwise, no class names need replacements so we can add them to\n      // canonical values map and all exports JSON verbatim\n      canonicalValuesMap[identifier] = classNames;\n\n      allExportsJson[resourcePath] = allExportsJson[resourcePath] || {};\n      allExportsJson[resourcePath][localName] = classNames;\n    }\n  }\n}\n\nfunction replaceReplacements(classNames) {\n  return classNames.replaceAll(\n    REPLACEMENT_REGEX,\n    (_, resourcePath, localName) =\u003e {\n      const identifier = generateIdentifier(resourcePath, localName);\n\n      if (identifier in canonicalValuesMap) {\n        return canonicalValuesMap[identifier];\n      }\n\n      // Recurse through other stand-in that may be imports\n      const canonicalValue = replaceReplacements(replacementsMap[identifier]);\n\n      canonicalValuesMap[identifier] = canonicalValue;\n\n      return canonicalValue;\n    },\n  );\n}\n\nfunction getJSON({ resourcePath, imports, exports, replacements }) {\n  const exportsJson = exports.reduce((acc, { name, value }) =\u003e {\n    return { ...acc, [name]: value };\n  }, {});\n\n  if (replacements.length \u003e 0) {\n    // replacements present --\u003e add stand-in values for absolute paths and local names,\n    // which will be resolved to their canonical values in the plugin below\n    addReplacements(resourcePath, imports, exportsJson, replacements);\n  } else {\n    // no replacements present --\u003e add to canonicalValuesMap verbatim\n    // since all values here are canonical/don't need resolution\n    for (const [key, value] of Object.entries(exportsJson)) {\n      const id = `[${resourcePath}][${key}]`;\n\n      canonicalValuesMap[id] = value;\n    }\n\n    allExportsJson[resourcePath] = exportsJson;\n  }\n}\n\nclass CssModulesJsonPlugin {\n  constructor(options) {\n    this.options = options;\n  }\n\n  // eslint-disable-next-line class-methods-use-this\n  apply(compiler) {\n    compiler.hooks.emit.tap(\"CssModulesJsonPlugin\", () =\u003e {\n      for (const [identifier, classNames] of Object.entries(replacementsMap)) {\n        const adjustedClassNames = replaceReplacements(classNames);\n\n        replacementsMap[identifier] = adjustedClassNames;\n\n        const [, resourcePath, localName] = identifier.match(IDENTIFIER_REGEX);\n\n        allExportsJson[resourcePath] = allExportsJson[resourcePath] || {};\n        allExportsJson[resourcePath][localName] = adjustedClassNames;\n      }\n\n      fs.writeFileSync(\n        this.options.filepath,\n        JSON.stringify(\n          // Make path to be relative to `context` (your project root)\n          Object.fromEntries(\n            Object.entries(allExportsJson).map((key) =\u003e {\n              key[0] = path\n                .relative(compiler.context, key[0])\n                .replace(/\\\\/g, \"/\");\n\n              return key;\n            }),\n          ),\n          null,\n          2,\n        ),\n        \"utf8\",\n      );\n    });\n  }\n}\n\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        loader: \"css-loader\",\n        options: { modules: { getJSON } },\n      },\n    ],\n  },\n  plugins: [\n    new CssModulesJsonPlugin({\n      filepath: path.resolve(__dirname, \"./output.css.json\"),\n    }),\n  ],\n};\n```\n\nIn the above, all import aliases are replaced with `___REPLACEMENT[\u003cresourcePath\u003e][\u003clocalName\u003e]___` in `getJSON`, and they're resolved in the custom plugin. All CSS mappings are contained in `allExportsJson`:\n\n```json\n{\n  \"foo/bar/baz.module.css\": {\n    \"main\": \"D2Oy\",\n    \"header\": \"thNN\"\n  },\n  \"foot/bear/bath.module.css\": {\n    \"logo\": \"sqiR\",\n    \"info\": \"XMyI\"\n  }\n}\n```\n\nThis is saved to a local file named `output.css.json`.\n\n### `importLoaders`\n\nType:\n\n```ts\ntype importLoaders = number;\n```\n\nDefault: `0`\n\nAllows to enables/disables or setups number of loaders applied before CSS loader for `@import` at-rules, CSS modules and ICSS imports, i.e. `@import`/`composes`/`@value value from './values.css'`/etc.\n\nThe option `importLoaders` allows you to configure how many loaders before `css-loader` should be applied to `@import`ed resources and CSS modules/ICSS imports.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        use: [\n          \"style-loader\",\n          {\n            loader: \"css-loader\",\n            options: {\n              importLoaders: 2,\n              // 0 =\u003e no loaders (default);\n              // 1 =\u003e postcss-loader;\n              // 2 =\u003e postcss-loader, sass-loader\n            },\n          },\n          \"postcss-loader\",\n          \"sass-loader\",\n        ],\n      },\n    ],\n  },\n};\n```\n\nThis may change in the future when the module system (i. e. webpack) supports loader matching by origin.\n\n### `sourceMap`\n\nType:\n\n```ts\ntype sourceMap = boolean;\n```\n\nDefault: depends on the `compiler.devtool` value\n\nBy default generation of source maps depends on the [`devtool`](https://webpack.js.org/configuration/devtool/) option. All values enable source map generation except `eval` and `false` value.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        loader: \"css-loader\",\n        options: {\n          sourceMap: true,\n        },\n      },\n    ],\n  },\n};\n```\n\n### `esModule`\n\nType:\n\n```ts\ntype esModule = boolean;\n```\n\nDefault: `true`\n\nBy default, `css-loader` generates JS modules that use the ES modules syntax.\nThere are some cases in which using ES modules is beneficial, like in the case of [module concatenation](https://webpack.js.org/plugins/module-concatenation-plugin/) and [tree shaking](https://webpack.js.org/guides/tree-shaking/).\n\nYou can enable a CommonJS modules syntax using:\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        loader: \"css-loader\",\n        options: {\n          esModule: false,\n        },\n      },\n    ],\n  },\n};\n```\n\n### `exportType`\n\nType:\n\n```ts\ntype exportType = \"array\" | \"string\" | \"css-style-sheet\";\n```\n\nDefault: `'array'`\n\nAllows exporting styles as array with modules, string or [constructable stylesheet](https://developers.google.com/web/updates/2019/02/constructable-stylesheets) (i.e. [`CSSStyleSheet`](https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet)).\nDefault value is `'array'`, i.e. loader exports array of modules with specific API which is used in `style-loader` or other.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        assert: { type: \"css\" },\n        loader: \"css-loader\",\n        options: {\n          exportType: \"css-style-sheet\",\n        },\n      },\n    ],\n  },\n};\n```\n\n**src/index.js**\n\n```js\nimport sheet from \"./styles.css\" assert { type: \"css\" };\n\ndocument.adoptedStyleSheets = [sheet];\nshadowRoot.adoptedStyleSheets = [sheet];\n```\n\n#### `'array'`\n\nThe default export is array of modules with specific API which is used in `style-loader` or other.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.(sa|sc|c)ss$/i,\n        use: [\"style-loader\", \"css-loader\", \"postcss-loader\", \"sass-loader\"],\n      },\n    ],\n  },\n};\n```\n\n**src/index.js**\n\n```js\n// `style-loader` applies styles to DOM\nimport \"./styles.css\";\n```\n\n#### `'string'`\n\n\u003e [!WARNING]\n\u003e\n\u003e You should not use [`style-loader`](https://github.com/webpack-contrib/style-loader) or [`mini-css-extract-plugin`](https://github.com/webpack-contrib/mini-css-extract-plugin) with this value.\n\n\u003e [!WARNING]\n\u003e\n\u003e The `esModule` 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).\n\nThe default export is `string`.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.(sa|sc|c)ss$/i,\n        use: [\"css-loader\", \"postcss-loader\", \"sass-loader\"],\n      },\n    ],\n  },\n};\n```\n\n**src/index.js**\n\n```js\nimport sheet from \"./styles.css\";\n\nconsole.log(sheet);\n```\n\n#### `'css-style-sheet'`\n\n\u003e [!WARNING]\n\u003e\n\u003e `@import` rules not yet allowed, more [information](https://web.dev/css-module-scripts/#@import-rules-not-yet-allowed)\n\n\u003e [!WARNING]\n\u003e\n\u003e You don't need [`style-loader`](https://github.com/webpack-contrib/style-loader) anymore, please remove it.\n\n\u003e [!WARNING]\n\u003e\n\u003e The `esModule` 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).\n\n\u003e [!WARNING]\n\u003e\n\u003e Source maps are not currently supported in `Chrome` due [bug](https://bugs.chromium.org/p/chromium/issues/detail?id=1174094\u0026q=CSSStyleSheet%20source%20maps\u0026can=2)\n\nThe default export is a [constructable stylesheet](https://developers.google.com/web/updates/2019/02/constructable-stylesheets) (i.e. [`CSSStyleSheet`](https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet)).\n\nUseful for [custom elements](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements) and shadow DOM.\n\nMore information:\n\n- [Using CSS Module Scripts to import stylesheets](https://web.dev/css-module-scripts/)\n- [Constructable Stylesheets: seamless reusable styles](https://developers.google.com/web/updates/2019/02/constructable-stylesheets)\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        assert: { type: \"css\" },\n        loader: \"css-loader\",\n        options: {\n          exportType: \"css-style-sheet\",\n        },\n      },\n\n      // For Sass/SCSS:\n      //\n      // {\n      //   assert: { type: \"css\" },\n      //   rules: [\n      //     {\n      //       loader: \"css-loader\",\n      //       options: {\n      //         exportType: \"css-style-sheet\",\n      //         // Other options\n      //       },\n      //     },\n      //     {\n      //       loader: \"sass-loader\",\n      //       options: {\n      //         // Other options\n      //       },\n      //     },\n      //   ],\n      // },\n    ],\n  },\n};\n```\n\n**src/index.js**\n\n```js\n// Example for Sass/SCSS:\n// import sheet from \"./styles.scss\" assert { type: \"css\" };\n\n// Example for CSS modules:\n// import sheet, { myClass } from \"./styles.scss\" assert { type: \"css\" };\n\n// Example for CSS:\nimport sheet from \"./styles.css\" assert { type: \"css\" };\n\ndocument.adoptedStyleSheets = [sheet];\nshadowRoot.adoptedStyleSheets = [sheet];\n```\n\nFor migration purposes, you can use the following configuration:\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        oneOf: [\n          {\n            assert: { type: \"css\" },\n            loader: \"css-loader\",\n            options: {\n              exportType: \"css-style-sheet\",\n              // Other options\n            },\n          },\n          {\n            use: [\n              \"style-loader\",\n              {\n                loader: \"css-loader\",\n                options: {\n                  // Other options\n                },\n              },\n            ],\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\n## Examples\n\n### Recommend\n\nFor `production` builds it's recommended to extract the CSS from your bundle being able to use parallel loading of CSS/JS resources later on.\nThis can be achieved by using the [mini-css-extract-plugin](https://github.com/webpack-contrib/mini-css-extract-plugin), because it creates separate css files.\nFor `development` mode (including `webpack-dev-server`) you can use [style-loader](https://github.com/webpack-contrib/style-loader), because it injects CSS into the DOM using multiple `\u003cstyle\u003e\u003c/style\u003e` and works faster.\n\n\u003e [!NOTE]\n\u003e\n\u003e Do not use `style-loader` and `mini-css-extract-plugin` together.\n\n**webpack.config.js**\n\n```js\nconst MiniCssExtractPlugin = require(\"mini-css-extract-plugin\");\nconst devMode = process.env.NODE_ENV !== \"production\";\n\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        // If you enable `experiments.css` or `experiments.futureDefaults`, please uncomment line below\n        // type: \"javascript/auto\",\n        test: /\\.(sa|sc|c)ss$/i,\n        use: [\n          devMode ? \"style-loader\" : MiniCssExtractPlugin.loader,\n          \"css-loader\",\n          \"postcss-loader\",\n          \"sass-loader\",\n        ],\n      },\n    ],\n  },\n  plugins: [].concat(devMode ? [] : [new MiniCssExtractPlugin()]),\n};\n```\n\n### Disable url resolving using the `/* webpackIgnore: true */` comment\n\nWith the help of the `/* webpackIgnore: true */`comment, it is possible to disable sources handling for rules and for individual declarations.\n\n```css\n/* webpackIgnore: true */\n@import url(./basic.css);\n@import /* webpackIgnore: true */ url(./imported.css);\n\n.class {\n  /* Disabled url handling for the all urls in the 'background' declaration */\n  color: red;\n  /* webpackIgnore: true */\n  background: url(\"./url/img.png\"), url(\"./url/img.png\");\n}\n\n.class {\n  /* Disabled url handling for the first url in the 'background' declaration */\n  color: red;\n  background:\n    /* webpackIgnore: true */ url(\"./url/img.png\"), url(\"./url/img.png\");\n}\n\n.class {\n  /* Disabled url handling for the second url in the 'background' declaration */\n  color: red;\n  background:\n    url(\"./url/img.png\"),\n    /* webpackIgnore: true */ url(\"./url/img.png\");\n}\n\n/* prettier-ignore */\n.class {\n  /* Disabled url handling for the second url in the 'background' declaration */\n  color: red;\n  background: url(\"./url/img.png\"),\n    /* webpackIgnore: true */\n    url(\"./url/img.png\");\n}\n\n/* prettier-ignore */\n.class {\n  /* Disabled url handling for third and sixth urls in the 'background-image' declaration */\n  background-image: image-set(\n    url(./url/img.png) 2x,\n    url(./url/img.png) 3x,\n    /* webpackIgnore:  true */ url(./url/img.png) 4x,\n    url(./url/img.png) 5x,\n    url(./url/img.png) 6x,\n    /* webpackIgnore:  true */\n    url(./url/img.png) 7x\n  );\n}\n```\n\n### Assets\n\nThe following `webpack.config.js` can load CSS files, embed small PNG/JPG/GIF/SVG images as well as fonts as [Data URLs](https://tools.ietf.org/html/rfc2397) and copy larger files to the output directory.\n\n**For webpack v5:**\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        use: [\"style-loader\", \"css-loader\"],\n      },\n      {\n        test: /\\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)$/i,\n        // More information here https://webpack.js.org/guides/asset-modules/\n        type: \"asset\",\n      },\n    ],\n  },\n};\n```\n\n### Extract\n\nFor production builds it's recommended to extract the CSS from your bundle being able to use parallel loading of CSS/JS resources later on.\n\n- This can be achieved by using the [mini-css-extract-plugin](https://github.com/webpack-contrib/mini-css-extract-plugin) to extract the CSS when running in production mode.\n\n- As an alternative, if seeking better development performance and css outputs that mimic production. [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\n\n### Pure CSS, CSS modules and PostCSS\n\nWhen you have pure CSS (without CSS modules), CSS modules and PostCSS in your project you can use this setup:\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        // For pure CSS - /\\.css$/i,\n        // For Sass/SCSS - /\\.((c|sa|sc)ss)$/i,\n        // For Less - /\\.((c|le)ss)$/i,\n        test: /\\.((c|sa|sc)ss)$/i,\n        use: [\n          \"style-loader\",\n          {\n            loader: \"css-loader\",\n            options: {\n              // 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\n              // If you need run `sass-loader` and `postcss-loader` on each CSS `@import` please set it to `2`\n              importLoaders: 1,\n            },\n          },\n          {\n            loader: \"postcss-loader\",\n            options: { plugins: () =\u003e [postcssPresetEnv({ stage: 0 })] },\n          },\n          // Can be `less-loader`\n          {\n            loader: \"sass-loader\",\n          },\n        ],\n      },\n      // For webpack v5\n      {\n        test: /\\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)$/i,\n        // More information here https://webpack.js.org/guides/asset-modules/\n        type: \"asset\",\n      },\n    ],\n  },\n};\n```\n\n### Resolve unresolved URLs using an alias\n\n**index.css**\n\n```css\n.class {\n  background: url(/assets/unresolved/img.png);\n}\n```\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        use: [\"style-loader\", \"css-loader\"],\n      },\n    ],\n  },\n  resolve: {\n    alias: {\n      \"/assets/unresolved/img.png\": path.resolve(\n        __dirname,\n        \"assets/real-path-to-img/img.png\",\n      ),\n    },\n  },\n};\n```\n\n### Named export with custom export names\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        loader: \"css-loader\",\n        options: {\n          modules: {\n            namedExport: true,\n            exportLocalsConvention: function (name) {\n              return name.replace(/-/g, \"_\");\n            },\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\n### Separating `Interoperable CSS`-only and `CSS Module` features\n\nThe following setup is an example of allowing `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` behavior before v4.\nMeanwhile all files matching `*.module.scss` are treated as `CSS Modules` in this example.\n\nAn example case is assumed where a project requires canvas drawing variables to be synchronized with CSS - canvas drawing uses the same color (set by color name in JavaScript) as HTML background (set by class name in CSS).\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      // ...\n      // --------\n      // SCSS ALL EXCEPT MODULES\n      {\n        test: /\\.scss$/i,\n        exclude: /\\.module\\.scss$/i,\n        use: [\n          {\n            loader: \"style-loader\",\n          },\n          {\n            loader: \"css-loader\",\n            options: {\n              importLoaders: 1,\n              modules: {\n                mode: \"icss\",\n              },\n            },\n          },\n          {\n            loader: \"sass-loader\",\n          },\n        ],\n      },\n      // --------\n      // SCSS MODULES\n      {\n        test: /\\.module\\.scss$/i,\n        use: [\n          {\n            loader: \"style-loader\",\n          },\n          {\n            loader: \"css-loader\",\n            options: {\n              importLoaders: 1,\n              modules: {\n                mode: \"local\",\n              },\n            },\n          },\n          {\n            loader: \"sass-loader\",\n          },\n        ],\n      },\n      // --------\n      // ...\n    ],\n  },\n};\n```\n\n**variables.scss**\n\nFile treated as `ICSS`-only.\n\n```scss\n$colorBackground: red;\n:export {\n  colorBackgroundCanvas: $colorBackground;\n}\n```\n\n**Component.module.scss**\n\nFile treated as `CSS Module`.\n\n```scss\n@import \"variables.scss\";\n.componentClass {\n  background-color: $colorBackground;\n}\n```\n\n**Component.jsx**\n\nUsing both `CSS Module` functionality as well as SCSS variables directly in JavaScript.\n\n```jsx\nimport * as svars from \"variables.scss\";\nimport * as styles from \"Component.module.scss\";\n\n// Render DOM with CSS modules class name\n// \u003cdiv className={styles.componentClass}\u003e\n//   \u003ccanvas ref={mountsCanvas}/\u003e\n// \u003c/div\u003e\n\n// Somewhere in JavaScript canvas drawing code use the variable directly\n// const ctx = mountsCanvas.current.getContext('2d',{alpha: false});\nctx.fillStyle = `${svars.colorBackgroundCanvas}`;\n```\n\n## Contributing\n\nPlease take a moment to read our contributing guidelines if you haven't yet done so.\n\n[CONTRIBUTING](./.github/CONTRIBUTING.md)\n\n## License\n\n[MIT](./LICENSE)\n\n[npm]: https://img.shields.io/npm/v/css-loader.svg\n[npm-url]: https://npmjs.com/package/css-loader\n[node]: https://img.shields.io/node/v/css-loader.svg\n[node-url]: https://nodejs.org\n[tests]: https://github.com/webpack-contrib/css-loader/workflows/css-loader/badge.svg\n[tests-url]: https://github.com/webpack-contrib/css-loader/actions\n[cover]: https://codecov.io/gh/webpack-contrib/css-loader/branch/master/graph/badge.svg\n[cover-url]: https://codecov.io/gh/webpack-contrib/css-loader\n[discussion]: https://img.shields.io/github/discussions/webpack/webpack\n[discussion-url]: https://github.com/webpack/webpack/discussions\n[size]: https://packagephobia.now.sh/badge?p=css-loader\n[size-url]: https://packagephobia.now.sh/result?p=css-loader\n","funding_links":["https://opencollective.com/webpack"],"categories":["JavaScript","Plugins"],"sub_categories":["Rspack Loaders"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebpack-contrib%2Fcss-loader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwebpack-contrib%2Fcss-loader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebpack-contrib%2Fcss-loader/lists"}