4
4
<a href="https://www.npmjs.com/package/laravel-mix-vue-css-modules"><img src="https://img.shields.io/npm/v/laravel-mix-vue-css-modules.svg?style=for-the-badge" alt="npm" /></a> <a href="https://www.npmjs.com/package/laravel-mix-vue-css-modules"><img src="https://img.shields.io/npm/dt/laravel-mix-vue-css-modules.svg?style=for-the-badge" alt="npm" /></a>
5
5
</h1 >
6
6
7
- Add support for css module laravel mix.
7
+ Add support for css module laravel mix. ** CSS, SCSS, LESS & STYLUS **
8
8
9
9
## Installation
10
10
11
11
```
12
12
npm i laravel-mix-vue-css-modules
13
13
```
14
14
15
- ## Update v1 to v2
16
-
17
- ```
18
- npm install laravel-mix-vue-css-modules@latest
19
- ```
20
-
21
15
## Usage
22
16
23
17
First, VueCssModules must be enabled. Your ` webpack.mix.js ` could look like this:
@@ -46,7 +40,7 @@ You can then use it in your templates with a dynamic class binding:
46
40
47
41
```vue
48
42
<template >
49
- <p :class="$style .red"> This should be red</p >
43
+ <p :class="$style .red"> This should be red</p >
50
44
</template >
51
45
```
52
46
@@ -60,24 +54,58 @@ If you only want to use CSS Modules in some of your Vue components, you can set
60
54
mix.vueCssModules({ oneOf : true });
61
55
```
62
56
63
- #### Pre-Processors
57
+ #### Custom Injectname
58
+
59
+ ``` js
60
+ < style module = " $cssA" >
61
+ /* identifiers injected as $cssA */
62
+ < / style>
63
+
64
+ < style module = " $cssB" >
65
+ /* identifiers injected as $cssB */
66
+ < / style>
67
+ ```
64
68
65
- CSS Modules can be used along with other pre-processors. default pre-processor is enable. to disable it set ` preProcessor ` to ` false `
69
+ ## Pre-Processors
70
+
71
+ By default all pre-processors are disabled.
72
+
73
+ #### For Scss
66
74
67
75
``` js
68
- mix .vueCssModules ({ preProcessor: false });
76
+ mix .vueCssModules ({ preProcessor: { scss: true } });
77
+ ```
78
+
79
+ #### For Less
80
+
81
+ ` npm i less less-loader --save-dev `
82
+
83
+ then set ` less ` to ` true `
84
+
85
+ ``` js
86
+ mix .vueCssModules ({ preProcessor: { less: true } });
87
+ ```
88
+
89
+ #### For Stylus
90
+
91
+ ` npm i stylus stylus-loader --save-dev `
92
+
93
+ then set ` stylus ` to ` true `
94
+
95
+ ``` js
96
+ mix .vueCssModules ({ preProcessor: { stylus: true } });
69
97
```
70
98
71
99
#### Custom localIdentName
72
100
73
101
Default:
74
102
75
- - ` ' [path][name]__[local]` for development
76
- - ` ' [hash:base64]' ` for production
103
+ - ` [path][name]__[local] ` for development
104
+ - ` [hash:base64] ` for production
77
105
78
106
``` js
79
107
mix .vueCssModules ({
80
- cssLoaderOptions: { localIdentName: " [path][name]__[local]" },
108
+ cssLoaderOptions: { localIdentName: " [path][name]__[local]" }
81
109
});
82
110
```
83
111
@@ -103,6 +131,12 @@ Default: `1`
103
131
mix .vueCssModules ({ cssLoaderOptions: { importLoaders: 2 } });
104
132
```
105
133
134
+ #### Exclude
135
+
136
+ ``` js
137
+ mix .vueCssModules ({ exclude: [path .resolve (__dirname , " node-modules" )] });
138
+ ```
139
+
106
140
#### Exclude css
107
141
108
142
you may want some of your css exluded from generated class by css module.
@@ -111,12 +145,104 @@ you may want some of your css exluded from generated class by css module.
111
145
const getLocalIdent = require (" css-loader/lib/getLocalIdent" );
112
146
113
147
mix .vueCssModules ({
114
- cssLoaderOptions: {
115
- getLocalIdent : (context , localIdentName , localName , options ) => {
116
- return context .resourcePath .includes (" x.scss" )
117
- ? localName
118
- : getLocalIdent (context, localIdentName, localName, options);
119
- },
120
- },
148
+ cssLoaderOptions: {
149
+ getLocalIdent : (context , localIdentName , localName , options ) => {
150
+ return context .resourcePath .includes (" x.scss" )
151
+ ? localName
152
+ : getLocalIdent (context, localIdentName, localName, options);
153
+ }
154
+ }
121
155
});
122
156
```
157
+
158
+ ## Example
159
+
160
+ ``` vue
161
+ <script>
162
+ export default {};
163
+ </script>
164
+
165
+ <template>
166
+ <div>
167
+ <span class="blue">css scoped</span>
168
+
169
+ <span :class="$css.blue">css module</span>
170
+
171
+ <span class="red">scss scoped</span>
172
+
173
+ <span :class="$scss.red">scss module</span>
174
+
175
+ <span class="green">less scoped</span>
176
+
177
+ <span :class="$less.green">less module</span>
178
+
179
+ <span class="indigo">stylus scoped</span>
180
+
181
+ <span :class="$stylus.indigo">stylus module</span>
182
+ </div>
183
+ </template>
184
+
185
+ <style scoped>
186
+ .blue {
187
+ color: blue;
188
+ }
189
+ </style>
190
+
191
+ <style module="$css">
192
+ .blue {
193
+ color: blue;
194
+ }
195
+ </style>
196
+
197
+ <style lang="scss" scoped>
198
+ @mixin my-color($color) {
199
+ color: $color;
200
+ }
201
+
202
+ .red {
203
+ @include my-color(red);
204
+ }
205
+ </style>
206
+
207
+ <style lang="scss" module="$scss">
208
+ @mixin my-color($color) {
209
+ color: $color;
210
+ }
211
+
212
+ .red {
213
+ @include my-color(red);
214
+ }
215
+ </style>
216
+
217
+ <style lang="less" scoped>
218
+ @color: green;
219
+
220
+ .green {
221
+ color: @color;
222
+ }
223
+ </style>
224
+
225
+ <style lang="less" module="$less">
226
+ @color: green;
227
+
228
+ .green {
229
+ color: @color;
230
+ }
231
+ </style>
232
+
233
+ <style lang="stylus" scoped>
234
+ my-color()
235
+ color: arguments
236
+
237
+ .indigo
238
+ my-color: indigo
239
+ </style>
240
+
241
+ <style lang="stylus" module="$stylus">
242
+ my-color()
243
+ color: arguments
244
+
245
+ .indigo
246
+ my-color: indigo
247
+ </style>
248
+ ```
0 commit comments