You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/options/css.md
+105Lines changed: 105 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -328,6 +328,94 @@ export function greet() {
328
328
329
329
This is useful for component libraries where you want CSS to be automatically included when users import your components.
330
330
331
+
## CSS Modules
332
+
333
+
Files with the `.module.css` extension (and preprocessor variants like `.module.scss`, `.module.less`, etc.) are treated as [CSS modules](https://github.com/css-modules/css-modules). Class names are automatically scoped and exported as a JavaScript object:
334
+
335
+
```ts
336
+
// src/index.ts
337
+
importstylesfrom'./app.module.css'
338
+
339
+
console.log(styles.title) // "scoped_title_hash"
340
+
```
341
+
342
+
```css
343
+
/* app.module.css */
344
+
.title {
345
+
color: red;
346
+
}
347
+
.content {
348
+
font-size: 14px;
349
+
}
350
+
```
351
+
352
+
The CSS is emitted with scoped class names, and the JS output exports the mapping from original to scoped names.
353
+
354
+
### Configuration
355
+
356
+
Configure CSS modules behavior via `css.modules`:
357
+
358
+
```ts
359
+
exportdefaultdefineConfig({
360
+
css: {
361
+
modules: {
362
+
// Scoping behavior: 'local' (default) or 'global'
363
+
scopeBehaviour: 'local',
364
+
365
+
// Pattern for scoped class names (Lightning CSS pattern syntax)
366
+
generateScopedName: '[hash]_[local]',
367
+
368
+
// Transform class name convention in JS exports
369
+
localsConvention: 'camelCase',
370
+
},
371
+
},
372
+
})
373
+
```
374
+
375
+
Set `css.modules: false` to disable CSS modules entirely — `.module.css` files will be treated as regular CSS.
376
+
377
+
### `localsConvention`
378
+
379
+
Controls how class names are exported in JavaScript:
When using `transformer: 'lightningcss'` (default), this accepts a Lightning CSS [pattern string](https://lightningcss.dev/css-modules.html#custom-naming-conventions) (e.g., `'[hash]_[local]'`).
392
+
393
+
When using `transformer: 'postcss'`, this also accepts a function:
394
+
395
+
```ts
396
+
exportdefaultdefineConfig({
397
+
css: {
398
+
transformer: 'postcss',
399
+
modules: {
400
+
generateScopedName: (name, filename, css) => {
401
+
return`my-lib_${name}`
402
+
},
403
+
},
404
+
},
405
+
})
406
+
```
407
+
408
+
> [!NOTE]
409
+
> Function-form `generateScopedName` is only supported with `transformer: 'postcss'`. The Lightning CSS transformer only supports string patterns.
410
+
411
+
### Optional Dependencies
412
+
413
+
When using `transformer: 'postcss'` with CSS modules, install [`postcss-modules`](https://github.com/css-modules/postcss-modules):
414
+
415
+
```bash
416
+
npm install -D postcss postcss-modules
417
+
```
418
+
331
419
## CSS Code Splitting
332
420
333
421
### Merged Mode (Default)
@@ -372,6 +460,22 @@ dist/
372
460
async-abc123.css ← CSS from async chunk
373
461
```
374
462
463
+
## PostCSS Optional Peer Dependencies
464
+
465
+
When using `transformer: 'postcss'`, the following packages may need to be installed depending on the features you use:
0 commit comments