@@ -12,7 +12,9 @@ function getSingleLocalNamesForComposes(root) {
12
12
`composition is only allowed when selector is single :local class name not in "${ root } "`
13
13
) ;
14
14
}
15
+
15
16
node = node . nodes [ 0 ] ;
17
+
16
18
if (
17
19
node . type !== 'pseudo' ||
18
20
node . value !== ':local' ||
@@ -26,7 +28,9 @@ function getSingleLocalNamesForComposes(root) {
26
28
'" is weird'
27
29
) ;
28
30
}
31
+
29
32
node = node . first ;
33
+
30
34
if ( node . type !== 'selector' || node . length !== 1 ) {
31
35
throw new Error (
32
36
'composition is only allowed when selector is single :local class name not in "' +
@@ -36,7 +40,9 @@ function getSingleLocalNamesForComposes(root) {
36
40
'" is weird'
37
41
) ;
38
42
}
43
+
39
44
node = node . first ;
45
+
40
46
if ( node . type !== 'class' ) {
41
47
// 'id' is not possible, because you can't compose ids
42
48
throw new Error (
@@ -47,6 +53,7 @@ function getSingleLocalNamesForComposes(root) {
47
53
'" is weird'
48
54
) ;
49
55
}
56
+
50
57
return node . value ;
51
58
} ) ;
52
59
}
@@ -77,6 +84,8 @@ const processor = postcss.plugin('postcss-modules-scope', function(options) {
77
84
return css => {
78
85
const generateScopedName =
79
86
( options && options . generateScopedName ) || processor . generateScopedName ;
87
+ const generateExportEntry =
88
+ ( options && options . generateExportEntry ) || processor . generateExportEntry ;
80
89
81
90
const exports = Object . create ( null ) ;
82
91
@@ -86,13 +95,18 @@ const processor = postcss.plugin('postcss-modules-scope', function(options) {
86
95
css . source . input . from ,
87
96
css . source . input . css
88
97
) ;
98
+ const exportEntry = generateExportEntry (
99
+ rawName ? rawName : name ,
100
+ scopedName ,
101
+ css . source . input . from ,
102
+ css . source . input . css
103
+ ) ;
104
+ const { key, value } = exportEntry ;
89
105
90
- exports [ name ] = exports [ name ] || [ ] ;
91
-
92
- const unescapedScopedName = unescape ( scopedName ) ;
106
+ exports [ key ] = exports [ key ] || [ ] ;
93
107
94
- if ( exports [ name ] . indexOf ( unescapedScopedName ) < 0 ) {
95
- exports [ name ] . push ( unescapedScopedName ) ;
108
+ if ( exports [ key ] . indexOf ( value ) < 0 ) {
109
+ exports [ key ] . push ( value ) ;
96
110
}
97
111
98
112
return scopedName ;
@@ -119,6 +133,7 @@ const processor = postcss.plugin('postcss-modules-scope', function(options) {
119
133
} ) ;
120
134
}
121
135
}
136
+
122
137
throw new Error (
123
138
`${ node . type } ("${ node } ") is not allowed in a :local block`
124
139
) ;
@@ -131,12 +146,14 @@ const processor = postcss.plugin('postcss-modules-scope', function(options) {
131
146
if ( node . nodes . length !== 1 ) {
132
147
throw new Error ( 'Unexpected comma (",") in :local block' ) ;
133
148
}
149
+
134
150
const selector = localizeNode ( node . first , node . spaces ) ;
135
151
// move the spaces that were around the psuedo selector to the first
136
152
// non-container node
137
153
selector . first . spaces = node . spaces ;
138
154
139
155
node . replaceWith ( selector ) ;
156
+
140
157
return ;
141
158
}
142
159
/* falls through */
@@ -151,6 +168,7 @@ const processor = postcss.plugin('postcss-modules-scope', function(options) {
151
168
152
169
// Find any :import and remember imported names
153
170
const importedNames = { } ;
171
+
154
172
css . walkRules ( rule => {
155
173
if ( / ^ : i m p o r t \( .+ \) $ / . test ( rule . selector ) ) {
156
174
rule . walkDecls ( decl => {
@@ -173,10 +191,11 @@ const processor = postcss.plugin('postcss-modules-scope', function(options) {
173
191
let parsedSelector = selectorParser ( ) . astSync ( rule ) ;
174
192
175
193
rule . selector = traverseNode ( parsedSelector . clone ( ) ) . toString ( ) ;
176
- // console.log(rule.selector);
194
+
177
195
rule . walkDecls ( / c o m p o s e s | c o m p o s e - w i t h / , decl => {
178
196
const localNames = getSingleLocalNamesForComposes ( parsedSelector ) ;
179
197
const classes = decl . value . split ( / \s + / ) ;
198
+
180
199
classes . forEach ( className => {
181
200
const global = / ^ g l o b a l \( ( [ ^ \) ] + ) \) $ / . exec ( className ) ;
182
201
@@ -200,14 +219,17 @@ const processor = postcss.plugin('postcss-modules-scope', function(options) {
200
219
) ;
201
220
}
202
221
} ) ;
222
+
203
223
decl . remove ( ) ;
204
224
} ) ;
205
225
206
226
rule . walkDecls ( decl => {
207
- var tokens = decl . value . split ( / ( , | ' [ ^ ' ] * ' | " [ ^ " ] * " ) / ) ;
227
+ let tokens = decl . value . split ( / ( , | ' [ ^ ' ] * ' | " [ ^ " ] * " ) / ) ;
228
+
208
229
tokens = tokens . map ( ( token , idx ) => {
209
230
if ( idx === 0 || tokens [ idx - 1 ] === ',' ) {
210
231
const localMatch = / ^ ( \s * ) : l o c a l \s * \( ( .+ ?) \) / . exec ( token ) ;
232
+
211
233
if ( localMatch ) {
212
234
return (
213
235
localMatch [ 1 ] +
@@ -221,14 +243,16 @@ const processor = postcss.plugin('postcss-modules-scope', function(options) {
221
243
return token ;
222
244
}
223
245
} ) ;
246
+
224
247
decl . value = tokens . join ( '' ) ;
225
248
} ) ;
226
249
} ) ;
227
250
228
251
// Find any :local keyframes
229
252
css . walkAtRules ( atrule => {
230
253
if ( / k e y f r a m e s $ / i. test ( atrule . name ) ) {
231
- var localMatch = / ^ \s * : l o c a l \s * \( ( .+ ?) \) \s * $ / . exec ( atrule . params ) ;
254
+ const localMatch = / ^ \s * : l o c a l \s * \( ( .+ ?) \) \s * $ / . exec ( atrule . params ) ;
255
+
232
256
if ( localMatch ) {
233
257
atrule . params = exportScopedName ( localMatch [ 1 ] ) ;
234
258
}
@@ -237,26 +261,37 @@ const processor = postcss.plugin('postcss-modules-scope', function(options) {
237
261
238
262
// If we found any :locals, insert an :export rule
239
263
const exportedNames = Object . keys ( exports ) ;
264
+
240
265
if ( exportedNames . length > 0 ) {
241
266
const exportRule = postcss . rule ( { selector : ':export' } ) ;
267
+
242
268
exportedNames . forEach ( exportedName =>
243
269
exportRule . append ( {
244
270
prop : exportedName ,
245
271
value : exports [ exportedName ] . join ( ' ' ) ,
246
272
raws : { before : '\n ' } ,
247
273
} )
248
274
) ;
275
+
249
276
css . append ( exportRule ) ;
250
277
}
251
278
} ;
252
279
} ) ;
253
280
254
- processor . generateScopedName = function ( exportedName , path ) {
281
+ processor . generateScopedName = function ( name , path ) {
255
282
const sanitisedPath = path
256
283
. replace ( / \. [ ^ \. \/ \\ ] + $ / , '' )
257
284
. replace ( / [ \W _ ] + / g, '_' )
258
285
. replace ( / ^ _ | _ $ / g, '' ) ;
259
- return `_${ sanitisedPath } __${ exportedName } ` . trim ( ) ;
286
+
287
+ return `_${ sanitisedPath } __${ name } ` . trim ( ) ;
288
+ } ;
289
+
290
+ processor . generateExportEntry = function ( name , scopedName ) {
291
+ return {
292
+ key : unescape ( name ) ,
293
+ value : unescape ( scopedName ) ,
294
+ } ;
260
295
} ;
261
296
262
297
module . exports = processor ;
0 commit comments