1
- import { compileStyle } from '../src/compileStyle'
1
+ import { compileStyle , compileStyleAsync } from '../src/compileStyle'
2
2
import { mockWarn } from '@vue/shared'
3
3
4
- function compile ( source : string ) : string {
5
- const res = compileStyle ( {
6
- source,
7
- filename : 'test.css' ,
8
- id : 'test'
9
- } )
10
- if ( res . errors . length ) {
11
- res . errors . forEach ( err => {
12
- console . error ( err )
13
- } )
14
- expect ( res . errors . length ) . toBe ( 0 )
15
- }
16
- return res . code
17
- }
18
-
19
4
describe ( 'SFC scoped CSS' , ( ) => {
20
5
mockWarn ( )
21
6
7
+ function compileScoped ( source : string ) : string {
8
+ const res = compileStyle ( {
9
+ source,
10
+ filename : 'test.css' ,
11
+ id : 'test' ,
12
+ scoped : true
13
+ } )
14
+ if ( res . errors . length ) {
15
+ res . errors . forEach ( err => {
16
+ console . error ( err )
17
+ } )
18
+ expect ( res . errors . length ) . toBe ( 0 )
19
+ }
20
+ return res . code
21
+ }
22
+
22
23
test ( 'simple selectors' , ( ) => {
23
- expect ( compile ( `h1 { color: red; }` ) ) . toMatch ( `h1[test] { color: red;` )
24
- expect ( compile ( `.foo { color: red; }` ) ) . toMatch ( `.foo[test] { color: red;` )
24
+ expect ( compileScoped ( `h1 { color: red; }` ) ) . toMatch (
25
+ `h1[test] { color: red;`
26
+ )
27
+ expect ( compileScoped ( `.foo { color: red; }` ) ) . toMatch (
28
+ `.foo[test] { color: red;`
29
+ )
25
30
} )
26
31
27
32
test ( 'descendent selector' , ( ) => {
28
- expect ( compile ( `h1 .foo { color: red; }` ) ) . toMatch (
33
+ expect ( compileScoped ( `h1 .foo { color: red; }` ) ) . toMatch (
29
34
`h1 .foo[test] { color: red;`
30
35
)
31
36
} )
32
37
33
38
test ( 'multiple selectors' , ( ) => {
34
- expect ( compile ( `h1 .foo, .bar, .baz { color: red; }` ) ) . toMatch (
39
+ expect ( compileScoped ( `h1 .foo, .bar, .baz { color: red; }` ) ) . toMatch (
35
40
`h1 .foo[test], .bar[test], .baz[test] { color: red;`
36
41
)
37
42
} )
38
43
39
44
test ( 'pseudo class' , ( ) => {
40
- expect ( compile ( `.foo:after { color: red; }` ) ) . toMatch (
45
+ expect ( compileScoped ( `.foo:after { color: red; }` ) ) . toMatch (
41
46
`.foo[test]:after { color: red;`
42
47
)
43
48
} )
44
49
45
50
test ( 'pseudo element' , ( ) => {
46
- expect ( compile ( `::selection { display: none; }` ) ) . toMatch (
51
+ expect ( compileScoped ( `::selection { display: none; }` ) ) . toMatch (
47
52
'[test]::selection {'
48
53
)
49
54
} )
50
55
51
56
test ( 'spaces before pseudo element' , ( ) => {
52
- const code = compile ( `.abc, ::selection { color: red; }` )
57
+ const code = compileScoped ( `.abc, ::selection { color: red; }` )
53
58
expect ( code ) . toMatch ( '.abc[test],' )
54
59
expect ( code ) . toMatch ( '[test]::selection {' )
55
60
} )
56
61
57
62
test ( '::v-deep' , ( ) => {
58
- expect ( compile ( `::v-deep(.foo) { color: red; }` ) ) . toMatchInlineSnapshot ( `
63
+ expect ( compileScoped ( `::v-deep(.foo) { color: red; }` ) )
64
+ . toMatchInlineSnapshot ( `
59
65
"[test] .foo { color: red;
60
66
}"
61
67
` )
62
- expect ( compile ( `::v-deep(.foo .bar) { color: red; }` ) )
68
+ expect ( compileScoped ( `::v-deep(.foo .bar) { color: red; }` ) )
63
69
. toMatchInlineSnapshot ( `
64
70
"[test] .foo .bar { color: red;
65
71
}"
66
72
` )
67
- expect ( compile ( `.baz .qux ::v-deep(.foo .bar) { color: red; }` ) )
73
+ expect ( compileScoped ( `.baz .qux ::v-deep(.foo .bar) { color: red; }` ) )
68
74
. toMatchInlineSnapshot ( `
69
75
".baz .qux[test] .foo .bar { color: red;
70
76
}"
71
77
` )
72
78
} )
73
79
74
80
test ( '::v-slotted' , ( ) => {
75
- expect ( compile ( `::v-slotted(.foo) { color: red; }` ) ) . toMatchInlineSnapshot ( `
81
+ expect ( compileScoped ( `::v-slotted(.foo) { color: red; }` ) )
82
+ . toMatchInlineSnapshot ( `
76
83
".foo[test-s] { color: red;
77
84
}"
78
85
` )
79
- expect ( compile ( `::v-slotted(.foo .bar) { color: red; }` ) )
86
+ expect ( compileScoped ( `::v-slotted(.foo .bar) { color: red; }` ) )
80
87
. toMatchInlineSnapshot ( `
81
88
".foo .bar[test-s] { color: red;
82
89
}"
83
90
` )
84
- expect ( compile ( `.baz .qux ::v-slotted(.foo .bar) { color: red; }` ) )
91
+ expect ( compileScoped ( `.baz .qux ::v-slotted(.foo .bar) { color: red; }` ) )
85
92
. toMatchInlineSnapshot ( `
86
93
".baz .qux .foo .bar[test-s] { color: red;
87
94
}"
88
95
` )
89
96
} )
90
97
91
98
test ( '::v-global' , ( ) => {
92
- expect ( compile ( `::v-global(.foo) { color: red; }` ) ) . toMatchInlineSnapshot ( `
99
+ expect ( compileScoped ( `::v-global(.foo) { color: red; }` ) )
100
+ . toMatchInlineSnapshot ( `
93
101
".foo { color: red;
94
102
}"
95
103
` )
96
- expect ( compile ( `::v-global(.foo .bar) { color: red; }` ) )
104
+ expect ( compileScoped ( `::v-global(.foo .bar) { color: red; }` ) )
97
105
. toMatchInlineSnapshot ( `
98
106
".foo .bar { color: red;
99
107
}"
100
108
` )
101
109
// global ignores anything before it
102
- expect ( compile ( `.baz .qux ::v-global(.foo .bar) { color: red; }` ) )
110
+ expect ( compileScoped ( `.baz .qux ::v-global(.foo .bar) { color: red; }` ) )
103
111
. toMatchInlineSnapshot ( `
104
112
".foo .bar { color: red;
105
113
}"
106
114
` )
107
115
} )
108
116
109
117
test ( 'media query' , ( ) => {
110
- expect ( compile ( `@media print { .foo { color: red }}` ) )
118
+ expect ( compileScoped ( `@media print { .foo { color: red }}` ) )
111
119
. toMatchInlineSnapshot ( `
112
120
"@media print {
113
121
.foo[test] { color: red
@@ -116,7 +124,7 @@ describe('SFC scoped CSS', () => {
116
124
} )
117
125
118
126
test ( 'supports query' , ( ) => {
119
- expect ( compile ( `@supports(display: grid) { .foo { display: grid }}` ) )
127
+ expect ( compileScoped ( `@supports(display: grid) { .foo { display: grid }}` ) )
120
128
. toMatchInlineSnapshot ( `
121
129
"@supports(display: grid) {
122
130
.foo[test] { display: grid
@@ -125,7 +133,7 @@ describe('SFC scoped CSS', () => {
125
133
} )
126
134
127
135
test ( 'scoped keyframes' , ( ) => {
128
- const style = compile ( `
136
+ const style = compileScoped ( `
129
137
.anim {
130
138
animation: color 5s infinite, other 5s;
131
139
}
@@ -184,25 +192,20 @@ describe('SFC scoped CSS', () => {
184
192
185
193
// vue-loader/#1370
186
194
test ( 'spaces after selector' , ( ) => {
187
- const { code } = compileStyle ( {
188
- source : `.foo , .bar { color: red; }` ,
189
- filename : 'test.css' ,
190
- id : 'test'
191
- } )
192
-
193
- expect ( code ) . toMatchInlineSnapshot ( `
195
+ expect ( compileScoped ( `.foo , .bar { color: red; }` ) ) . toMatchInlineSnapshot ( `
194
196
".foo[test], .bar[test] { color: red;
195
197
}"
196
198
` )
197
199
} )
198
200
199
201
describe ( 'deprecated syntax' , ( ) => {
200
202
test ( '::v-deep as combinator' , ( ) => {
201
- expect ( compile ( `::v-deep .foo { color: red; }` ) ) . toMatchInlineSnapshot ( `
203
+ expect ( compileScoped ( `::v-deep .foo { color: red; }` ) )
204
+ . toMatchInlineSnapshot ( `
202
205
"[test] .foo { color: red;
203
206
}"
204
207
` )
205
- expect ( compile ( `.bar ::v-deep .foo { color: red; }` ) )
208
+ expect ( compileScoped ( `.bar ::v-deep .foo { color: red; }` ) )
206
209
. toMatchInlineSnapshot ( `
207
210
".bar[test] .foo { color: red;
208
211
}"
@@ -213,7 +216,7 @@ describe('SFC scoped CSS', () => {
213
216
} )
214
217
215
218
test ( '>>> (deprecated syntax)' , ( ) => {
216
- const code = compile ( `>>> .foo { color: red; }` )
219
+ const code = compileScoped ( `>>> .foo { color: red; }` )
217
220
expect ( code ) . toMatchInlineSnapshot ( `
218
221
"[test] .foo { color: red;
219
222
}"
@@ -224,7 +227,7 @@ describe('SFC scoped CSS', () => {
224
227
} )
225
228
226
229
test ( '/deep/ (deprecated syntax)' , ( ) => {
227
- const code = compile ( `/deep/ .foo { color: red; }` )
230
+ const code = compileScoped ( `/deep/ .foo { color: red; }` )
228
231
expect ( code ) . toMatchInlineSnapshot ( `
229
232
"[test] .foo { color: red;
230
233
}"
@@ -235,3 +238,35 @@ describe('SFC scoped CSS', () => {
235
238
} )
236
239
} )
237
240
} )
241
+
242
+ describe ( 'SFC CSS modules' , ( ) => {
243
+ test ( 'should include resulting classes object in result' , async ( ) => {
244
+ const result = await compileStyleAsync ( {
245
+ source : `.red { color: red }\n.green { color: green }\n:global(.blue) { color: blue }` ,
246
+ filename : `test.css` ,
247
+ id : 'test' ,
248
+ modules : true
249
+ } )
250
+ expect ( result . modules ) . toBeDefined ( )
251
+ expect ( result . modules ! . red ) . toMatch ( '_red_' )
252
+ expect ( result . modules ! . green ) . toMatch ( '_green_' )
253
+ expect ( result . modules ! . blue ) . toBeUndefined ( )
254
+ } )
255
+
256
+ test ( 'postcss-modules options' , async ( ) => {
257
+ const result = await compileStyleAsync ( {
258
+ source : `:local(.foo-bar) { color: red }\n.baz-qux { color: green }` ,
259
+ filename : `test.css` ,
260
+ id : 'test' ,
261
+ modules : true ,
262
+ modulesOptions : {
263
+ scopeBehaviour : 'global' ,
264
+ generateScopedName : `[name]__[local]__[hash:base64:5]` ,
265
+ localsConvention : 'camelCaseOnly'
266
+ }
267
+ } )
268
+ expect ( result . modules ) . toBeDefined ( )
269
+ expect ( result . modules ! . fooBar ) . toMatch ( '__foo-bar__' )
270
+ expect ( result . modules ! . bazQux ) . toBeUndefined ( )
271
+ } )
272
+ } )
0 commit comments