@@ -88,7 +88,7 @@ const plugin = (options = {}) => {
88
88
89
89
return {
90
90
postcssPlugin : "postcss-modules-scope" ,
91
- OnceExit ( root , { rule } ) {
91
+ Once ( root , { rule } ) {
92
92
const exports = Object . create ( null ) ;
93
93
94
94
function exportScopedName ( name , rawName ) {
@@ -188,15 +188,13 @@ const plugin = (options = {}) => {
188
188
// Find any :import and remember imported names
189
189
const importedNames = { } ;
190
190
191
- root . walkRules ( ( rule ) => {
192
- if ( / ^ : i m p o r t \( .+ \) $ / . test ( rule . selector ) ) {
193
- rule . walkDecls ( ( decl ) => {
194
- importedNames [ decl . prop ] = true ;
195
- } ) ;
196
- }
191
+ root . walkRules ( / ^ : i m p o r t \( .+ \) $ / , ( rule ) => {
192
+ rule . walkDecls ( ( decl ) => {
193
+ importedNames [ decl . prop ] = true ;
194
+ } ) ;
197
195
} ) ;
198
196
199
- // Find any :local classes
197
+ // Find any :local selectors
200
198
root . walkRules ( ( rule ) => {
201
199
let parsedSelector = selectorParser ( ) . astSync ( rule ) ;
202
200
@@ -233,30 +231,31 @@ const plugin = (options = {}) => {
233
231
decl . remove ( ) ;
234
232
} ) ;
235
233
234
+ // Find any :local values
236
235
rule . walkDecls ( ( decl ) => {
236
+ if ( ! / : l o c a l \s * \( ( .+ ?) \) / . test ( decl . value ) ) {
237
+ return ;
238
+ }
239
+
237
240
let tokens = decl . value . split ( / ( , | ' [ ^ ' ] * ' | " [ ^ " ] * " ) / ) ;
238
241
239
242
tokens = tokens . map ( ( token , idx ) => {
240
243
if ( idx === 0 || tokens [ idx - 1 ] === "," ) {
241
244
let result = token ;
242
245
243
- const localMatch = / ^ ( \s * ) : l o c a l \s * \( ( .+ ?) \) / . exec ( token ) ;
244
- const nextLocalMatch = / : l o c a l \s * \( ( .+ ?) \) / . exec ( token ) ;
246
+ const localMatch = / : l o c a l \s * \( ( .+ ?) \) / . exec ( token ) ;
245
247
246
248
if ( localMatch ) {
247
- result =
248
- localMatch [ 1 ] +
249
- exportScopedName ( localMatch [ 2 ] ) +
250
- token . substr ( localMatch [ 0 ] . length ) ;
251
- } else if ( nextLocalMatch ) {
252
- const input = nextLocalMatch . input ;
253
- const matchPattern = nextLocalMatch [ 0 ] ;
254
- const matchVal = nextLocalMatch [ 1 ] ;
249
+ const input = localMatch . input ;
250
+ const matchPattern = localMatch [ 0 ] ;
251
+ const matchVal = localMatch [ 1 ] ;
255
252
const newVal = exportScopedName ( matchVal ) ;
253
+
256
254
result = input . replace ( matchPattern , newVal ) ;
257
255
} else {
258
- // do nothing
256
+ return token ;
259
257
}
258
+
260
259
return result ;
261
260
} else {
262
261
return token ;
@@ -268,14 +267,14 @@ const plugin = (options = {}) => {
268
267
} ) ;
269
268
270
269
// Find any :local keyframes
271
- root . walkAtRules ( ( atRule ) => {
272
- if ( / k e y f r a m e s $ / i. test ( atRule . name ) ) {
273
- const localMatch = / ^ \s * : l o c a l \s * \( ( .+ ?) \) \s * $ / . exec ( atRule . params ) ;
270
+ root . walkAtRules ( / k e y f r a m e s $ / i, ( atRule ) => {
271
+ const localMatch = / ^ \s * : l o c a l \s * \( ( .+ ?) \) \s * $ / . exec ( atRule . params ) ;
274
272
275
- if ( localMatch ) {
276
- atRule . params = exportScopedName ( localMatch [ 1 ] ) ;
277
- }
273
+ if ( ! localMatch ) {
274
+ return ;
278
275
}
276
+
277
+ atRule . params = exportScopedName ( localMatch [ 1 ] ) ;
279
278
} ) ;
280
279
281
280
// If we found any :locals, insert an :export rule
0 commit comments