Skip to content

Commit c153fe6

Browse files
authoredJul 22, 2020
refactor: improve schema options (#1123)
1 parent 58b4b98 commit c153fe6

File tree

3 files changed

+77
-58
lines changed

3 files changed

+77
-58
lines changed
 

‎README.md

+34-34
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ module.exports = {
549549
Type: `Boolean|RegExp|Function`
550550
Default: `'true'`
551551

552-
Allows auto enable css modules based on filename.
552+
Allows auto enable CSS modules based on filename.
553553

554554
###### `Boolean`
555555

@@ -694,39 +694,12 @@ module.exports = {
694694
};
695695
```
696696

697-
##### `exportGlobals`
698-
699-
Type: `Boolean`
700-
Default: `false`
701-
702-
Allow `css-loader` to export names from global class or id, so you can use that as local name.
703-
704-
**webpack.config.js**
705-
706-
```js
707-
module.exports = {
708-
module: {
709-
rules: [
710-
{
711-
test: /\.css$/i,
712-
loader: 'css-loader',
713-
options: {
714-
modules: {
715-
exportGlobals: true,
716-
},
717-
},
718-
},
719-
],
720-
},
721-
};
722-
```
723-
724697
##### `localIdentName`
725698

726699
Type: `String`
727700
Default: `'[hash:base64]'`
728701

729-
You can configure the generated ident with the `localIdentName` option.
702+
Allows to configure the generated local ident name.
730703
See [loader-utils's documentation](https://github.com/webpack/loader-utils#interpolatename) for more information on options.
731704

732705
Recommendations:
@@ -763,7 +736,7 @@ module.exports = {
763736
Type: `String`
764737
Default: `compiler.context`
765738

766-
Allow to redefine basic loader context for local ident name.
739+
Allows to redefine basic loader context for local ident name.
767740

768741
**webpack.config.js**
769742

@@ -790,7 +763,7 @@ module.exports = {
790763
Type: `String`
791764
Default: `undefined`
792765

793-
Allow to add custom hash to generate more unique classes.
766+
Allows to add custom hash to generate more unique classes.
794767

795768
**webpack.config.js**
796769

@@ -842,7 +815,7 @@ module.exports = {
842815
Type: `Function`
843816
Default: `undefined`
844817

845-
You can also specify the absolute path to your custom `getLocalIdent` function to generate classname based on a different schema.
818+
Allows to specify a function to generate the classname.
846819
By default we use built-in function to generate a classname.
847820

848821
**webpack.config.js**
@@ -872,8 +845,8 @@ module.exports = {
872845
Type: `Boolean`
873846
Default: `false`
874847

875-
Enable/disable ES modules named export for css classes.
876-
Names of exported classes are converted to camelCase.
848+
Enables/disables ES modules named export for locals.
849+
Names of locals are converted to camelCase.
877850

878851
> i It is not allowed to use JavaScript reserved words in css class names
879852
@@ -919,6 +892,33 @@ module.exports = {
919892
};
920893
```
921894

895+
##### `exportGlobals`
896+
897+
Type: `Boolean`
898+
Default: `false`
899+
900+
Allow `css-loader` to export names from global class or id, so you can use that as local name.
901+
902+
**webpack.config.js**
903+
904+
```js
905+
module.exports = {
906+
module: {
907+
rules: [
908+
{
909+
test: /\.css$/i,
910+
loader: 'css-loader',
911+
options: {
912+
modules: {
913+
exportGlobals: true,
914+
},
915+
},
916+
},
917+
],
918+
},
919+
};
920+
```
921+
922922
##### `exportlocalsConvention`
923923

924924
Type: `String`

‎src/options.json

+12-4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"additionalProperties": false,
3838
"properties": {
3939
"auto": {
40+
"description": "Allows auto enable CSS modules based on filename (https://github.com/webpack-contrib/css-loader#auto).",
4041
"anyOf": [
4142
{
4243
"instanceof": "RegExp"
@@ -50,6 +51,7 @@
5051
]
5152
},
5253
"mode": {
54+
"description": "Setup `mode` option (https://github.com/webpack-contrib/css-loader#mode).",
5355
"anyOf": [
5456
{
5557
"enum": ["local", "global", "pure"]
@@ -59,22 +61,23 @@
5961
}
6062
]
6163
},
62-
"exportGlobals": {
63-
"type": "boolean"
64-
},
6564
"localIdentName": {
65+
"description": "Allows to configure the generated local ident name (https://github.com/webpack-contrib/css-loader#localidentname).",
6666
"type": "string",
6767
"minLength": 1
6868
},
6969
"localIdentContext": {
70+
"description": "Allows to redefine basic loader context for local ident name (https://github.com/webpack-contrib/css-loader#localidentcontext).",
7071
"type": "string",
7172
"minLength": 1
7273
},
7374
"localIdentHashPrefix": {
75+
"description": "Allows to add custom hash to generate more unique classes (https://github.com/webpack-contrib/css-loader#localidenthashprefix).",
7476
"type": "string",
7577
"minLength": 1
7678
},
7779
"localIdentRegExp": {
80+
"description": "Allows to specify custom RegExp for local ident name (https://github.com/webpack-contrib/css-loader#localidentregexp).",
7881
"anyOf": [
7982
{
8083
"type": "string",
@@ -86,10 +89,15 @@
8689
]
8790
},
8891
"getLocalIdent": {
92+
"description": "Allows to specify a function to generate the classname (https://github.com/webpack-contrib/css-loader#getlocalident).",
8993
"instanceof": "Function"
9094
},
9195
"namedExport": {
92-
"description": "Use the named export ES modules.",
96+
"description": "Enables/disables ES modules named export for locals (https://github.com/webpack-contrib/css-loader#namedexport).",
97+
"type": "boolean"
98+
},
99+
"exportGlobals": {
100+
"description": "Allows to export names from global class or id, so you can use that as local name (https://github.com/webpack-contrib/css-loader#exportglobals).",
93101
"type": "boolean"
94102
},
95103
"exportLocalsConvention": {

‎test/__snapshots__/validate-options.test.js.snap

+31-20
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ exports[`validate options should throw an error on the "importLoaders" option wi
3939
exports[`validate options should throw an error on the "modules" option with "{"auto":"invalid"}" value 1`] = `
4040
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
4141
- options.modules should be one of these:
42-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportLocalsConvention?, exportOnlyLocals? }
42+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
4343
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
4444
Details:
4545
* options.modules.auto should be one of these:
4646
RegExp | function | boolean
47+
-> Allows auto enable CSS modules based on filename (https://github.com/webpack-contrib/css-loader#auto).
4748
Details:
4849
* options.modules.auto should be an instance of RegExp.
4950
* options.modules.auto should be an instance of function.
@@ -52,7 +53,8 @@ exports[`validate options should throw an error on the "modules" option with "{"
5253
5354
exports[`validate options should throw an error on the "modules" option with "{"exportGlobals":"invalid"}" value 1`] = `
5455
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
55-
- options.modules.exportGlobals should be a boolean."
56+
- options.modules.exportGlobals should be a boolean.
57+
-> Allows to export names from global class or id, so you can use that as local name (https://github.com/webpack-contrib/css-loader#exportglobals)."
5658
`;
5759
5860
exports[`validate options should throw an error on the "modules" option with "{"exportLocalsConvention":"unknown"}" value 1`] = `
@@ -70,32 +72,37 @@ exports[`validate options should throw an error on the "modules" option with "{"
7072
7173
exports[`validate options should throw an error on the "modules" option with "{"getLocalIdent":[]}" value 1`] = `
7274
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
73-
- options.modules.getLocalIdent should be an instance of function."
75+
- options.modules.getLocalIdent should be an instance of function.
76+
-> Allows to specify a function to generate the classname (https://github.com/webpack-contrib/css-loader#getlocalident)."
7477
`;
7578
7679
exports[`validate options should throw an error on the "modules" option with "{"localIdentContext":true}" value 1`] = `
7780
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
78-
- options.modules.localIdentContext should be a non-empty string."
81+
- options.modules.localIdentContext should be a non-empty string.
82+
-> Allows to redefine basic loader context for local ident name (https://github.com/webpack-contrib/css-loader#localidentcontext)."
7983
`;
8084
8185
exports[`validate options should throw an error on the "modules" option with "{"localIdentHashPrefix":true}" value 1`] = `
8286
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
83-
- options.modules.localIdentHashPrefix should be a non-empty string."
87+
- options.modules.localIdentHashPrefix should be a non-empty string.
88+
-> Allows to add custom hash to generate more unique classes (https://github.com/webpack-contrib/css-loader#localidenthashprefix)."
8489
`;
8590
8691
exports[`validate options should throw an error on the "modules" option with "{"localIdentName":true}" value 1`] = `
8792
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
88-
- options.modules.localIdentName should be a non-empty string."
93+
- options.modules.localIdentName should be a non-empty string.
94+
-> Allows to configure the generated local ident name (https://github.com/webpack-contrib/css-loader#localidentname)."
8995
`;
9096
9197
exports[`validate options should throw an error on the "modules" option with "{"localIdentRegExp":true}" value 1`] = `
9298
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
9399
- options.modules should be one of these:
94-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportLocalsConvention?, exportOnlyLocals? }
100+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
95101
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
96102
Details:
97103
* options.modules.localIdentRegExp should be one of these:
98104
non-empty string | RegExp
105+
-> Allows to specify custom RegExp for local ident name (https://github.com/webpack-contrib/css-loader#localidentregexp).
99106
Details:
100107
* options.modules.localIdentRegExp should be a non-empty string.
101108
* options.modules.localIdentRegExp should be an instance of RegExp."
@@ -104,11 +111,12 @@ exports[`validate options should throw an error on the "modules" option with "{"
104111
exports[`validate options should throw an error on the "modules" option with "{"mode":"globals"}" value 1`] = `
105112
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
106113
- options.modules should be one of these:
107-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportLocalsConvention?, exportOnlyLocals? }
114+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
108115
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
109116
Details:
110117
* options.modules.mode should be one of these:
111118
\\"local\\" | \\"global\\" | \\"pure\\" | function
119+
-> Setup \`mode\` option (https://github.com/webpack-contrib/css-loader#mode).
112120
Details:
113121
* options.modules.mode should be one of these:
114122
\\"local\\" | \\"global\\" | \\"pure\\"
@@ -118,11 +126,12 @@ exports[`validate options should throw an error on the "modules" option with "{"
118126
exports[`validate options should throw an error on the "modules" option with "{"mode":"locals"}" value 1`] = `
119127
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
120128
- options.modules should be one of these:
121-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportLocalsConvention?, exportOnlyLocals? }
129+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
122130
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
123131
Details:
124132
* options.modules.mode should be one of these:
125133
\\"local\\" | \\"global\\" | \\"pure\\" | function
134+
-> Setup \`mode\` option (https://github.com/webpack-contrib/css-loader#mode).
126135
Details:
127136
* options.modules.mode should be one of these:
128137
\\"local\\" | \\"global\\" | \\"pure\\"
@@ -132,11 +141,12 @@ exports[`validate options should throw an error on the "modules" option with "{"
132141
exports[`validate options should throw an error on the "modules" option with "{"mode":"pures"}" value 1`] = `
133142
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
134143
- options.modules should be one of these:
135-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportLocalsConvention?, exportOnlyLocals? }
144+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
136145
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
137146
Details:
138147
* options.modules.mode should be one of these:
139148
\\"local\\" | \\"global\\" | \\"pure\\" | function
149+
-> Setup \`mode\` option (https://github.com/webpack-contrib/css-loader#mode).
140150
Details:
141151
* options.modules.mode should be one of these:
142152
\\"local\\" | \\"global\\" | \\"pure\\"
@@ -146,11 +156,12 @@ exports[`validate options should throw an error on the "modules" option with "{"
146156
exports[`validate options should throw an error on the "modules" option with "{"mode":true}" value 1`] = `
147157
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
148158
- options.modules should be one of these:
149-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportLocalsConvention?, exportOnlyLocals? }
159+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
150160
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
151161
Details:
152162
* options.modules.mode should be one of these:
153163
\\"local\\" | \\"global\\" | \\"pure\\" | function
164+
-> Setup \`mode\` option (https://github.com/webpack-contrib/css-loader#mode).
154165
Details:
155166
* options.modules.mode should be one of these:
156167
\\"local\\" | \\"global\\" | \\"pure\\"
@@ -160,59 +171,59 @@ exports[`validate options should throw an error on the "modules" option with "{"
160171
exports[`validate options should throw an error on the "modules" option with "{"namedExport":"invalid"}" value 1`] = `
161172
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
162173
- options.modules.namedExport should be a boolean.
163-
-> Use the named export ES modules."
174+
-> Enables/disables ES modules named export for locals (https://github.com/webpack-contrib/css-loader#namedexport)."
164175
`;
165176
166177
exports[`validate options should throw an error on the "modules" option with "globals" value 1`] = `
167178
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
168179
- options.modules should be one of these:
169-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportLocalsConvention?, exportOnlyLocals? }
180+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
170181
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
171182
Details:
172183
* options.modules should be a boolean.
173184
* options.modules should be one of these:
174185
\\"local\\" | \\"global\\" | \\"pure\\"
175186
* options.modules should be an object:
176-
object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportLocalsConvention?, exportOnlyLocals? }"
187+
object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }"
177188
`;
178189
179190
exports[`validate options should throw an error on the "modules" option with "locals" value 1`] = `
180191
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
181192
- options.modules should be one of these:
182-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportLocalsConvention?, exportOnlyLocals? }
193+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
183194
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
184195
Details:
185196
* options.modules should be a boolean.
186197
* options.modules should be one of these:
187198
\\"local\\" | \\"global\\" | \\"pure\\"
188199
* options.modules should be an object:
189-
object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportLocalsConvention?, exportOnlyLocals? }"
200+
object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }"
190201
`;
191202
192203
exports[`validate options should throw an error on the "modules" option with "pures" value 1`] = `
193204
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
194205
- options.modules should be one of these:
195-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportLocalsConvention?, exportOnlyLocals? }
206+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
196207
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
197208
Details:
198209
* options.modules should be a boolean.
199210
* options.modules should be one of these:
200211
\\"local\\" | \\"global\\" | \\"pure\\"
201212
* options.modules should be an object:
202-
object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportLocalsConvention?, exportOnlyLocals? }"
213+
object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }"
203214
`;
204215
205216
exports[`validate options should throw an error on the "modules" option with "true" value 1`] = `
206217
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
207218
- options.modules should be one of these:
208-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportLocalsConvention?, exportOnlyLocals? }
219+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }
209220
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
210221
Details:
211222
* options.modules should be a boolean.
212223
* options.modules should be one of these:
213224
\\"local\\" | \\"global\\" | \\"pure\\"
214225
* options.modules should be an object:
215-
object { auto?, mode?, exportGlobals?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportLocalsConvention?, exportOnlyLocals? }"
226+
object { auto?, mode?, localIdentName?, localIdentContext?, localIdentHashPrefix?, localIdentRegExp?, getLocalIdent?, namedExport?, exportGlobals?, exportLocalsConvention?, exportOnlyLocals? }"
216227
`;
217228
218229
exports[`validate options should throw an error on the "sourceMap" option with "true" value 1`] = `

0 commit comments

Comments
 (0)
Please sign in to comment.