Skip to content

Commit 2f24ec5

Browse files
authored
docs(module): exportsConvention function form now accepts string[] (#8240)
Webpack 5.107 lets the generator.exportsConvention function for CSS modules return either a string or a string[]. Returning an array exports the local class under every name in the array, matching css-loader's behavior. Updates the type comments in the module.generator example and adds a dedicated subsection with a worked example. Refs: webpack/webpack#20914
1 parent a5ab2ad commit 2f24ec5

1 file changed

Lines changed: 33 additions & 4 deletions

File tree

src/content/configuration/module.mdx

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ export default {
167167
exportsOnly: true,
168168

169169
// Customize how css export names are exported to javascript modules, such as keeping them as is, transforming them to camel case, etc.
170-
// type: 'as-is' | 'camel-case' | 'camel-case-only' | 'dashes' | 'dashes-only' | ((name: string) => string)
171-
// available since webpack 5.90.4
170+
// type: 'as-is' | 'camel-case' | 'camel-case-only' | 'dashes' | 'dashes-only' | ((name: string) => string | string[])
171+
// available since webpack 5.90.4; the function form may return string[] since 5.107.0
172172
exportsConvention: "camel-case-only",
173173
},
174174
"css/auto": {
@@ -179,8 +179,8 @@ export default {
179179
exportsOnly: true,
180180

181181
// Customize how css export names are exported to javascript modules, such as keeping them as is, transforming them to camel case, etc.
182-
// type: 'as-is' | 'camel-case' | 'camel-case-only' | 'dashes' | 'dashes-only' | ((name: string) => string)
183-
// available since webpack 5.90.4
182+
// type: 'as-is' | 'camel-case' | 'camel-case-only' | 'dashes' | 'dashes-only' | ((name: string) => string | string[])
183+
// available since webpack 5.90.4; the function form may return string[] since 5.107.0
184184
exportsConvention: "camel-case-only",
185185

186186
// Customize the format of the local class names generated for css modules.
@@ -205,6 +205,35 @@ export default {
205205
};
206206
```
207207

208+
### Multiple aliases via `exportsConvention`
209+
210+
<Badge text="5.107.0+" />
211+
212+
When `exportsConvention` is a function, it may return either a `string` or a `string[]`. Returning an array exports the local class under every name in the array, matching `css-loader`'s behavior. This is useful when you want to expose the same class under several aliases without writing two rules.
213+
214+
**webpack.config.js**
215+
216+
```js
217+
export default {
218+
experiments: { css: true },
219+
module: {
220+
generator: {
221+
"css/module": {
222+
// expose each class under both its original name and an uppercase alias
223+
exportsConvention: (name) => [name, name.toUpperCase()],
224+
},
225+
},
226+
},
227+
};
228+
```
229+
230+
```js
231+
import styles from "./button.module.css";
232+
233+
console.log(styles.btn); // hashed class
234+
console.log(styles.BTN); // same hashed class, uppercase alias
235+
```
236+
208237
## module.parser
209238

210239
<Badge text="5.12.0+" />

0 commit comments

Comments
 (0)