Skip to content

Commit 1bca7de

Browse files
committed
docs for CSS modules
1 parent 75ca421 commit 1bca7de

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

docs/en/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- Features
55
- [ES2015](features/es2015.md)
66
- [Scoped CSS](features/scoped-css.md)
7+
- [CSS Modules](features/css-modules.md)
78
- [PostCSS](features/postcss.md)
89
- [Hot Reload](features/hot-reload.md)
910
- Configurations

docs/en/features/css-modules.md

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# CSS Modules
2+
3+
> requires ^9.8.0
4+
5+
[CSS Modules](https://github.com/css-modules/css-modules) is a popular system for modularizing and composing CSS. `vue-loader` provides first-class integration with CSS Modules as an alternative for simulated scoped CSS.
6+
7+
### Usage
8+
9+
Just add the `module` attribute to your `<style>`:
10+
11+
``` html
12+
<style module>
13+
.red {
14+
color: red;
15+
}
16+
</style>
17+
```
18+
19+
This will turn on CSS Modules mode for `css-loader`, and the resulting class identifier object will be injected into the component as a computed property with the name `$style`. To use it in your templates, you need to use a dynamic class binding:
20+
21+
``` html
22+
<template>
23+
<p :class="$style.red">
24+
This should be red
25+
</p>
26+
</template>
27+
```
28+
29+
Since it's a computed property, you can also access it from JavaScript:
30+
31+
``` js
32+
<script>
33+
export default {
34+
created () {
35+
console.log(this.$style.red) // "_1VyoJ-uZOjlOxP7jWUy19_0"
36+
}
37+
}
38+
</script>
39+
```
40+
41+
Refer to the [CSS Modules spec](https://github.com/css-modules/css-modules) for mode details such as [global exceptions](https://github.com/css-modules/css-modules#exceptions) and [composition](https://github.com/css-modules/css-modules#composition).
42+
43+
### Custom Inject Name
44+
45+
You can have more than one `<style>` tags in a single `*.vue` component. To avoid injected styles to overwrite each other, you can customize the name of the injected computed property by giving the `module` attribute a value:
46+
47+
``` html
48+
<style module="a">
49+
/* identifiers injected as $a */
50+
</style>
51+
52+
<style module="b">
53+
/* identifiers injected as $b */
54+
</style>
55+
```
56+
57+
### Configuring Local Indentifier Name
58+
59+
By default, class names are transformed unique identifiers in the form of `[hash:base64]`, but you can configure it by providing the `cssModules.localIdentName` option to `vue-loader`:
60+
61+
``` js
62+
// wepback 1
63+
vue: {
64+
cssModules: {
65+
localIdentName: '[path][name]---[local]---[hash:base64:5]'
66+
}
67+
}
68+
69+
// webpack 2
70+
module: {
71+
rules: [
72+
{
73+
test: '\.vue$',
74+
loader: 'vue',
75+
options: {
76+
cssModules: {
77+
localIdentName: '[path][name]---[local]---[hash:base64:5]'
78+
}
79+
}
80+
}
81+
]
82+
}
83+
```

0 commit comments

Comments
 (0)