33const postcss = require ( 'postcss' ) ;
44const selectorParser = require ( 'postcss-selector-parser' ) ;
55const valueParser = require ( 'postcss-value-parser' ) ;
6+ const { extractICSS } = require ( 'icss-utils' ) ;
67
78const isSpacing = node => node . type === 'combinator' && node . value === ' ' ;
89
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+
924function normalizeNodeArray ( nodes ) {
1025 const array = [ ] ;
1126
@@ -25,7 +40,7 @@ function normalizeNodeArray(nodes) {
2540 return array ;
2641}
2742
28- function localizeNode ( rule , mode , options ) {
43+ function localizeNode ( rule , mode , localAliasMap ) {
2944 const isScopePseudo = node =>
3045 node . value === ':local' || node . value === ':global' ;
3146
@@ -191,7 +206,14 @@ function localizeNode(rule, mode, options) {
191206 }
192207 case 'id' :
193208 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 ) {
195217 const innerNode = node . clone ( ) ;
196218 innerNode . spaces = { before : '' , after : '' } ;
197219
@@ -231,8 +253,10 @@ function localizeDeclNode(node, context) {
231253 switch ( node . type ) {
232254 case 'word' :
233255 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+ }
236260 }
237261 break ;
238262
@@ -360,6 +384,7 @@ function localizeAnimationShorthandDeclValues(decl, context) {
360384 options : context . options ,
361385 global : context . global ,
362386 localizeNextItem : shouldParseAnimationName && ! context . global ,
387+ localAliasMap : context . localAliasMap ,
363388 } ;
364389 return localizeDeclNode ( node , subContext ) ;
365390 } ) ;
@@ -374,6 +399,7 @@ function localizeDeclValues(localize, decl, context) {
374399 options : context . options ,
375400 global : context . global ,
376401 localizeNextItem : localize && ! context . global ,
402+ localAliasMap : context . localAliasMap ,
377403 } ;
378404 nodes [ index ] = localizeDeclNode ( node , subContext ) ;
379405 } ) ;
@@ -423,6 +449,9 @@ module.exports = postcss.plugin('postcss-modules-local-by-default', function(
423449 const globalMode = options && options . mode === 'global' ;
424450
425451 return function ( css ) {
452+ const { icssImports } = extractICSS ( css , false ) ;
453+ const localAliasMap = getImportLocalAliases ( icssImports ) ;
454+
426455 css . walkAtRules ( function ( atrule ) {
427456 if ( / k e y f r a m e s $ / i. test ( atrule . name ) ) {
428457 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(
440469 atrule . params = localMatch [ 0 ] ;
441470 globalKeyframes = false ;
442471 } else if ( ! globalMode ) {
443- atrule . params = ':local(' + atrule . params + ')' ;
472+ if ( atrule . params && ! localAliasMap . has ( atrule . params ) )
473+ atrule . params = ':local(' + atrule . params + ')' ;
444474 }
445475 atrule . walkDecls ( function ( decl ) {
446476 localizeDecl ( decl , {
477+ localAliasMap,
447478 options : options ,
448479 global : globalKeyframes ,
449480 } ) ;
@@ -452,6 +483,7 @@ module.exports = postcss.plugin('postcss-modules-local-by-default', function(
452483 atrule . nodes . forEach ( function ( decl ) {
453484 if ( decl . type === 'decl' ) {
454485 localizeDecl ( decl , {
486+ localAliasMap,
455487 options : options ,
456488 global : globalMode ,
457489 } ) ;
@@ -478,9 +510,10 @@ module.exports = postcss.plugin('postcss-modules-local-by-default', function(
478510 return ;
479511 }
480512
481- const context = localizeNode ( rule , options . mode ) ;
513+ const context = localizeNode ( rule , options . mode , localAliasMap ) ;
482514
483515 context . options = options ;
516+ context . localAliasMap = localAliasMap ;
484517
485518 if ( pureMode && context . hasPureGlobals ) {
486519 throw rule . error (
0 commit comments