Skip to content

Commit a49e904

Browse files
docs: improve documentation for hashPrefix, context, getLocalIdent (#900)
1 parent c309788 commit a49e904

File tree

1 file changed

+74
-20
lines changed

1 file changed

+74
-20
lines changed

README.md

+74-20
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,19 @@ module.exports = {
109109

110110
## Options
111111

112-
| Name | Type | Default | Description |
113-
| :-----------------------------------------: | :-------------------: | :-------------: | :------------------------------------------ |
114-
| **[`url`](#url)** | `{Boolean\|Function}` | `true` | Enable/Disable `url()` handling |
115-
| **[`import`](#import)** | `{Boolean\/Function}` | `true` | Enable/Disable @import handling |
116-
| **[`modules`](#modules)** | `{Boolean\|String}` | `false` | Enable/Disable CSS Modules and setup mode |
117-
| **[`localIdentName`](#localidentname)** | `{String}` | `[hash:base64]` | Configure the generated ident |
118-
| **[`sourceMap`](#sourcemap)** | `{Boolean}` | `false` | Enable/Disable Sourcemaps |
119-
| **[`camelCase`](#camelcase)** | `{Boolean\|String}` | `false` | Export Classnames in CamelCase |
120-
| **[`importLoaders`](#importloaders)** | `{Number}` | `0` | Number of loaders applied before CSS loader |
121-
| **[`exportOnlyLocals`](#exportonlylocals)** | `{Boolean}` | `false` | Export only locals |
112+
| Name | Type | Default | Description |
113+
| :-----------------------------------------: | :-------------------: | :-------------: | :----------------------------------------------------------------------- |
114+
| **[`url`](#url)** | `{Boolean\|Function}` | `true` | Enable/Disable `url()` handling |
115+
| **[`import`](#import)** | `{Boolean\/Function}` | `true` | Enable/Disable @import handling |
116+
| **[`modules`](#modules)** | `{Boolean\|String}` | `false` | Enable/Disable CSS Modules and setup mode |
117+
| **[`localIdentName`](#localidentname)** | `{String}` | `[hash:base64]` | Configure the generated ident |
118+
| **[`context`](#context)** | `{String}` | `undefined` | Allow to redefine basic loader context for local ident name |
119+
| **[`hashPrefix`](#hashprefix)** | `{String}` | `undefined` | Allow to add custom hash to generate more unique classes |
120+
| **[`getLocalIdent`](#getlocalident)** | `{Function}` | `undefined` | Configure the function to generate classname based on a different schema |
121+
| **[`sourceMap`](#sourcemap)** | `{Boolean}` | `false` | Enable/Disable Sourcemaps |
122+
| **[`camelCase`](#camelcase)** | `{Boolean\|String}` | `false` | Export Classnames in CamelCase |
123+
| **[`importLoaders`](#importloaders)** | `{Number}` | `0` | Number of loaders applied before CSS loader |
124+
| **[`exportOnlyLocals`](#exportonlylocals)** | `{Boolean}` | `false` | Export only locals |
122125

123126
### `url`
124127

@@ -461,7 +464,66 @@ module.exports = {
461464
};
462465
```
463466

467+
### `context`
468+
469+
Type: `String`
470+
Default: `undefined`
471+
472+
Allow to redefine basic loader context for local ident name.
473+
By default we use `rootContext` of loader.
474+
475+
**webpack.config.js**
476+
477+
```js
478+
module.exports = {
479+
module: {
480+
rules: [
481+
{
482+
test: /\.css$/,
483+
loader: 'css-loader',
484+
options: {
485+
modules: true,
486+
context: path.resolve(__dirname, 'context'),
487+
},
488+
},
489+
],
490+
},
491+
};
492+
```
493+
494+
### `hashPrefix`
495+
496+
Type: `String`
497+
Default: `undefined`
498+
499+
Allow to add custom hash to generate more unique classes.
500+
501+
**webpack.config.js**
502+
503+
```js
504+
module.exports = {
505+
module: {
506+
rules: [
507+
{
508+
test: /\.css$/,
509+
loader: 'css-loader',
510+
options: {
511+
modules: true,
512+
hashPrefix: 'hash',
513+
},
514+
},
515+
],
516+
},
517+
};
518+
```
519+
520+
### `getLocalIdent`
521+
522+
Type: `Function`
523+
Default: `undefined`
524+
464525
You can also specify the absolute path to your custom `getLocalIdent` function to generate classname based on a different schema.
526+
By default we use built-in function to generate a classname.
465527

466528
**webpack.config.js**
467529

@@ -474,9 +536,6 @@ module.exports = {
474536
loader: 'css-loader',
475537
options: {
476538
modules: true,
477-
context: path.resolve(__dirname, 'context'), // Allow to redefine basic loader context for `local-ident-name`
478-
hashPrefix: 'hash', // Allow to add custom hash to generate more unique classes
479-
localIdentName: '[path][name]__[local]--[hash:base64:5]',
480539
getLocalIdent: (context, localIdentName, localName, options) => {
481540
return 'whatever_random_class_name';
482541
},
@@ -654,7 +713,8 @@ module.exports = {
654713
### Extract
655714

656715
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.
657-
- 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.
716+
717+
- 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.
658718

659719
- 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
660720

@@ -670,21 +730,15 @@ Please take a moment to read our contributing guidelines if you haven't yet done
670730

671731
[npm]: https://img.shields.io/npm/v/css-loader.svg
672732
[npm-url]: https://npmjs.com/package/css-loader
673-
674733
[node]: https://img.shields.io/node/v/css-loader.svg
675734
[node-url]: https://nodejs.org
676-
677735
[deps]: https://david-dm.org/webpack-contrib/css-loader.svg
678736
[deps-url]: https://david-dm.org/webpack-contrib/css-loader
679-
680737
[tests]: https://img.shields.io/circleci/project/github/webpack-contrib/css-loader.svg
681738
[tests-url]: https://circleci.com/gh/webpack-contrib/css-loader
682-
683739
[cover]: https://codecov.io/gh/webpack-contrib/css-loader/branch/master/graph/badge.svg
684740
[cover-url]: https://codecov.io/gh/webpack-contrib/css-loader
685-
686741
[chat]: https://badges.gitter.im/webpack/webpack.svg
687742
[chat-url]: https://gitter.im/webpack/webpack
688-
689743
[size]: https://packagephobia.now.sh/badge?p=css-loader
690744
[size-url]: https://packagephobia.now.sh/result?p=css-loader

0 commit comments

Comments
 (0)