3
3
const postcss = require ( 'postcss' ) ;
4
4
const selectorParser = require ( 'postcss-selector-parser' ) ;
5
5
const valueParser = require ( 'postcss-value-parser' ) ;
6
+ const { extractICSS } = require ( 'icss-utils' ) ;
6
7
7
8
const isSpacing = node => node . type === 'combinator' && node . value === ' ' ;
8
9
10
+ function getImportLocalAliases ( icssImports ) {
11
+ const localAliases = new Map ( ) ;
12
+ Object . keys ( icssImports ) . forEach ( key => {
13
+ Object . keys ( icssImports [ key ] ) . forEach ( prop => {
14
+ localAliases . set ( prop , icssImports [ key ] [ prop ] ) ;
15
+ } ) ;
16
+ } ) ;
17
+ return localAliases ;
18
+ }
19
+
20
+ function maybeLocalizeValue ( value , localAliasMap ) {
21
+ if ( localAliasMap . has ( value ) ) return value ;
22
+ }
23
+
9
24
function normalizeNodeArray ( nodes ) {
10
25
const array = [ ] ;
11
26
@@ -25,7 +40,7 @@ function normalizeNodeArray(nodes) {
25
40
return array ;
26
41
}
27
42
28
- function localizeNode ( rule , mode , options ) {
43
+ function localizeNode ( rule , mode , localAliasMap ) {
29
44
const isScopePseudo = node =>
30
45
node . value === ':local' || node . value === ':global' ;
31
46
@@ -191,7 +206,14 @@ function localizeNode(rule, mode, options) {
191
206
}
192
207
case 'id' :
193
208
case 'class' : {
194
- if ( ! context . global ) {
209
+ if ( context . global ) {
210
+ break ;
211
+ }
212
+
213
+ const isImportedValue = localAliasMap . has ( node . value ) ;
214
+ const isImportedWithExplicitScope = isImportedValue && context . explicit ;
215
+
216
+ if ( ! isImportedValue || isImportedWithExplicitScope ) {
195
217
const innerNode = node . clone ( ) ;
196
218
innerNode . spaces = { before : '' , after : '' } ;
197
219
@@ -231,8 +253,10 @@ function localizeDeclNode(node, context) {
231
253
switch ( node . type ) {
232
254
case 'word' :
233
255
if ( context . localizeNextItem ) {
234
- node . value = ':local(' + node . value + ')' ;
235
- context . localizeNextItem = false ;
256
+ if ( ! context . localAliasMap . has ( node . value ) ) {
257
+ node . value = ':local(' + node . value + ')' ;
258
+ context . localizeNextItem = false ;
259
+ }
236
260
}
237
261
break ;
238
262
@@ -360,6 +384,7 @@ function localizeAnimationShorthandDeclValues(decl, context) {
360
384
options : context . options ,
361
385
global : context . global ,
362
386
localizeNextItem : shouldParseAnimationName && ! context . global ,
387
+ localAliasMap : context . localAliasMap ,
363
388
} ;
364
389
return localizeDeclNode ( node , subContext ) ;
365
390
} ) ;
@@ -374,6 +399,7 @@ function localizeDeclValues(localize, decl, context) {
374
399
options : context . options ,
375
400
global : context . global ,
376
401
localizeNextItem : localize && ! context . global ,
402
+ localAliasMap : context . localAliasMap ,
377
403
} ;
378
404
nodes [ index ] = localizeDeclNode ( node , subContext ) ;
379
405
} ) ;
@@ -423,6 +449,9 @@ module.exports = postcss.plugin('postcss-modules-local-by-default', function(
423
449
const globalMode = options && options . mode === 'global' ;
424
450
425
451
return function ( css ) {
452
+ const { icssImports } = extractICSS ( css , false ) ;
453
+ const localAliasMap = getImportLocalAliases ( icssImports ) ;
454
+
426
455
css . walkAtRules ( function ( atrule ) {
427
456
if ( / k e y f r a m e s $ / i. test ( atrule . name ) ) {
428
457
const globalMatch = / ^ \s * : g l o b a l \s * \( ( .+ ) \) \s * $ / . exec ( atrule . params ) ;
@@ -440,10 +469,12 @@ module.exports = postcss.plugin('postcss-modules-local-by-default', function(
440
469
atrule . params = localMatch [ 0 ] ;
441
470
globalKeyframes = false ;
442
471
} else if ( ! globalMode ) {
443
- atrule . params = ':local(' + atrule . params + ')' ;
472
+ if ( atrule . params && ! localAliasMap . has ( atrule . params ) )
473
+ atrule . params = ':local(' + atrule . params + ')' ;
444
474
}
445
475
atrule . walkDecls ( function ( decl ) {
446
476
localizeDecl ( decl , {
477
+ localAliasMap,
447
478
options : options ,
448
479
global : globalKeyframes ,
449
480
} ) ;
@@ -452,6 +483,7 @@ module.exports = postcss.plugin('postcss-modules-local-by-default', function(
452
483
atrule . nodes . forEach ( function ( decl ) {
453
484
if ( decl . type === 'decl' ) {
454
485
localizeDecl ( decl , {
486
+ localAliasMap,
455
487
options : options ,
456
488
global : globalMode ,
457
489
} ) ;
@@ -478,9 +510,10 @@ module.exports = postcss.plugin('postcss-modules-local-by-default', function(
478
510
return ;
479
511
}
480
512
481
- const context = localizeNode ( rule , options . mode ) ;
513
+ const context = localizeNode ( rule , options . mode , localAliasMap ) ;
482
514
483
515
context . options = options ;
516
+ context . localAliasMap = localAliasMap ;
484
517
485
518
if ( pureMode && context . hasPureGlobals ) {
486
519
throw rule . error (
0 commit comments