11const postcss = require ( 'postcss' )
22const { default : replaceSymbols , replaceAll } = require ( 'icss-replace-symbols' )
3- const { genICSSRules } = require ( './icss.js' )
3+ const {
4+ extractICSSImports,
5+ extractICSSExports,
6+ genICSSRules
7+ } = require ( './icss.js' )
48
59const matchImports = / ^ ( .+ ?| \( [ \s \S ] + ?\) ) \s + f r o m \s + ( " [ ^ " ] * " | ' [ ^ ' ] * ' | [ \w - ] + ) $ /
610const matchValueDefinition = / (?: \s + | ^ ) ( [ \w - ] + ) : ? \s + ( .+ ?) \s * $ / g
711const matchImport = / ^ ( [ \w - ] + ) (?: \s + a s \s + ( [ \w - ] + ) ) ? /
812
13+ // 'i' prefix to prevent postcss parsing "_" as css hook
914const getAliasName = ( name , index ) =>
10- `__value_ ${ name . replace ( / \W / g, '_' ) } _${ index } `
15+ `i__value_ ${ name . replace ( / \W / g, '_' ) } _${ index } `
1116
1217let importIndex = 0
1318const createImportedName = importName => getAliasName ( importName , importIndex ++ )
@@ -16,15 +21,15 @@ module.exports = postcss.plugin('postcss-modules-values', () => (
1621 css ,
1722 result
1823) => {
19- let importAliases = [ ]
20- let definitions = { }
24+ const imports = extractICSSImports ( css )
25+ const exports = extractICSSExports ( css )
2126
2227 const addDefinition = atRule => {
2328 let matches
2429 while ( ( matches = matchValueDefinition . exec ( atRule . params ) ) ) {
2530 let [ , key , value ] = matches
2631 // Add to the definitions, knowing that values can refer to each other
27- definitions [ key ] = replaceAll ( definitions , value )
32+ exports [ key ] = replaceAll ( exports , value )
2833 atRule . remove ( )
2934 }
3035 }
@@ -34,7 +39,7 @@ module.exports = postcss.plugin('postcss-modules-values', () => (
3439 if ( matches ) {
3540 let [ , aliasesString , path ] = matches
3641 // We can use constants for path names
37- if ( definitions [ path ] ) path = definitions [ path ]
42+ if ( exports [ path ] ) path = exports [ path ]
3843 let aliases = aliasesString
3944 . replace ( / ^ \( \s * ( [ \s \S ] + ) \s * \) $ / , '$1' )
4045 . split ( / \s * , \s * / )
@@ -43,7 +48,7 @@ module.exports = postcss.plugin('postcss-modules-values', () => (
4348 if ( tokens ) {
4449 let [ , theirName , myName = theirName ] = tokens
4550 let importedName = createImportedName ( myName )
46- definitions [ myName ] = importedName
51+ exports [ myName ] = importedName
4752 return { theirName, importedName }
4853 } else {
4954 throw new Error ( `@import statement "${ alias } " is invalid!` )
@@ -53,7 +58,7 @@ module.exports = postcss.plugin('postcss-modules-values', () => (
5358 acc [ importedName ] = theirName
5459 return acc
5560 } , { } )
56- importAliases . push ( { path, aliases } )
61+ imports [ path ] = Object . assign ( { } , imports [ path ] , aliases )
5762 atRule . remove ( )
5863 }
5964 }
@@ -72,10 +77,10 @@ module.exports = postcss.plugin('postcss-modules-values', () => (
7277 } )
7378
7479 /* If we have no definitions, don't continue */
75- if ( Object . keys ( definitions ) . length === 0 ) return
80+ if ( Object . keys ( exports ) . length === 0 ) return
7681
7782 /* Perform replacements */
78- replaceSymbols ( css , definitions )
83+ replaceSymbols ( css , exports )
7984
80- css . prepend ( genICSSRules ( importAliases , definitions ) )
85+ css . prepend ( genICSSRules ( imports , exports ) )
8186} )
0 commit comments